我修改了一个函数,为我的某些成员创建自定义价格,即正常价格为$ 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
现在,购物车中的自定义价格也将被反映