【问题标题】:How to dynamically change the price of a product in Woocommerce - WordPress?如何在 Woocommerce - WordPress 中动态更改产品的价格?
【发布时间】:2015-02-27 15:12:49
【问题描述】:

如果我的客户来自某个特定的地方,我需要更改商店中产品的价格,并享受 10% 的折扣,因此,我编写了以下代码:

add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
     $product = new WC_Product( get_the_ID() );
     $price = $product->price;
     echo $price * 0.9;
}

好的,它有效!但是在结帐时,价格是正常的,没有 10% 的折扣!

是否有一些挂钩可以更改结帐区域中的产品价格或某些不同的代码可以在两者中正确更改(在产品页面和结帐中)?

【问题讨论】:

    标签: php wordpress woocommerce hook


    【解决方案1】:

    Woocommerce 也是新功能。 你的问题看起来和这个很相似。 Adding Custom price with woocomerce product price in Cart & Checkout

    我认为您需要使用 woocommerce_cart_item_subtotal 挂钩直接更改购物车中的价格(不完全作为参数)

    我对代码做了一些修改(更改了价格公式)。我认为这可能会对您有所帮助。

        add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
        // Display the line total price
        add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
    
        function calculate_discounted_price( $price, $values ) {
            // You have all your data on $values;
            $price = $price*.09;
            return $price;
        }
    
        // wc_price => format the price with your own currency
        function display_discounted_price( $values, $item ) {
            return wc_price( $item[ 'line_total' ] );
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2019-03-29
      • 2017-04-06
      • 2022-07-01
      • 2015-02-25
      • 2018-10-13
      • 1970-01-01
      相关资源
      最近更新 更多