PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label woocommerce-rest-api. Show all posts
Showing posts with label woocommerce-rest-api. Show all posts

Thursday, February 10, 2022

[FIXED] How to update woocommerce product via api with Symfony and Guzzle

 February 10, 2022     guzzle, php, symfony, woocommerce, woocommerce-rest-api     No comments   

Issue

Question: How to update the price of a woocommerce product via API using Guzzle and guzzle/oauth-subscriber

I've used This Question as my reference to get oauth1 working for requesting data, which works well. Just haven't been able to workout out to send post variables.

Most tutorials and the guzzle docs use the $client->request('GET', 'http://httpbin.org', [ 'query' => ['foo' => 'bar'] ]); but that isn't working either:

        $response = $client->request('POST', $endpoint, [
            'auth' => 'oauth',
            'form_params' => [
                'price' => '21'
            ]
        ]);

This is my current code, with the get $client->get() commented out what successfully returns a product.

        $endpoint = 'products/12';

        $handler = new \GuzzleHttp\Handler\CurlHandler();
        $stack = \GuzzleHttp\HandlerStack::create($handler);

        $middleware = new \GuzzleHttp\Subscriber\Oauth\Oauth1([
            'consumer_key'    => $this->consumer_key,
            'consumer_secret' => $this->consumer_secret,
            'token_secret'    => '',
            'token'           => '',
            'request_method' => Oauth1::REQUEST_METHOD_QUERY,
            'signature_method' => Oauth1::SIGNATURE_METHOD_HMAC
        ]);
        $stack->push($middleware);

        $client = new \GuzzleHttp\Client([
            'base_uri' => $this->url . $this->api,
            'handler' => $stack
        ]);

        $response = $client->post( $endpoint, [ 'auth' => 'oauth' ], ['price' => '21'] );

        var_dump($response);

        //$response = $client->get( $endpoint, [ 'auth' => 'oauth' ] );
        //return array(
        //  'status' => $response->getStatusCode(),
        //  'header' => $response->getHeaderLine('content-type'),
        //  'body' => json_decode($response->getBody())
        //);

Solution

There were three issues with my code:

  1. Using POST to update, not PUT. As stated POST is to create, not update.

  2. Reading Woocommerce docs I found that 'price' is read only, so no combination of parameters I was trying were going to work. regular_price is the correct parameter to use here.

  3. None of the options I was passing to $client-> were working, it needs to be a query string. Which I appended to the $endpoint variable.

$endpoint .= "?stock_quantity=456";
$response = $client->put( $endpoint, [ 'auth' => 'oauth' ] );


Answered By - Bradmage
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, February 7, 2022

[FIXED] Prevent WooCommerce from firing product update webhook on REST API update only

 February 07, 2022     hook-woocommerce, woocommerce, woocommerce-rest-api, wordpress     No comments   

Issue

I am trying to create a stock sync system that communicates with WooCommerce via REST API and webhooks. It works simply like that:

  1. If the product was updated - the webhook is fired to my API which creates the event with the current stock information.
  2. When event is created it is being processed and the product update information is sent via REST to other WooCommerce stores in my "network". The product is updated.

The issue here is that it creates some kind of infinite loop as when the product is updated on other website it fires webhook again and creates a new event in my API again.

Question: Is it possible to prevent WooCommerce from firing product update webhooks ONLY when product is updated via REST API (or specific function)?


Solution

Yes, there is an example using the filter woocommerce_webhook_should_deliver here: https://shanerutter.co.uk/fix-woocommerce-webhook-loop/

Also, I've had luck un-scheduling the Action Scheduler actions upon their creation, which is a consequence of a REST API inventory type sync job firing product updated hooks and web hooks:

add_action( 'init', function() {
    as_unschedule_all_actions( 'adjust_download_permissions' );
} );


Answered By - Sean Conklin
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, February 5, 2022

[FIXED] Update Order custom Field value with WooCommerce REST API

 February 05, 2022     json, php, woocommerce, woocommerce-rest-api, wordpress     No comments   

Issue

I am trying to update the Order custom field value lworder. I used the below code but I did not find any update. Can anyone please help me with this?

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v3/orders/{ORDER ID}', array(
    //'method'    => 'PUT',
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
    ),
    'meta_key' => array(
            'lworder' => 'ordered', 
    )
) );

$body = json_decode( $api_response['body'] );
//print_r( $body );

if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
    echo 'The Order field  ' . $body->name . ' has been updated';
}

Solution

Tested OK with WooCommerce 6.1.1

$api_response = wp_remote_post('https://mujuonly.github.io/index.php/wp-json/wc/v3/orders/1631817799', array(
        'method' => 'PUT',
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode('ck_b0f7480d348807e3c065053e7810fb83ce3b6b41:cs_0c44e9ad9173fd3698010e86f1140914dcb8fc8a')
        ),
        'body' => array('billing' => array(
                'city' => 'Disssssmmmmss',
            ),
            'meta_data' => array(0 => array(
                    'key' => 'lworder',
                    'value' => 'orderd'
                )
            )
        )
    ));

    $body = json_decode($api_response['body']);

    if (wp_remote_retrieve_response_message($api_response) === 'OK') {
        echo 'The Order field  ' . $body->name . ' has been updated';
    }


Answered By - mujuonly
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] Disable WooCommerce API authentication for Custom endpoint

 February 05, 2022     restful-authentication, woocommerce-rest-api, wordpress     No comments   

Issue

I have created a custom WooCommerce API endpoint (in a custom WP plugin) for that creates a new order in WooCommerce. I usually use HTTPS and basic auth with consumer key and consumer secret.

This customer API was designed to be accessed by another platform that does not have the ability to enter consumer key and secret in request header. So I would like to disable WooCommerce authentication for this plugin only. I will be authenticating using a field in the original request by comparing a key.

Does anyone know how to do this?


Solution

I found the solution:

// To disable authentication, hook into this filter at a later priority and return a valid WP_User

    add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ), 0 );


Answered By - Amjad
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing