Add Products to Woocommerce through WP API

After a lot of searching and banging my head everywhere I have successfully implemented to add WooCommerce products through Rest API. Here is my complete working code for reference :-

    $api_response = wp_remote_get('www.myapi.com');
    $body = wp_remote_retrieve_body($api_response);

    /****** API not ready yet...working on it  *******/
    $data = [
        'type' => 'variable',
        'description' => 'Trying it out for real',
        'short_description' => 'Pellentesque habitant.',
        'categories' => [
            [
                'id' => 37
            ],
            [
                'id' => 38
            ]
        ],
        'images' => [
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg',
                'position' => 0
            ]
        ],
        'attributes' => [
            [
                'name' => 'Color',
                'position' => 0,
                'visible' => true,
                'variation' => true,
                'options' => [
                    'Black',
                    'Green'
                ]
            ],
            [
                'name' => 'Size',
                'position' => 0,
                'visible' => true,
                'variation' => true,
                'options' => [
                    'S',
                    'M'
                ]
            ]
        ],
        'default_attributes' => [
            [
                'name' => 'Color',
                'option' => 'Black'
            ],
            [
                'name' => 'Size',
                'option' => 'S'
            ]
        ],
        'variations' => [ 
            [
                'regular_price' => '29.98', 
                'attributes' => [ 
                    [
                        'slug'=>'color',
                        'name'=>'Color',
                        'option'=>'Black'
                    ]
                ]   
            ],
            [
                'regular_price' => '69.98',
                'attributes' => [
                    [
                        'slug'=>'color', 
                        'name'=>'Color', 
                        'option'=>'Green'
                    ]
                ]
            ] 
        ]
    ];

    $wp_rest_request = new WP_REST_Request( 'POST' );
    $wp_rest_request->set_body_params( $data );
    $products_controller = new WC_REST_Products_Controller;
    $res = $products_controller->create_item( $wp_rest_request );
    $res = $res->data;


    // The created product must have variations
    // If it doesn't, it's the new WC3+ API which forces us to build those manually
    if ( !isset( $res['variations'] ) ){
        $res['variations'] = array();
    }
    if ( count( $res['variations'] ) == 0 && count( $data['variations'] ) > 0 ) {
        if ( ! isset( $variations_controler ) ) {
            $variations_controler = new WC_REST_Product_Variations_Controller();
        }
        foreach ( $data['variations'] as $variation ) {

            $wp_rest_request = new WP_REST_Request( 'POST' );
            $variation_rest = array(
                'product_id' => $res['id'],
                'regular_price' => $variation['regular_price'],
                'attributes' => $variation['attributes'],
            );
            $wp_rest_request->set_body_params( $variation_rest );
            $new_variation = $variations_controler->create_item( $wp_rest_request );
            $res['variations'][] = $new_variation->data;
        }
    }

Leave a Comment