在Woocommerce迷你购物车/购物车中设置自定义计算商品价格 [英] Set a custom calculated item price in Woocommerce mini-cart / Cart
问题描述
当前,我根据不同的情况对产品价格进行一些自定义计算.当客户将产品添加到购物车中时,会在会话数据, cart_item_data ['my-price']
中设置自定义价格,并且我使用 add_filter('woocommerce_add_cart_item')
来实现功能和一切似乎现在都在工作.
Currently i have some custom calculation of product price based on different situation. When customer added a product in to cart then the custom price is set in session data , cart_item_data['my-price']
and i implemented using add_filter( 'woocommerce_add_cart_item')
function and everything seems to working now .
现在查看购物车页面中的价格,结帐页面与我的 cart_item_data ['my-price']正确.
Now the price in view cart page, checkout page is correct with my cart_item_data['my-price'].
但是我面临的唯一问题是菜单中出现的woocommerce迷你购物车中的价格未更新,该如何更改?
当我在Google上看到过滤器
When i google i see a filter
add_filter('woocommerce_cart_item_price');
但是我不明白如何使用它,我会执行以下操作
but i can't understand how to use this i do the following
add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);
function modify_cart_product_price( $price, $cart_item, $cart_item_key){
if($cart_item['my-price']!==0){
$price =$cart_item['my-price'];
}
return $price;
//exit;
}
此处单个价格正确
,但总价格不正确
推荐答案
更新 (2021年10月)
为了成功进行此测试(并且我不知道您如何进行计算),我在产品添加到购物车表单中添加了以下自定义隐藏字段:
For testing this successfully (and as I don't know how you make calculations), I have added a custom hidden field in product add to cart form with the following:
// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
global $product;
// The fake calculated price
?>
<input type="hidden" id="my-price" name="my-price" value="115">
<?php
}
将产品添加到购物车时,此 my-price
自定义字段也会被提交(发布).要在购物车对象中设置此值,请使用以下功能:
When product is added to cart, this my-price
custom field is also submitted (posted). To set this value in cart object I use the following function:
add_filter( 'woocommerce_add_cart_item', 'custom_cart_item_prices', 20, 2 );
function custom_cart_item_prices( $cart_item_data, $cart_item_key ) {
// Get and set your price calculation
if( isset( $_POST['my-price'] ) ){
$cart_item_data['my-price'] = $_POST['my-price'];
// Every add to cart action is set as a unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
现在将新的计算出的价格 my-price
应用于(设置)购物车项目,我使用最后一个函数:
Now to apply (set) the new calculated price my-price
to the cart item, I use this last function:
// For mini cart *(cart item displayed price)*
add_action( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2 );
function filter_cart_item_price( $price, $cart_item ) {
if ( ! is_checkout() && isset($cart_item['my-price']) ) {
$args = array( 'price' => floatval( $cart_item['my-price'] ) );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price;
}
add_action( 'woocommerce_before_calculate_totals', 'set_calculated_cart_item_price', 20, 1 );
function set_calculated_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( isset( $cart_item['my-price'] ) && ! empty( $cart_item['my-price'] ) || $cart_item['my-price'] != 0 ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( $cart_item['my-price'] );
}
}
}
所有代码都放入您的活动子主题(或活动主题)的function.php文件中.
经测试可正常工作
这篇关于在Woocommerce迷你购物车/购物车中设置自定义计算商品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!