[英]Update all WooCommerce products if “sale_price” is greater than “price”
我正在嘗試更新所有產品銷售價格的wp_postmeta表。
我正在努力解決這個問題,因為這個字段在這個wp_postmeta表中都是meta_key
/ meta_value
對。
如何編寫將更新所有'_sale_price' = 0 WHERE '_sale_price' > '_price'
?
使用自定義函數的不同方法應該執行相同的工作。 您只需使用此功能一次 ,然后在作業完成后將其刪除(首次加載站點時,該過程將取決於您擁有的產品數量)。
這是代碼:
function update_products_sale_price(){
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish'
);
// getting all products
$products = get_posts( $args );
// Going through all products
foreach ( $products as $key => $value ) {
// the product ID
$product_id = $value->ID;
// Getting the product sale price
$sale_price = get_post_meta($product_id, '_sale_price', true);
// if product sale price is not defined we give to the variable a 0 value
if (empty($sale_price))
$sale_price = 0;
// Getting the product sale price
$price = get_post_meta($product_id, '_price', true);
// udate sale_price to 0 if sale price is bigger than price
if ($sale_price > $price)
update_post_meta($product_id, '_sale_price', '0');
}
}
// Here the function we will do the job.
update_products_sale_price();
您還可以將代碼函數嵌入到鈎子中...
此代碼位於活動子主題或主題的function.php文件中
此代碼經過測試且功能齊全
參考文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.