0

I am trying to create a woo commerce product using Rest API Provided i have a Consumer Key: and Consumer Secret: With read and Write Authentication am not sure where am going wrong the PHP is not populating any error message nor the Product Was Created.

<?php


    require_once 'class-wc-api-client.php';





function addProduct()
{      

    $options = array(
        'debug'           => false,
        'return_as_array' => false,
        'validate_url'    => false,
        'timeout'         => 30,
        'ssl_verify'      => false,
    );

    try {

    $client = new WC_API_Client(  'http://example.com/demo/workpressistalledlocation/','xxxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxx',$options );



        $client->products->create(  array(  'sku' => '100001',  'title' => 'Superduper product',   'type' => 'simple',  'regular_price' => '21.50', 'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',  'short_description' => 'Short description for product'));

print_r( $completed_orders );
    } catch ( WC_API_Client_Exception $e ) {

        echo $e->getMessage() . PHP_EOL;
        echo $e->getCode() . PHP_EOL;

        if ( $e instanceof WC_API_Client_HTTP_Exception ) {

            print_r( $e->get_request() );
            print_r( $e->get_response() );
        }
    }



}  
1
  • $completed_orders this variable is not initailized.. Commented Apr 21, 2016 at 6:18

1 Answer 1

1

You could try this if you haven't found answer yet.

Use only if you are using woo-commerce rest api v2/v3 version

            $product = array(
            'title' => 'title_of_product',
            'sku' => 'unique_sku',
            'regular_price' => 'price',
            'type' => 'simple/variable/grouped/external',
            'description' => $this->tftimport_description,
            'managing_stock' => $manage_wc_stock,
            'status' => 'draft',
            'enable_html_description' => true,
        );

        $wc_product = $this->get_wc_api_client()->products->create($product);

You could also check for more help on rest api from woocommerce offical api documenation linke below :

https://woothemes.github.io/woocommerce-rest-api-docs/?php#create-a-product

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.