我是REST API的新手,
我有一个php页面,我需要在其中填充所有产品和变体。所以我正在使用curl
https://my_ip/index.php/wp-json/wc/v2/products
然后对它进行IM循环并传递产品ID以获取变化,因此在内部使用https://my_ip/index.php/wp-json/wc/v2/products/$variable_product_id/variations/
进行即时通信,但是这会花费更多的时间,因为每种产品都有更多变化,因此以任何方式在单个请求中获得所有产品和变体?
like:可以在class-wc-rest-products-controller.php
或任何查询中或woocommerce中进行任何自定义吗?这样我就可以得到结果。
mycode:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my_ip/index.php/wp-json/wc/v2/products",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSL_VERIFYPEER=> false,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic *****************",
"Cache-Control: no-cache"
),
));
$response = curl_exec( $curl) ;
curl_close( $curl );
$Product = json_decode( $response );
//after getting all product. looping all to get variations price.
foreach ( $Product as $row ){
$product_id= $row->id;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my_ip/index.php/wp-json/wc/v2/products/".$product_id."/variations/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSL_VERIFYPEER=> false,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic **************",
"Cache-Control: no-cache"
),
));
$response1 = curl_exec( $curl );
curl_close( $curl );
}
$variations=json_decode( $response1 );
print_r( $variations->price );
// here getting variation of particular product
因此,当我们调用https://my-ip/index.php/wp-json/wc/v2/products/423
时,我们得到所有产品,只有variations id
属于该产品
"variations": [
603,
604,
605,
606
],
但是我需要的是每个变体的详细信息(价格)
{
"id": 603,
"date_created": "2018-06-18T12:45:58",
"date_created_gmt": "2018-06-18T12:45:58",
"date_modified": "2018-06-29T06:41:21",
"date_modified_gmt": "2018-06-29T06:41:21",
"description": "",
"sku": "",
"price": "550",
"regular_price": "550",
}
注意:我正在/ var / www / html /中创建一个单独的test.php页面。此页面不在任何wp-content内
答案 0 :(得分:2)
我使用他们phpLib
while (count($woocommerce->get('products',array('per_page' => 100, 'page' => $page))) > 0) {
$all_products = array_merge($all_products,$woocommerce->get('products',array('per_page' => 100, 'page' => $page)));
$page++;
}
if ($source_product->type = "variable") {
$variation = $woocommerce->get('products/'.$source_product->id.'/variations');
foreach ($variation as $source_child) {
//do stuff
}
}
答案 1 :(得分:0)
每次循环都重写值-response1
如果要保存所有值,则需要将此值添加到数组中 例如:
<?php
$array = array();
foreach ($Product as $row){
$product_id= $row->id;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my_ip/index.php/wp-json/wc/v2/products/" . $product_id . "/variations/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSL_VERIFYPEER=> false,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic **************",
"Cache-Control: no-cache"
),
));
$array[] = json_decode( curl_exec( $curl ) );
curl_close( $curl );
}
print_r( $array );