我使用add_fee方法来收取购物车价格。它工作正常,但是当我点击结账按钮并进入结账页面或刷新页面时,新价格消失,旧价格就在那里。如何在购物车中保存价格?
function woo_add_cart_fee($cart_obj) {
global $woocommerce;
$count = $_POST['qa'];
$extra_shipping_cost = 0;
//Loop through the cart to find out the extra costs
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
//Get the product info
$_product = $values['data'];
//Adding together the extra costs
$extra_shipping_cost = $extra_shipping_cost + $_product->price;
}
$extra_shipping_cost = $extra_shipping_cost * $count;
//Lets check if we actually have a fee, then add it
if ($extra_shipping_cost) {
$woocommerce->cart->add_fee( __('count', 'woocommerce'), $extra_shipping_cost );
}
}
add_action( 'woocommerce_before_calculate_totals', 'woo_add_cart_fee');
答案 0 :(得分:1)
您需要使用woocommerce_cart_calculate_fees
挂钩。我还重新访问了您的代码,因为您需要存储$_POST['qa'];
值以使费用持久:
add_action( 'woocommerce_cart_calculate_fees', 'cart_custom_fee', 20, 1 );
function cart_custom_fee( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$count = $_POST['qa'];
$extra_shipping_cost = 0;
// Loop through the cart items to find out the extra costs
foreach ( $cart_obj->get_cart() as $cart_item ) {
//Adding together the extra costs
$extra_shipping_cost = $extra_shipping_cost + $cart_item['data']->price;
}
$extra_shipping_cost = $extra_shipping_cost * $count;
//Lets check if we actually have a fee, then add it
if ( $extra_shipping_cost != 0 ) {
$cart_obj->add_fee( __('count', 'woocommerce'), $extra_shipping_cost );
}
}
代码放在活动子主题(或活动主题)的function.php文件中。
这应该按预期工作。