Problem with adding programmatically woocommerce product post into category

Question

I have problem with adding product to category when I creating this product. I have this code:

$my_post = array(
        'post_type' => 'product',
        'post_title' => 'Drukarka '.$_POST['marka'].' '.$_POST['model'],
        'post_content' => $_POST['opis'],
        'post_status' => 'publish',
        'post_author'   => $current_user->ID,
        'taxonomy' => 'product_cat'

    );

    $post_id = wp_insert_post($my_post);

wp_set_object_terms( $post_id, get_term_by('slug', 'druk-cyfrowy', 'product_cat'), 'product_cat');

Var dump of: wp_set_object_terms( $post_id, get_term_by('slug', 'druk-cyfrowy', 'product_cat'), 'product_cat');

Returns this:

object(WP_Error)#12822 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(26) "Nieprawidłowa taksonomia." } } ["error_data"]=> array(0) { } }

And var dump: of get_term_by('slug', 'druk-cyfrowy', 'product_cat')

Returns false and I don’t know why.
Please help me

0
, Mariusz 4 years 2020-03-28T08:50:52-05:00 0 Answers 100 views 0

Leave an answer

Browse
Browse