【发布时间】:2021-07-24 22:58:23
【问题描述】:
我在我的 woocommerce 产品中添加了一个名为“Discount_Percentage”的 ACF 字段。当我将正常价格留空时,我想根据折扣百分比计算正常价格,或者当折扣百分比字段为空时计算折扣百分比。 以下是我的代码 sn-p:
function wc_update_discount_and_sale_price($post_id) {
$selling_price = get_field('_price');
$mrp = get_field('_regular_price');
$discount_percentage = get_field('discount_percentage');
if(empty($selling_price) && empty($discount_percentage)){
return;
}
if(!empty($selling_price) && empty($discount_percentage)){
$discount_value = (($mrp - $selling_price)*100)/($mrp);
update_field("discount_percentage",$discount_value,$post_id);
return;
}
if(empty($selling_price) && !empty($discount_percentage)){ //not working
$selling_price = $mrp*(100 - $discount_percentage)/100;
update_post_meta($post_id,'_price', $selling_price);
}
}
add_action('save_post','wc_update_discount_and_sale_price');
前两个 if 语句工作正常。在第三个 if 语句中,没有填充正常价格。
更新: 将 _price 更改为 _sale_price 有效。
【问题讨论】:
标签: wordpress advanced-custom-fields hook-woocommerce code-snippets