1

i am trying to insert products from an array.
The product is successfully created and the values are inserted normally except the sub-category.
The issue here is that the subcategory is registered as top level (no parent) or it's not registered at all. Anyone have any idea on what i am missing here ?

Outside from function

        $parent_cat_a= term_exists($myproduct['12'], 'category');
        $parent_cat_a_id= $parent_term_a['term_id'];
        eisagwghproion($myproductssimple);


Inside function

 $product_to_create = array(
            'post_content' => '',
            'post_status' => "publish",
            'post_title' => $myproduct['6'], 
            'post_status' => 'publish',
            'post_date' => $myproduct['22'],
            'post_parent' => '',
            'post_type' => "product",
                    );
                $post_id= wp_insert_post($product_to_create, true);
                if (!$post_id) //
                {
                    return false;
                }            
                update_post_meta($post_id, '_price', $myproduct['17']);
                update_post_meta($post_id, '_regular_price', $myproduct['17']);
                update_post_meta($post_id, '_sku', $myproduct['2'] );
                wp_set_object_terms($post_id, 'simple', 'product_type');

                                  if(strlen($myproduct['13']) > 1){
                    $parent_cat_a = term_exists($myproduct['13'], 'product_cat');
                    if($parent_cat_a !== null) //εαν υπαρχει η υπο κατηγορια
                        {
                            wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
                        }
                        else //εαν δεν υπάρχει η υπο κατηγορια
                        {
                            $parent_cat_a_id = $parent_cat_a['term_id'];  
                            wp_insert_term(
                                $myproduct['13'], // the term 
                                'product_cat', // the taxonomy
                                array(
                                'slug' => $myproduct['13'],
                                'parent'=> $parent_cat_a_id
                                )
                            );
                            wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
                        }
                    }
                    else{wp_set_object_terms($post_id, $myproduct['12'] , 'product_cat');}

1 Answer 1

0

with this code we add a new main category

wp_insert_term( 'My New Category', 'product_cat', array('parent' => 0));

then knowing the category parent id we can add a sub category like so

wp_insert_term( 'My New Sub-category', 'product_cat', array('parent' => 72));

this is more or less what you have, but when I run the function get_terms I receive an array with two values, this means your variable $parent_cat_a would be something like this

Array
(
    [term_id] => 74
    [term_taxonomy_id] => 74
)

This means when you are calling $parent_cat_a, you arent recieving the parent id, you are just putting an array with more than one choice, worpress will not know how to use it, you should be calling $parent_cat_a['term_id']

so where you have

$parent_cat_a = term_exists($myproduct['13'], 'product_cat');

I would replace it with

$parent_cat_aa = term_exists($myproduct['13'], 'product_cat');
$parent_cat_a = $parent_cat_aa['term_id'];

this probably will put your code to work, one more thing, like the first code above, inside the wp_insert_term() before parent you don't need the slug, wordpress will put it for you, will check the category name and make a slug without spaces. its safer once you are putting the exactly same name.

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.