I am attempting to grab specific products from one site and pass them to another website using WooCommerce Rest API. I have the Rest API working and it grabs the desired products, however I’m getting stuck at the response. Here are the steps I’m working with:
- Create a cURL request to grab the products from the Parent Site
- Use the response from the cURL request to loop through each product
- Inside of the loop update the fields returned from the response.
Here is my PHP code:
function test_api() {
$url = "https://www.example.com/wp-json/wc/v3/products";
$consumer_key = 'XXX';
$consumer_secret = 'XXX';
$params = array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'tag' => 5303,
'_fields' => 'id,name,regular_price,sku,permalink,short_description,categories,stock_status,images,type,variations,status',
);
$endpoint = $url.'?'.implode('&', array_map(
function($v, $k) {
return sprintf("%s=%s", $k, $v);
}, $params, array_keys($params)
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
$result = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
//Continue the functions
parse_api_products(json_decode($result, true));
wp_send_json_success(json_decode($result, true)); //Using this as ajax for testing for now
}
function parse_api_products($response) {
$product_list = print_r($response, true);
$date_updated = date('m-d-Y g:ia');
//Log the response
$logger = wc_get_logger();
$context = array('source' => 'api-product-sync');
$logger->info('API Product Sync: ' . $product_list, $context);
$logger->warning($product_list, array('source' => 'test'));
foreach ($product_list as $item) {
$sku = $item->sku;
$product_id = wc_get_product_id_by_sku( $sku );
if($product_id) {
$product = wc_get_product($product_id);
wp_update_post([
'ID' => $product_id,
'post_title' => $item->name
]);
update_post_meta( $product_id, '_regular_price', $item->stock_status);
update_post_meta( $product_id, '_stock_status', $item->stock_status);
$product->set_short_description($item->short_description);
}
}
}
The error code I end up with is:
[proxy_fcgi:error [pid 1752176:tid 1752345 [client 69.92.178.86:0 AH01071: Got error 'PHP message: PHP Warning: foreach() argument must be of type array|object, string given in /home/13622.cloudwaysapps.com/wjzzcseneb/public_html/wp-content/themes/bricks-child/functions.php on line 602', referer: https://woocommerce-1362296-50153.cloudwaysapps.com/test-page/
I also added this data to the WC Logger and get the following result:
2024-12-12T20:19:18+00:00 Warning Array
(
[0] => Array
(
[id] => 205881
[name] => AA Test Product
[regular_price] => 19
[sku] => skutestaa01
[permalink] => https://www.example.com/?post_type=product&p=205881
[short_description] =>
[categories] => Array
(
[0] => Array
(
[id] => 817
[name] => Phone Cases
[slug] => phone-cases
)
[1] => Array
(
[id] => 870
[name] => Necklaces
[slug] => necklaces
)
)
[images] => Array
(
[0] => Array
(
[id] => 206076
[date_created] => 2024-12-04T06:19:06
[date_created_gmt] => 2024-12-04T20:19:06
[date_modified] => 2024-12-04T07:09:43
[date_modified_gmt] => 2024-12-04T21:09:43
[src] => https://cdn.example.com/2024/12/Winter.jpg
[name] => Winter
[alt] =>
)
[1] => Array
(
[id] => 206075
[date_created] => 2024-12-04T06:19:00
[date_created_gmt] => 2024-12-04T20:19:00
[date_modified] => 2024-12-04T07:28:37
[date_modified_gmt] => 2024-12-04T21:28:37
[src] => https://cdn.example.com/2024/12/New-Brands.jpg
[name] => New Brands
[alt] =>
)
)
[type] => simple
[variations] => Array
(
)
[status] => draft
[stock_status] => instock
)
[1] => Array
(
[id] => 206510
[name] => AA Test Product #2
[regular_price] =>
[sku] => aa-sku-test-0
[permalink] => https://www.example.com/?post_type=product&p=206510
[short_description] => <p>This is a test!</p>
[categories] => Array
(
[0] => Array
(
[id] => 2038
[name] => Top Sellers
[slug] => top-sellers
)
)
[images] => Array
(
[0] => Array
(
[id] => 206488
[date_created] => 2024-12-12T01:06:46
[date_created_gmt] => 2024-12-12T15:06:46
[date_modified] => 2024-12-12T01:06:46
[date_modified_gmt] => 2024-12-12T15:06:46
[src] => https://cdn.example.com/2021/05/ST.jpg
[name] => ST
[alt] =>
)
)
[type] => variable
[variations] => Array
(
[0] => 206511
[1] => 206512
)
[status] => draft
[stock_status] => instock
)
)
The current $product_list variable matches a PHP array but for some reason is returning as a string causing the error. I’ve tried using the JSON response from the initial cURL response but it also throws various errors as it returns as a string (even when I attempt to decode it). What can I do to get this variable to read as an array so I can process the updates in the foreach loop?
6