我有三种订阅产品,每年,半年和每月。默认情况下,我的商店页面上每年每6个月和每月显示价格。
基于“Change product prices via a hook in WooCommerce 3”答案代码,我试图显示每月的所有价格,因此要根据订阅期限更改价格显示:
// Utility function to change the prices with a multiplier (number)
function get_price_multiplier($var_product) {
switch($var_product) {
case 111:
// Annual
return 12;
break;
case 222:
// Semiannual
return 6;
break;
case 333:
// Month
return 1;
break;
default:
return 1;
break;
}
}
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
$var_product = $product->get_id();
return $price / get_price_multiplier($var_product);
}
这有效但是当产品被添加到购物车时,价格不是正常价格,而是来自上述功能的修改价格。
基于“Set programmatically product sale price and cart item prices in Woocommerce 3”答案代码,我已经能够解决这个问题:
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
$regular_price = $cart_item['data']->get_regular_price();
$variation_id = $cart_item['variation_id'];
$cart_item['data']->set_price( $regular_price * get_price_multiplier($variation_id) );
}
}
这重置了购物车的价格,它似乎工作,虽然其中一个价格是一分钱。
这一切都是错综复杂的,容易出现问题,还是实现我追求的目标的一种可靠方式:在商店页面上显示每个订阅期限的价格?
你是以正确的方式做到的......有两种方式(最后一种是最好的方式):
注意:当乘数等于1时,不需要计算和价格变化。
1)第一种选择:改进的代码版本与您的代码版本非常相似。
这是我评论的代码:
// Utility function that increase conditionally the variation price with a multiplier (int)
function get_variation_calculated_price( $variation_id, $price, $multiplier = true ) {
switch( $variation_id ) {
case 111: // Annual
$rate = 12;
break;
case 222: // Semi-annual
$rate = 6;
break;
default: // Month (and others)
$rate = 1;
break;
}
// Return calculated price (or false when multiplier is 1, as calculation is not needed)
return $rate !== 1 ? ( $multiplier ? $price * $rate : $price / $rate ) : false;
}
// Change variations calculated prices
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $variation ) {
if( $new_price = get_variation_calculated_price( $variation->get_id(), $price, false ) )
return $new_price;
return $price;
}
// Customizing cart item prices
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
// Only for variations
if( $cart_item['variation_id'] > 0 ) {
if( $new_price = get_variation_calculated_price( $cart_item['variation_id'], $cart_item['data']->get_price() ) ) {
$cart_item['data']->set_price( $new_price );
}
}
}
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
2)第二种选择:更准确,更轻,限制产品价格,避免购物车,结账和后端的价格变化。
注意:您需要避免在购物车页面中显示交叉销售产品(因为价格不会像商店页面那样更改)。
因此代码将更轻,更紧凑和高效:
// Utility function that increase conditionally the variation price with a multiplier (int)
function get_variation_calculated_price( $variation_id, $price ) {
switch( $variation_id ) {
case 111: // Annual
$rate = 12;
break;
case 939: // Semi-annual
$rate = 6;
break;
default: // Month (and others)
$rate = 1;
break;
}
// Return calculated price (or false when multiplier is 1, as calculation is not needed)
return $rate !== 1 ? $price / $rate : false;
}
// Change variations calculated prices
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $variation ) {
// Not in cart, checkout and admin
if( is_cart() || is_checkout() || is_admin() )
return $price;
if( $new_price = get_variation_calculated_price( $variation->get_id(), $price ) )
return $new_price;
return $price;
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。