Php 在Woocommerce中基于自定义用户的自动应用优惠券
第一个帖子在这里,所以要温柔。我已经做了我通常的搜索和测试,但我被难住了 无论如何,这里是我的代码,我正试图使用。我需要应用优惠券代码,如果用户属于其个人资料中的某个购买群体,则所有订单都可以享受2%的折扣 优惠券目前也对一个类别进行了排除,所以我不知道在自动应用代码时这是如何影响的 它只是遵循优惠券的限制吗Php 在Woocommerce中基于自定义用户的自动应用优惠券,php,wordpress,woocommerce,cart,coupon,Php,Wordpress,Woocommerce,Cart,Coupon,第一个帖子在这里,所以要温柔。我已经做了我通常的搜索和测试,但我被难住了 无论如何,这里是我的代码,我正试图使用。我需要应用优惠券代码,如果用户属于其个人资料中的某个购买群体,则所有订单都可以享受2%的折扣 优惠券目前也对一个类别进行了排除,所以我不知道在自动应用代码时这是如何影响的 它只是遵循优惠券的限制吗 add_action( 'woocommerce_before_cart', 'anla_apply_buying_group_discount_coupon' ); function
add_action( 'woocommerce_before_cart', 'anla_apply_buying_group_discount_coupon' );
function anla_apply_buying_group_discount_coupon() {
global $woocommerce;
global $current_user;
$user_id = get_current_user_id();
$maybe_has_buying_group_discount = get_user_meta( $user_id, 'has_buying_group_discount', true );
if ( '1' === $maybe_has_buying_group_discount ) {
return true;
}
elseif ( '0' === $maybe_has_buying_group_discount ) {
return false;
}
if ($maybe_has_buying_group_discount == true ) {
$woocommerce->cart->add_discount( 'buying group discount (2%)' );
}
elseif ($maybe_has_buying_group_discount == false ) {
$woocommerce->cart->remove_discount( 'buying group discount (2%)' );
$woocommerce->cart->calculate_totals();
}
}
非常感谢您的帮助。您的代码中有一些错误和不推荐的方法。你用的钩子不对。请尝试以下附加通知消息:
add_action( 'woocommerce_before_calculate_totals', 'apply_group_discount_coupon', 20, 1 );
function apply_group_discount_coupon( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set your coupon code
$coupon_name = "buying group discount (2%)";
$user_id = get_current_user_id();
$discount_enabled = get_user_meta( $user_id, 'has_buying_group_discount', true );
$coupon_code = sanitize_title( $coupon_name );
if ( $discount_enabled && ! $cart->has_discount( $coupon_code ) ){
$cart->apply_coupon( $coupon_code );
$message = __("You have 2% of discount as Premium user group", 'woocommerce');
} elseif ( ! $discount_enabled && $cart->has_discount( $coupon_code ) ) {
$cart->remove_coupon( $coupon_code );
$message = __("You can't get a discount as Premium user group", 'woocommerce');
} else
return;
// Display a custom notice
if( isset($message) ){
wc_clear_notices();
wc_add_notice( $message, 'notice');
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作
没有消息的版本:
add_action( 'woocommerce_before_calculate_totals', 'apply_group_discount_coupon', 20, 1 );
function apply_group_discount_coupon( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your coupon code
$coupon_name = "buying group discount (2%)";
$user_id = get_current_user_id();
$discount_enabled = get_user_meta( $user_id, 'has_buying_group_discount', true );
$coupon_code = sanitize_title( $coupon_name );
if ( $discount_enabled && ! $cart->has_discount( $coupon_code ) )
$cart->apply_coupon( $coupon_code );
elseif ( ! $discount_enabled && $cart->has_discount( $coupon_code ) ) {
$cart->remove_coupon( $coupon_code );
else
return;
}
“团购折扣(2%)”这是优惠券代码的名称,对吗?您可以尝试将其包装到
清理文本\u字段()
中吗。看起来那些括号/百分比符号可能会把它搞混。哇,这真是太棒了。它工作得很好,除了,也许我应该提到这一点-有没有一种方法不显示任何消息?我只想在cart totals行中提到这一点。再次感谢。你不知道,但我已经从这个网站上使用了很多你的代码。:)有趣。由于某种原因,我仍然收到消息说优惠券已成功应用,或者如果是排除在外的产品,则说明它未成功应用。我使用的是没有消息的你的版本。有什么想法吗?谢谢。@MichaelJ如果你收到通知说优惠券未成功申请,这是另一个问题,不是我的代码…这与优惠券的设置方式有关…这在两个代码上都是相同的…第一个代码只是清除通知以添加自己的代码…但两个代码的工作方式完全相同,区别只是第一个清除现有通知以显示自己的通知。