2

First post here so be gentle. I have done my usual searching and testing but I am stumped.

Anyways here is my code that I am trying to use. I need to apply a coupon code that takes 2% off all orders if the user belongs to a certain buying group which is in their profile.

The coupon also does an exclude on one category at the moment so I don't know exactly how that factors in when automatically applying a code.

Does it just follow the restrictions for the coupon?

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

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Michael J
  • 23
  • 2
  • 'buying group discount (2%)' That's the name of the coupon code right? Can you try wrapping it in `sanitize_text_field()`. Seems like those brackets/percent symbol could confuse it. – Ben Kelly Mar 29 '18 at 01:55

1 Answers1

0

There are some errors and deprecated methods in your code. The hook that you are using is not the right one. Instead try the following with additional notice messages:

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

Code goes in function.php file of your active child theme (or active theme). Tested and works.


The version without message:

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
  • 229,944
  • 23
  • 356
  • 399
  • Wow, this is awesome LoicTheAztec. It is working great except, and maybe I should have mentioned this - **is there a way to not show any messages at all?** I only want mention of this in the cart totals row. Thanks again. You don't know it but I have used a lot of your code already from this site. :) – Michael J Mar 29 '18 at 17:04
  • Interesting. I am still getting messages for some reason that the coupon was applied successfully, or if an excluded product, that it wasn't. I am using your version without messages. Any thoughts? Thanks. – Michael J Mar 30 '18 at 00:10
  • @MichaelJ if you get a notice that the coupon is not applied successfully is another problem, not my code… This has to do with the way your coupon is set… And this will be the same on both codes… The first one simply clear notices to add it own ones… But both codes works exactly the same way, the difference is only that the first one clear the existing notices to display it's own one. – LoicTheAztec Mar 30 '18 at 00:20