WooCommerce迷你购物车小部件的自定义价格 [英] Custom Prices for WooCommerce Mini Cart Widget

查看:73
本文介绍了WooCommerce迷你购物车小部件的自定义价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来生成自定义价格:

I have the following code to generate custom prices:

add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
      $price = my_custom_calculate_func($value);
      $value['data']->set_price($price);
    }
}

它在购物车页面上很好用,但是在WooCommerce迷你购物车小部件上,它不会显示正确的价格,但是会计算出正确的小计.

It works great on the cart page, but on the WooCommerce mini cart widget, it doesn't display the correct price but does calculate the proper sub-total.

我相信此代码作为模板存在,因此我已将文件从../wp-content/plugins/woocommerce/templates/cart/mini-cart.php复制到../wp-content/mytheme/woocommerce/cart/mini-cart.php,但更改此文件不会执行任何操作.我已经删除了此文件中的所有内容,并且保持不变.

I believe the code for this exists as a template, so I have copied the file from ../wp-content/plugins/woocommerce/templates/cart/mini-cart.php to ../wp-content/mytheme/woocommerce/cart/mini-cart.php but changing this file does nothing. I have deleted everything in this file and it stays the same.

任何指导表示赞赏.

推荐答案

产品价格在mini-cart.php中的第39行计算:

Product price is calculated in mini-cart.php at line 39:

$product_price     = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );

您可以在主题文件夹中的mini-cart.php中编辑此行,或在functions.php中使用过滤器:

You can edit this line in mini-cart.php in in your theme folder, or use filter in functions.php:

add_filter( 'woocommerce_cart_item_price', 'woocommerce_cart_item_price_filter', 10, 3 );
function woocommerce_cart_item_price_filter( $price, $cart_item, $cart_item_key ) {
    // your code to calculate $new_price

    return $new_price;
}

这篇关于WooCommerce迷你购物车小部件的自定义价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆