gpt4 book ai didi

php - 如果 "sale_price"大于 "price",则更新所有 WooCommerce 产品

转载 作者:搜寻专家 更新时间:2023-10-31 21:02:40 25 4
gpt4 key购买 nike

我正在尝试更新所有产品销售价格的 wp_postmeta 表。

由于这两个字段都是 meta_key/meta_value 对,所以我正在努力解决这个问题在此wp_postmeta 表中。

我如何编写一个查询来更新所有 '_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 文件中

此代码已经过测试且功能齐全


引用资料:

关于php - 如果 "sale_price"大于 "price",则更新所有 WooCommerce 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38934249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com