使用 woocommerce_product_get_price 钩子结帐价格问题
Checkout price issue using woocommerce_product_get_price hook
我需要将 Woocommerce 前端中的每个产品的价格加倍。为此,我使用了以下代码:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
但使用此代码时出错。结帐页面价格不正确。
例如产品原价是10。我们通过这个代码将价格翻倍。所以现在产品价格是20。当我将此产品添加到购物车时,购物车和结帐页面的价格为 40。这意味着该乘法发生了两次。
请帮忙解决这个问题。
已更新要加倍价格:
1) 首先,您将代码限制为仅限单一产品和档案页面:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
if( is_shop() || is_product_category() || is_product_tag() || is_product() )
return $price*2;
return $price;
}
2) 然后对于购物车和结帐页面,您将以这种方式更改购物车商品价格:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 );
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
// Price calculation
$new_price = $cart_data['data']->get_price() * 2;
// Set and register the new calculated price
$cart_data['data']->set_price( $new_price );
$cart_data['new_price'] = $new_price;
return $cart_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
return $session_data;
// Get the new calculated price and update cart session item price
$session_data['data']->set_price( $session_data['new_price'] );
return $session_data;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并正常工作。它将按照您在购物车、结帐和订单页面中的预期更改所有价格……
我需要将 Woocommerce 前端中的每个产品的价格加倍。为此,我使用了以下代码:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
但使用此代码时出错。结帐页面价格不正确。 例如产品原价是10。我们通过这个代码将价格翻倍。所以现在产品价格是20。当我将此产品添加到购物车时,购物车和结帐页面的价格为 40。这意味着该乘法发生了两次。
请帮忙解决这个问题。
已更新要加倍价格:
1) 首先,您将代码限制为仅限单一产品和档案页面:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
if( is_shop() || is_product_category() || is_product_tag() || is_product() )
return $price*2;
return $price;
}
2) 然后对于购物车和结帐页面,您将以这种方式更改购物车商品价格:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 );
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
// Price calculation
$new_price = $cart_data['data']->get_price() * 2;
// Set and register the new calculated price
$cart_data['data']->set_price( $new_price );
$cart_data['new_price'] = $new_price;
return $cart_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
return $session_data;
// Get the new calculated price and update cart session item price
$session_data['data']->set_price( $session_data['new_price'] );
return $session_data;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并正常工作。它将按照您在购物车、结帐和订单页面中的预期更改所有价格……