同时根据产品自定义字段更改WooCommerce Minicart项目价格 [英] Also change WooCommerce Minicart item price based on product custom field
本文介绍了同时根据产品自定义字段更改WooCommerce Minicart项目价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Based on Allow customer to set the product price (giftcard) and add to cart if sum is minimum 100 in WooCommerce, which answers my initial question - I am left with one small problem regarding the WooCommerce minicart.
产品价格不会根据客户使用Giftcard字段提交的价格进行相应更新.因此,我有两个不同的解决方案,都失败了.
The product price is not updated accordingly to what the customer submits using the giftcard field. So I have two different solutions whereof both fail.
这是我尝试过的:
add_filter('woocommerce_widget_cart_item_quantity', 'custom_wc_widget_cart_item_quantity', 10, 3 );
function custom_wc_widget_cart_item_quantity( $cart, $cart_item, $cart_item_key ) {
foreach ( $cart->get_cart() as $cart_item ) {
if ( isset ( $cart_item['giftcard_product_price'] ) ) {
$cart_item['data']->set_price( $cart_item['giftcard_product_price'] );
return sprintf( '<span class="quantity">%s × <span class="woocommerce-Price-amount amount">%s <span class="woocommerce-Price-currencySymbol">%s</span></span></span>', $cart_item['quantity'], $cart_item['giftcard_product_price'] );
}
}
}
不起作用:Minicart变成空白.然后我也尝试了:
add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);
function modify_cart_product_price( $price, $cart_item, $cart_item_key){
$price = $cart_item['data']->set_price($cart_item['giftcard_product_price']);
return $price;
}
我能得到的任何帮助将不胜感激.
Any help I can get would be grateful.
推荐答案
要使用的正确方法是 woocommerce_cart_item_price
:
add_filter( 'woocommerce_cart_item_price', 'giftcard_cart_item_price', 10, 3 );
function giftcard_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
$giftcard_key = 'giftcard_product_price';
if( isset( $cart_item[$giftcard_key] ) ) {
$args = array( 'price' => floatval( $cart_item[$giftcard_key] ) );
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_html;
}
现在,自定义价格将在微型购物车中正确显示...
Now the custom price will appear correctly in minicart...
这篇关于同时根据产品自定义字段更改WooCommerce Minicart项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文