我对在WooCommerce中管理价格有疑问。
我只有一家经营简单产品的商店。假设对于所有订户和客户,每种产品的正常价格都打了10%的折扣。这很简单:
function custom_price( $price, $product ) {
global $post, $blog_id;
$post_id = $post->ID;
get_post_meta($post->ID, '_regular_price');
if ( is_user_logged_in() ) {
return $price = ($price * 0.9);
} else{
return $price;
}
}
add_filter( 'woocommerce_get_price', 'custom_price', 10, 2);
对于已经具有销售价格的产品,我希望woocommerce以正常价格计算登录用户的折扣,让客户看到销售价格和折扣价格之间的最低价格。因此:
场景1
场景2
Woocommerce,带有上面的代码段,而是以登录价计算已登录用户的10%折扣,并返回:
场景1
场景2
我该如何解决?谢谢您的帮助
答案 0 :(得分:1)
自WooCommerce 3以来,您的问题代码已过时和不建议使用…而是使用应符合您的情况的以下代码:
add_filter( 'woocommerce_product_get_price', 'custom_discount_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_discount_price', 10, 2 );
function custom_discount_price( $price, $product ) {
// For logged in users
if ( is_user_logged_in() ) {
$discount_rate = 0.9; // 10% of discount
// Product is on sale
if ( $product->is_on_sale() ) {
// return the smallest price value between on sale price and custom discounted price
return min( $price, ( $product->get_regular_price() * $discount_rate ) );
}
// Product is not on sale
else {
// Returns the custom discounted price
return $price * $discount_rate;
}
}
return $price;
}
代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。