• Resolved tickdog

    (@tickdog)


    If I create new products with the Rest API all fields including the sku are created. I can view the products and the sku’s in the products area. But, if I try and sort by sku on the products page or try to test if the sku exists before adding a new product it is not seen.

    Example: $product_id = wc_get_product_id_by_sku($sku); returns 0 even if the sku exists.

    If however, I use the bulk edit function and change the status of those new items then the sku is recognized.

    What am I missing? Below is my code.

    $post_id = wp_insert_post( array(
              'post_author' => $user_id,
              'post_title' => $name,
              'post_status' => $publish,
              'post_type' => "product",
            ) );
    
            wp_set_object_terms( $post_id, 'simple', 'product_type' );
    
            update_post_meta( $post_id, '_visibility', 'visible' );
            update_post_meta( $post_id, '_sku', $sku);
            update_post_meta( $post_id, '_price', $regPrice );
            update_post_meta( $post_id, '_regular_price', $regPrice );
            update_post_meta( $post_id, '_tax_class', $tax_class);
            update_post_meta( $post_id, '_stock', $stock );
            update_post_meta( $post_id, '_manage_stock', $manageStock );
            update_post_meta( $post_id, '_backorders', 'no' );
    
            $term_ids = [];
            foreach ($product_categories as $key => $category) {
            //  $catName = $category->name
              $catId = $category->term_id;
              $catName = $category->name;
                foreach($inCats as $inCat) {
                  if ($catName == $inCat ) {
                    array_push($term_ids, $catId);
                   }
                 }
              }
            wp_set_object_terms( $post_id, $term_ids, 'product_cat' );
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Rest API new product create sets the sku but it is not recognized’ is closed to new replies.