Php WooCommerce迷你购物车小部件的自定义价格
我有以下代码来生成自定义价格:Php WooCommerce迷你购物车小部件的自定义价格,php,wordpress,woocommerce,hook-woocommerce,Php,Wordpress,Woocommerce,Hook Woocommerce,我有以下代码来生成自定义价格: 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_
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迷你购物车小部件上,它不会显示正确的价格,但会计算正确的小计
我相信这段代码是作为模板存在的,所以我将文件从../wp-content/plugins/woocmerce/templates/cart/mini-cart.php复制到../wp-content/myteme/woocmerce/cart/mini-cart.php,但更改此文件没有任何作用。我已经删除了这个文件中的所有内容,它保持不变
感谢您的指导。产品价格在mini-cart.php第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中使用过滤器:
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;
}
谢谢它对我有用。。