skip to Main Content

It seems as if $data isn’t correct. I can list products and I get the whole product JSON as a response. But the product in WooCommerce doesn’t change the price. When I do the same thing via curl command line, the update is working.
Referring to that: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product

What am I doing wrong?

<?php

$url = "https://wooexample.com/wp-json/wc/v3/products/455";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/json",
   "Authorization: Basic " . base64_encode ( 'ck_33fbff7b90dcddba9bd4cbedaeda6b2fa3:cs_4156da18fc357b288e42a7e7b75fa6682b' ),
);
// print_r($headers);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = [
    'regular_price' => '24',
    'price' => '24'
];

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
print_r(json_decode($resp));

2

Answers


  1. The problem lies on the data format you are sending.

    the docs is using a library that’s supposedly transform the data already in a supported format, while your curl request is posting raw array data via curl

    you can try transforming your data into a form data with http_build_query

    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data) );

    or convert them into JSON format

    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data) );

    EDIT

    You also appear to have wrong curl request, you’re passing the keys on as base64_encoded authorization in headers, but based on the docs you’re supposed to do

    curl -X PUT https://example.com/wp-json/wc/v3/products/794 
        -u consumer_key:consumer_secret 
        -H "Content-Type: application/json" 
        -d '{
      "regular_price": "24.54"
    }'
    

    Your php curl request should have be something like

    $headers = [
        "Content-Type: application/json",
    ]
    .
    .
    curl_setopt($curl, CURLOPT_USERPWD, "your_key_here" . ":" . "your_secret_here");
    
    Login or Signup to reply.
  2. Ty this one, I have checked, it’s working perfectly for me.

    <?php
    
    $url = "https://localhost/wordpress/wp-json/wc/v3/products/455";
    
    $consumer_key = 'your consumer key put here..';
    $consumer_secret = 'your consumer secre put here..';
    
    $headers = array(
        'Authorization' => 'Basic ' . base64_encode($consumer_key.':'.$consumer_secret )
    );
    $data = array(
        'regular_price' => '24',
        'price' => '24'
    );
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    //for debug only!
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
    $resp = curl_exec($curl);
    $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    curl_close($curl);
    print_r(json_decode($resp));
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search