[英]Conditional product prices cart issue in WooCommerce 3
我修改了一个函数,为一些会员创建自定义价格,即正常价格为1美元,但是如果您是青铜会员,则为2美元,白银会员为3美元,等等。
价格在商店和单个产品页面上已更改。 但是,将产品添加到购物车后,价格将恢复为原始金额。 我是否应该包含其他代码,以便在结帐和结算过程中始终准确更改价格?
// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
function custom_variation_price( $price, $variation, $product ) {
global $product;
$id = $product->get_id();
$user_id = get_current_user_id();
$plan_id = 1628;
if ( wc_memberships_is_user_member( $user_id, $plan_id ) ) {
$new = $price * 2;
return ($new);
}
}
使用您的代码,您只是在更改显示的变化价格范围。 因此,您将需要更多:
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );
// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );
function custom_price( $price, $product ) {
// Only logged in users
if ( ! is_user_logged_in() ) return $price;
// HERE the defined plan ID
$plan_id = 1628;
if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id ) ) {
$price *= 2; // set price x 2
}
return $price;
}
function custom_variation_price( $price, $variation, $product ) {
// Only logged in users
if ( ! is_user_logged_in() ) return $price;
// HERE the defined plan ID
$plan_id = 1628;
if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id ) ) {
$price *= 2; // set price x 2
}
return $price;
}
代码进入活动子主题(或活动主题)的function.php文件中。
经过测试并在woocommerce上工作3+
现在,购物车中的自定义价格也会反映出来
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.