如果客户已登录,Woocommerce会在简单产品上提供全球百分比折扣 [英] Woocommerce global percentage discount on simple products if customer is logged in
本文介绍了如果客户已登录,Woocommerce会在简单产品上提供全球百分比折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找有关以下功能出了什么问题的建议.
I'm looking for advice on what's wrong with the following function.
在此示例中,我的目标是在用户登录后为所有WooCommerce简单产品提供50%的折扣.
My goal in this example is to apply a 50% off discount to all WooCommerce simple products, as long as the user is logged in.
function tier_pricing_logic() {
if ( is_user_logged_in() ) {
function assign_tier_pricing( $price, $product ) {
$price = $price * 0.5; // Set all prices for simple products to 50% off.
}
return $price;
add_filter('woocommerce_product_get_price', 'assign_tier_pricing', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'assign_tier_pricing', 90, 2 );
}
}
add_action( 'init', 'tier_pricing_logic' );
此功能对价格没有影响,我是否完全错误?
This function has no effect on the prices, am I approaching this all wrong?
推荐答案
在这里,您不需要init
钩子,并且IF语句需要在钩子函数内,因此请尝试这样做(对于简单产品):
Here you don't need the init
hook and your IF statement needs to be inside the hooked function, so try that instead (for simple products):
add_filter('woocommerce_product_get_price', 'assign_tier_pricing', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'assign_tier_pricing', 90, 2 );
function assign_tier_pricing( $price, $product ) {
if ( is_user_logged_in() && $product->is_type('simple') ) {
$price *= 0.5; // Set all prices for simple products to 50% off.
}
return $price;
}
这篇关于如果客户已登录,Woocommerce会在简单产品上提供全球百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文