如果 "sale_price" 大于 "price",则更新所有 WooCommerce 产品
Update all WooCommerce products if "sale_price" is greater than "price"
我正在尝试更新所有产品促销价的 wp_postmeta table。
由于这两个字段都是 meta_key
/ meta_value
[=28=,所以我正在努力解决这个问题] 对 wp_postmeta table.
我如何编写一个查询来更新所有 '_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();
你也可以把代码函数嵌入到hook中…
此代码位于您的活动子主题或主题的 function.php 文件中
此代码已经过测试且功能齐全
参考文献:
我正在尝试更新所有产品促销价的 wp_postmeta table。
由于这两个字段都是 meta_key
/ meta_value
[=28=,所以我正在努力解决这个问题] 对 wp_postmeta table.
我如何编写一个查询来更新所有 '_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();
你也可以把代码函数嵌入到hook中…
此代码位于您的活动子主题或主题的 function.php 文件中
此代码已经过测试且功能齐全
参考文献: