0

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?

1
  • Does anyone have an idea?
    – walle
    Jun 15, 2017 at 13:13

1 Answer 1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.