【问题标题】:Auto apply coupon based on custom user_meta in Woocommerce基于 Woocommerce 中的自定义 user_meta 自动应用优惠券
【发布时间】:2018-09-07 20:23:23
【问题描述】:

第一次在这里发帖,所以要温柔。我已经完成了通常的搜索和测试,但我很难过。

无论如何,这是我正在尝试使用的代码。如果用户属于其个人资料中的某个购买组,我需要应用优惠券代码,该优惠券代码可享受 2% 的折扣

优惠券目前还排除了一个类别,所以我不知道在自动应用代码时这些因素是如何影响的。

它只是遵循优惠券的限制吗?

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();
    }
}

感谢任何帮助。

【问题讨论】:

  • 'buying group discount (2%)' 那是优惠券代码的名称对吗?您可以尝试将其包装在sanitize_text_field() 中。似乎那些括号/百分比符号可能会混淆它。

标签: php wordpress woocommerce cart coupon


【解决方案1】:

您的代码中有一些错误和不推荐使用的方法。您使用的钩子不是正确的。而是尝试以下附加通知消息:

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;
}

【讨论】:

  • 哇,这太棒了 LoicTheAztec。它工作得很好,除了,也许我应该提到这一点 - 有没有办法根本不显示任何消息?我只想在购物车总计行中提及这一点。再次感谢。你不知道,但我已经从这个站点使用了很多你的代码。 :)
  • 有趣。由于某种原因,我仍然收到消息说优惠券已成功应用,或者如果是排除的产品,则不是。我正在使用您的版本,没有消息。有什么想法吗?谢谢。
  • @MichaelJ 如果您收到优惠券未成功应用的通知是另一个问题,而不是我的代码...这与您的优惠券设置方式有关...这在两者上都是一样的代码...第一个只是清除通知以添加它自己的...但是两个代码的工作方式完全相同,区别只是第一个清除现有通知以显示它自己的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 2021-06-16
  • 1970-01-01
相关资源
最近更新 更多