![](/img/trans.png)
[英]Set a custom calculated item price in Woocommerce mini-cart / Cart
[英]Display a custom calculated cart item price in WooCommerce
在 WooCommerce 中,我試圖計算購物車中可變產品的價格。 我想將產品價格與一些自定義購物車項目數值相乘。
這是我的代碼:
add_filter( 'woocommerce_cart_item_price', 'func_change_product_price_cart', 10, 3 );
function func_change_product_price_cart($price, $cart_item, $cart_item_key){
if (isset($cart_item['length'])){
$price = $cart_item['length']*(price of variation);
return $price;
}
}
價格計算不起作用。 我做錯了什么?
此掛鈎中的$price
參數是格式化的產品項目 price ,然后您需要原始價格才能使其適用於您的自定義計算。 請嘗試以下操作:
add_filter( 'woocommerce_cart_item_price', 'change_cart_item_price', 10, 3 );
function change_cart_item_price( $price, $cart_item, $cart_item_key ){
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'] );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'] );
}
if ( isset($cart_item['length']) ) {
$price = wc_price( $product_price * $cart_item['length'] );
}
return $price;
}
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.