我有三种订阅产品,每年,半年和每月。默认情况下,价格在我的商店页面上按每年,每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) );
}
}
这重置了购物车的价格,尽管其中一个价格降低了一分钱,但它似乎可以正常工作。
这是否令人费解并且容易出问题,或者是实现我所追求的可靠方法:在商店页面上显示每个订阅条款的价格?
答案 0 :(得分:1)
您做对的方式正确……有2种方法(最后一种是最好的方法):
注意:当乘数等于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文件中。经过测试,可以正常工作。