0

I am trying to create a WooCommerce extension so that I can fetch products through API and store them in my WordPress store. I have been successful in storing simple products. But i'm stuck at creating variable products or any other type.

I haven't created the API yet as need to be sure of the format it needs to be in, but can be easily achieved in format as described in this link

Here is my working code for simple products :

if (in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )) { 
function create_wc_product_by_api(){
    $api_response = wp_remote_get('www.myapi.com');
    $body = wp_remote_retrieve_body($api_response);
    
    /****** $data will be replaced by my API response data *********/
    $data = [
        'name' => 'Premium Quality',
        'type' => 'simple',
        'regular_price' => '21.99',
        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
        'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
        'categories' => [
            [
                'id' => 9
            ],
            [
                'id' => 14
            ]
        ],
        'images' => [
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg',
                'position' => 0
            ],
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg',
                'position' => 1
            ]
        ]
    ];

    $wp_rest_request = new WP_REST_Request( 'POST' );
    $wp_rest_request->set_body_params( $data );

    $products_controller = new WC_REST_Products_Controller;

    $wp_rest_response = $products_controller->create_item( $wp_rest_request );

}

}

UPDATE

I have looked into default WooCommerce Code and found out the class WC_REST_Product_Variations_Controller. Looks like this will solve my problem. Will update again if this works.

1 Answer 1

1

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;
        }
    }

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.