woocommerce offtopic - Add attribute taxonomy through code don't appear in admin product page

I've insertedthe attribute manually after inserting new products through code:$attributes = Array( "type_pro

I've inserted the attribute manually after inserting new products through code:

$attributes = Array( 
                        "type_product1" => $data1, 
                        "type_product2" => $data2, 
                        "type_product3" => $data3,
                        "type_product4" => $data4, 
                        "type_product5" => $data5, 
                        "type_product6" => $data5
                    ); 

                foreach ($attributes as $key => $value) {
                    $term_taxonomy_ids = wp_set_object_terms( $product_id, $value, 'pa_'.$key, true );
                    $product_attributes = Array (
                        'pa_'.$key => Array(
                        'name' => 'pa_'.$key, // set attribute name
                        'value' => $value, // set attribute value
                        'is_visible' => 1,
                        'is_variation' => '1',
                        'is_taxonomy' => 1
                    )
                    );


                }
                    //Add as post meta
                    update_post_meta($post_ID, '_product_attributes', $product_attributes);

after that if I go in products -> attribute i see the count of single terms of name of attributes incremented but if i go in the single product under name attributes (custom product attribute) i'll see only last attribute (type_product6) set with value other attributes are not shown.... The strange thing is that if I go to the attribute page and i click on the count of single term of attribute it shows me the list of product with this attribute set!

How can I fix it?

I've inserted the attribute manually after inserting new products through code:

$attributes = Array( 
                        "type_product1" => $data1, 
                        "type_product2" => $data2, 
                        "type_product3" => $data3,
                        "type_product4" => $data4, 
                        "type_product5" => $data5, 
                        "type_product6" => $data5
                    ); 

                foreach ($attributes as $key => $value) {
                    $term_taxonomy_ids = wp_set_object_terms( $product_id, $value, 'pa_'.$key, true );
                    $product_attributes = Array (
                        'pa_'.$key => Array(
                        'name' => 'pa_'.$key, // set attribute name
                        'value' => $value, // set attribute value
                        'is_visible' => 1,
                        'is_variation' => '1',
                        'is_taxonomy' => 1
                    )
                    );


                }
                    //Add as post meta
                    update_post_meta($post_ID, '_product_attributes', $product_attributes);

after that if I go in products -> attribute i see the count of single terms of name of attributes incremented but if i go in the single product under name attributes (custom product attribute) i'll see only last attribute (type_product6) set with value other attributes are not shown.... The strange thing is that if I go to the attribute page and i click on the count of single term of attribute it shows me the list of product with this attribute set!

How can I fix it?

Share Improve this question edited Jun 15, 2017 at 15:43 walle asked Jun 1, 2017 at 9:45 wallewalle 132 silver badges7 bronze badges 1
  • Does anyone have an idea? – walle Commented Jun 15, 2017 at 13:13
Add a comment  | 

1 Answer 1

Reset to default 1

the solution is:

foreach ($attributes as $attr => $value) {
                            $attr = 'pa_'.$attr;
                            wp_set_object_terms($post_ID, $value, $attr);
                             $thedata[sanitize_title($attr)] = Array(
                                        'name' => wc_clean($attr),
                                        'value' => $value,
                                        'postion' => '0',
                                        'is_visible' => '1',
                                        'is_variation' => '1',
                                        'is_taxonomy' => '1'
                                );
                            }

                        update_post_meta($post_ID, '_product_attributes', $thedata);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745176578a4615221.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信