Maximum Coupon Size Discount on the percentage of purchases in WooCommerce

I have a coupon code (XYZ25) in woocommerce that includes a 25% discount and the maximum discount is 250 rupees.

How can I restrict a user from getting more than a Rs.250 discount if they use the XYZ25 coupon code for a 25% discount.

+4
source share
3 answers

Starting with Woocommerce 3.2 or 3.3, this code no longer works

  1. You can set an additional coupon codeFIX250 based on a fixed discount on the cart **RS.250 (without tax) and at minimal cost(4 x 250) = RS.1000 .

  2. , XYZ25 Rs.1000, XYZ25 FIX250 ...

:

add_action( 'woocommerce_calculate_totals', 'coupon_discount_max_switch', 10, 1);
function coupon_discount_max_switch( $cart_obj ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your 2 coupons slugs  <===  <===  <===  <===  <===  <===  <===  <===  <===
    $coupon_25_percent = 'xyz25';
    $coupon_25_fixed = 'fix250';

    // Set HERE the limit amount  <===  <===  <===  <===  <===  <===  <===  <===  <===  <===
    $limit = 250; // Without VAT

    $total_discount = $cart_obj->get_cart_discount_total(); // Total cart discount

    // When 'xyz25' is set and the total discount is reached
    if( $cart_obj->has_discount( $coupon_25_percent ) && $limit_icl_vat <= $total_discount ){
        // Remove the 'xyz25' coupon
        $cart_obj->remove_coupon( $coupon_25_percent );
        // Checking that the fixed dicount is not already set.
        if( ! $cart_obj->has_discount( $coupon_25_fixed ) ){
            // Add the 'fix250' coupon
            $cart_obj->add_discount( $coupon_25_fixed );

            // Displaying a custom message
            $message = __( "The cart discount limit of Rs.$limit is reached", "woocommerce" );
            wc_add_notice( $message, 'notice' );
        }
    } 
}

function.php ( ), .

2.6.x 3. WooCommerce. 0+.

+3

@LoicTheAztec. , .

:

add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {

    $max_discount = 250; // coupon limit
    $coupon_code = 'XYZ25'; // coupon to check.

    if ( ( $coupon->get_code() == $coupon_code ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) {

        $cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
        if ( wc_prices_include_tax() ) {
            $discount_percent = ( wc_get_price_including_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal;
        } else {
            $discount_percent = ( wc_get_price_excluding_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal_ex_tax;
        }
        $_discount = ( $max_discount * $discount_percent ) / $cart_item_qty;

        $discount = min( $_discount, $discount );
    }

    return $discount;
}

, , "Fixed cart discount"; $max_discount . , , .

, min( A, B ). A - , B - .

(250, 100) = 100
(250, 150) = 150 - (250, 250) = 250 - (250, 300) = 250 - (250, 600) = 250

, .
, .

UPDATE , .

add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {

    $fake_code = 'fake_code_abcdefghigklmnopqrstuvwxyz'; // used to ignore this filter
    if ( $coupon->get_code() == $fake_code ) return $discount;

    $max_discount = 250; // coupon limit
    $coupon_code = 'XYZ25'; // coupon to check.

    if ( $coupon->get_code() == $coupon_code )  {

        $_coupon = new WC_Coupon( ); // lets create a fake coupon to test our $max_discount.
        $_coupon->set_props( array(
            'discount_type' => 'fix_cart',
            'amount'        => $max_discount,
            'code'          => $fake_code
        ) );

        $_discount = $_coupon->get_discount_amount( $discounting_amount, $cart_item, $single );

        $discount = min( $_discount, $discount );

    }

    return $discount;
}
+2

functions.php

add_action('woocommerce_coupon_options_usage_limit', 'woocommerce_coupon_options_usage_limit', 10, 2);
function woocommerce_coupon_options_usage_limit($coupon_id, $coupon)
{

    echo '<div class="options_group">';
    // max discount per coupons
    $max_discount = get_post_meta($coupon_id, '_max_discount', true);
    woocommerce_wp_text_input(array(
        'id'                => 'max_discount',
        'label'             => __('Usage max discount', 'woocommerce'),
        'placeholder'       => esc_attr__('Unlimited discount', 'woocommerce'),
        'description'       => __('The maximum discount this coupon can give.', 'woocommerce'),
        'type'              => 'number',
        'desc_tip'          => true,
        'class'             => 'short',
        'custom_attributes' => array(
            'step' => 1,
            'min'  => 0,
        ),
        'value'             => $max_discount ? $max_discount : '',
    ));
    echo '</div>';

}

add_action('woocommerce_coupon_options_save', 'woocommerce_coupon_options_save', 10, 2);
function woocommerce_coupon_options_save($coupon_id, $coupon)
{

    update_post_meta($coupon_id, '_max_discount', wc_format_decimal($_POST['max_discount']));

}

add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5);
function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon)
{

    $max_discount = get_post_meta($coupon->get_id(), '_max_discount', true);

    if (isset($max_discount) && is_numeric($max_discount) && ($max_discount > 0) && !is_null($cart_item) && WC()->cart->subtotal_ex_tax)
    {
        if($discount > $max_discount)
        {
            $discount = $max_discount;
        }
    }

    return $discount;
}

- " β†’ ". , .

Link: http://reigelgallarde.me/programming/woocommerce-set-maximum-coupon-discount/

-2
source

Source: https://habr.com/ru/post/1676820/


All Articles