Posted on

WooCommerce REST API tutorial for Auctions and Competitions

Introduction

Auctions for WooCommerce support REST api from v2.8 and Competitions for WooCommerce support it from v1.0. Here we will show couple of examples how you can use WooCommerce REST api with our plugins. First, we need to set up test / dev enviroment, we need REST client and key that allows us access to our WooCommerce. You need a REST client, any will do, for our tutorial we have small php client available for download here along with some PHP code examples. You can extract this in any folder you want on your website or on 3rd party website / domain.

Please note that in our example $woocommerce variable is Client object not global WooCommerce variable. We are not loading WordPress code at all since this is whole point of using REST api – none of the globals are available when using REST api client.

Generating key / secret for WooCommerce api access

Please go to WooCommerce Settings > Advanced tab.  Click on REST API and generate key / secret pair, you should get similar view as on screenshot below:

WooCommerce REST API tutorial for Auctions and Competitions 1

You can set permissions to read or write only or read / write. You need to copy paste these into request.php file under part where you instance rest client object, you can’t miss this since it’s commented and fairly easy to spot.

Generating REST api get request for retrieving product data

We need to define some parameters for request using $data variable which is in fact array with paramters like type,  name, id, etc. There is plenty of parameters which can be used here, same as when you create custom query, complete WooCommere rest api reference documentation is here. Here is code that gets 100 auction products in json format:

$data = [
    'per_page' => 100,
    'type' => 'auction' // get auction product type, 100 per page
    // 'type' => 'competition' // get competition product type
];

echo "<pre>";
print_r( $woocommerce->get( 'products', $data) );
echo "</pre>";

For competitions this is similar, we just need to change product type:

$data = [
    'per_page' => 100,
    'type' => 'competition'
];

To retrieve specific product using ID you don’t need $data variable:

echo "<pre>"; 
print_r( $woocommerce->get( 'products/12345') );
echo "</pre>";

That is just quick example of what is possible, complete reference is available in WooCommerce REST api documentation.

Generating REST api request for creating auction product

$data = [
    'name' => 'My Auction',
    'type' => 'auction',
    'status' => 'draft',          // status can be: draft, published, pending
    'regular_price' => '90.99',   // buy now price
    'price' => '90.99', 

    'meta_data' => array(
        [ 'key' => '_auction_start_price', 'value' => '7.99' ],             // starting bid
        [ 'key' => '_auction_bid_increment', 'value' => '1.5' ],            // min bid increment
        [ 'key' => '_auction_reserved_price', 'value' => '2000.0' ],        // reserve price
        [ 'key' => '_auction_dates_from', 'value' => '2023-01-01 00:00' ],  // start date
        [ 'key' => '_auction_dates_to', 'value' => '2039-02-27 00:00' ],    // end date
        [ 'key' => '_auction_proxy', 'value' => 'no' ],                     // proxy auction, yes | no   
        [ 'key' => '_auction_sealed', 'value' => 'no' ],                    // sealed auction, yes | no
        [ 'key' => '_auction_bid_increment_percentage', 'value' => 'yes' ], // bid increment in percentage?, yes | no     
        [ 'key' => '_auction_extend_enable ', 'value' => 'yes' ],           // e, yes | no  
        [ 'key' => '_auction_extend_in_time', 'value' => '60' ],            // extend auctions in last 60 seconds
        [ 'key' => '_auction_extend_for_time', 'value' => '120' ],          // extend auctions for 120 seconds

    ),

    'description' => 'This is my new auction added via WooCommerce API',
    'short_description' => 'Sample test auction added via rest api.',    
];


print_r( $woocommerce->post( 'products', $data ) );

Generating REST api request for modifying auction product

$data = [

	'regular_price' => '100.99', 
	'price' => '100.99',
	
];

echo "<pre>";
print_r( $woocommerce->put( 'products/278', $data ) );
echo "</pre>";

This is example how to modify buy now price for auction ID 278. You can modify any field regular or custom one, just add attributes to $data array. Example for $data is above, in section about creating new auction product.

When creating or modifying product response is always fresh / updated product data.

Generating REST api request for creating competition product

$data = [
    'name' => 'My Competition',
    'type' => 'competition',
    'status' => 'draft',          // status can be: draft, published, pending

    'meta_data' => array(
        [ 'key' => '_competition_price', 'value' => '7.99' ],                       // price
        [ 'key' => '_competition_sale_price', 'value' => '1.5' ],                   // sale price
        [ 'key' => '_competition_min_tickets', 'value' => '99' ],                   // min tickets
        [ 'key' => '_competition_max_tickets', 'value' => '100' ],                  // max tickets
        [ 'key' => '_competition_max_tickets_per_user', 'value' => '5' ],           // max tickets per user
        [ 'key' => '_competition_num_winners', 'value' => '3' ],                    // number of competition winners
        [ 'key' => '_competition_multiple_winner_per_user', 'value' => 'no' ],      // multiple prizes per user?, yes | no
        [ 'key' => '_competition_manualy_winners', 'value' => 'no' ],               // manually pick winning numbers, yes | no    
        [ 'key' => '_competition_dates_from', 'value' => '2023-01-01 00:00' ],      // start date  
        [ 'key' => '_competition_dates_to', 'value' => '2024-01-01 00:00' ],        // end date
        [ 'key' => '_competition_use_pick_numbers', 'value' => 'yes' ],             // allow ticket numbers?, yes | no
        [ 'key' => '_competition_pick_number_alphabet', 'value' => 'no' ],          // use alphabet?, yes | no
        [ 'key' => '_competition_pick_numbers_random', 'value' => 'no' ],           // randomly assign ticket numbers?, yes | no
        [ 'key' => '_competition_pick_number_use_tabs', 'value' => 'no' ],          // sort tickets in tabs?, yes | no
        [ 'key' => '_competition_pick_number_tab_qty', 'value' => 'no' ],           // number of tickets per tab, number
        [ 'key' => '_competition_use_answers', 'value' => 'no' ],                   // force user to answer a question?, yes | no
        [ 'key' => '_competition_only_true_answers', 'value' => 'no' ],             // only allow true answers?, yes | no
        
        // question / answers should be added manually!
       
    ),


    'description' => 'This is my new competition added via WooCommerce API',
    'short_description' => 'Sample test competition added via rest api.',    
];

print_r( $woocommerce->post( 'products', $data ) );

Adding competition is similar to adding auctions, only difference are custom meta fields related to competitions.

 

In case you have a question or need support please contact us.