【问题标题】:Woocommerce regular price not getting updatedWoocommerce 正常价格未更新
【发布时间】: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


    【解决方案1】:

    您应该在第三种情况下删除此empty($selling_price) &&,因为根据您的第二种情况,$selling_price 不是空的,并且正如您所说的第一种和第二种情况工作正常。所以在这种情况下,除非$selling_price 为空,否则您的第三个条件将不成立。试试下面的代码。

    if( !empty( $discount_percentage ) ){   //not working
        $selling_price = $mrp * ( 100 - $discount_percentage ) / 100;
        update_post_meta( $post_id, '_price', $selling_price );
    }
    

    【讨论】:

    • 感谢您的回复!我没有使用 _price,而是将其更改为 _sale_price,它按预期工作。我正在尝试实时呈现值(当我在销售价格字段中键入时,折扣字段应自动更新)。目前,它会在帖子更新或保存时更新。我不知道如何实现。
    • 欢迎...很高兴为您提供帮助。如果你喜欢/想要你也可以upvote回答,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-03-19
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多