0

I need to import products from third-party API to woocommerce. How to save data using wp_insert_post()?

name = string
sku = string
type = string
regular_price = double
description = string
weight = double
stock_quantity = int
manage_stock = bool
in_stock = bool
categories = {array} // ['id' => $id]
images = string 
attributes = {array}

I have this code but I don't know how to add categories, images, and attributes.

foreach ( $products as $product ) {
            $item = [
                'post_title' => $product->name,
                'post_status' => 'publish',
                'post_type' => 'product'
            ];

            $post_id = wp_insert_post($item);
            wp_set_object_terms( $post_id, 'simple', 'product_type' );

            update_post_meta( $post_id, '_visibility', 'visible' );
            update_post_meta( $post_id, '_stock_status', 'instock');
            update_post_meta( $post_id, '_regular_price', $product->retail_price );
            update_post_meta( $post_id, '_price', $product->retail_price );
            update_post_meta( $post_id, '_weight', $product->weight );
            update_post_meta( $post_id, '_sku', $product->id_product );
            update_post_meta( $post_id, '_stock', $product->stock );                
            update_post_meta( $post_id, '_categories', $product->retail_price );
            update_post_meta( $post_id, '_images', $product->retail_price );
            update_post_meta( $post_id, '_manage_stock', 'yes' );
        }
0

There are answers which helped me:

Category: https://stackoverflow.com/questions/27219635/how-to-set-category-to-product-woocommerce

Images: How do I set a featured image (thumbnail) by image URL when using wp_insert_post()?

Attributes: https://stackoverflow.com/questions/23444319/wordpress-woocommerce-use-update-post-meta-to-add-product-attributes

  • Hi Zhi V, please provide your solution, keeping the references. – bummi Dec 1 '17 at 16:45
  • 1
    Ok, I will a bit later. – Zhi V Dec 1 '17 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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