if ( isset($sale_price) && ! empty($sale_price) ) {
$product->set_sale_price($sale_price);
$product->set_price($sale_price);
} else {
$product->set_price($regular_price);
}
$product->set_regular_price($regular_price);
$product->save();
echo $product_id."-".$regular_price."-".$sale_price."已处理!";
}
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
$variation_id = $variation['variation_id'];
$url="https://yoursite.com/wp-json/wc/v3/products/".$product_id."/variations/".$variation_id;
$consumer_key = 'KEY';
$consumer_secret = 'Secret';
$headers = array(
'Authorization' => 'Basic ' . base64_encode($consumer_key.':'.$consumer_secret )
);
$data = array(
'regular_price' => $regular_price,
'sale_price' => $sale_price
);
$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, 240);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
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);
}
echo $product_id."-".$regular_price."-".$sale_price."已处理";
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$args = [
'post_type' => 'product',
'posts_per_page' => 1,
'offset' => $offset,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'DESC',
];
$products = wc_get_products( $args );
$count = count($products);
每次执行结束后,跳到下一页。
$offset = $offset + 1;
header("Refresh: 3; URL=?offset=".$offset);
这样就能完美的更改数据库里面的价格和促销价。当然产品标题,产品分类,产品tag ,产品描述等等,都可以用这种方法去更改。
文章为作者独立观点,不代表DLZ123立场。如有侵权,请联系我们。( 版权为作者所有,如需转载,请联系作者 )

网站运营至今,离不开小伙伴们的支持。 为了给小伙伴们提供一个互相交流的平台和资源的对接,特地开通了独立站交流群。
群里有不少运营大神,不时会分享一些运营技巧,更有一些资源收藏爱好者不时分享一些优质的学习资料。
现在可以扫码进群,备注【加群】。 ( 群完全免费,不广告不卖课!)
发表评论 取消回复