Okay
  Public Ticket #3624870
Coupon
Closed

Comments

  •  1
    Jim started the conversation

    We want our agents to have the option to give out free samples.  Our thought was a coupon for 100% however when we do this the code is created and it is unrestricted - meaning bots crawling the site can pickup this code and anyone can use it. 

    How do we restrict this to B2B customers? Below we have this option but it doesn't fill out automatically for the agents and the agents do not have access to this area so a manager will have to know an agent put a code in and go to restrict it.  


    Its also not a dropdown so its confusing about what to put in this section. 

    How do you recommend giving out free samples, via code or altering the prices on a cart (that is tedious) 



    2740261565.png



  •  1,885
    WebWizards replied

    Hi Jim,

    In that section you can enter comma-separated options. That area should contain which user options are allowed to use the coupon.

    For example you can enter it as follows if you'd like only B2B users to be able to use that:

    1005276056.png

    Another example, this would mean that the coupon can only be used by logged out, and logged in b2c users:

    2207109571.png



    To make this apply for agent coupons, you can do it by adding the following PHP code snippet to the site:

    add_filter( 'woocommerce_coupon_is_valid', function($is_valid, $coupon, $discount){
        $coupon_id = wc_get_coupon_id_by_code($coupon->get_code());
        $agent_id = get_post_meta($coupon_id, 'salesking_agent', true);
        if (!empty($agent_id)){
            if (intval($agent_id) !== 1){
                // if agent, only allow b2b custoemrs
                if (get_user_meta(get_current_user_id(),'b2bking_b2buser', true) !== 'yes'){
                    $is_valid = false;
                }
            }
        }
        return $is_valid;
        
    }, 10, 3);
    

    This will automatically restrict all agent coupons to only be used by B2B users (Without having to configure this in the backend). If a non-B2B user tries to add an agent coupon, they will get an error.

    If you'd like, you can make that code only apply to 100% coupons. For that, use this code snippet instead: https://pastecode.io/s/kwsiojri


    Kind regards,

    Stefan

  •  1
  •  1
    Jim replied

    Is there a way to allow the coupon of 100% to bypass the minimum rule?  We have a min set to $125 order but the coupon puts it at zero and we cannot checkout.

  •  1,885
    WebWizards replied

    I believe you can achieve that as follows:

    Add the following PHP code snippet to your site:

    add_filter('b2bking_minmax_order_total_calculation_basis', function($val){
        return WC()->cart->get_subtotal();
    }, 10, 1);
    

    It should make it so that the minimum amount is calculated before the coupon.


    That snippet can be added to functions.php, or by following our guide here: https://woocommerce-b2b-plugin.com/docs/how-to-add-a-snippet-php-or-js/


    Let me know if it can solve it for you,

  •  1
    Jim replied

    This didn't work.

  •  1,885
    WebWizards replied

    The issue may have to do with tax. You can try this code instead to also account for that:

    add_filter('b2bking_minmax_order_total_calculation_basis', function($val){
        return WC()->cart->get_subtotal() + WC()->cart->get_subtotal_tax();
    }, 10, 1);
    


    Other than that, the code seems to work for me in tests.


    Example:

    - I have a rule that sets a $5000 min order for the cart.

    - Without the code snippet, I add the coupon for 100% discount, and the minimum order is not met:

    4078602652.png

    - With the code snippet, then the error no longer shows, and the user can proceed with the cart:

    2767929846.png



    If the above cannot work for you, would you be able to share a backend login to the site or a staging site, so we can look into this directly and troubleshoot?


    Kind regards,

    Stefan