1

I want to Alter function validate_coupon_minimum_amount() adding a condition that if a user use coupon "refresh18", check to see if subtotal in cart for a product category is greater than the coupon's minimum amount.

How could I do that?

Here is my code below that doesn't work:

function check_minimum_parts_amount_in_cart($coupon) {

    $cart_parts_subtotal = 0;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
      $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

    $terms = get_the_terms( $product_id, 'product_cat' );
    if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) && has_term( 'ice-shaving-parts', 'product_cat', $product_id ) ) {
        //echo '<pre>' . print_r($cart_item , 1) . '</pre>';
        //echo $cart_item['quantity'];
        $cart_parts_subtotal = $cart_parts_subtotal + ( $_product->get_price() * $cart_item['quantity'] );
     }
    }

    $subtotal = wc_remove_number_precision( $this->get_object_subtotal() );
    if( $coupon->get_name() == 'refresh2018' ){
      if( $coupon->get_minimum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_minimum_amount', $coupon->get_minimum_amount() > $cart_parts_subtotal, $coupon, $cart_parts_subtotal )){
        /* translators: %s: coupon minimum amount */
        throw new Exception( sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_minimum_amount() ) ), 108 );
      }else{
        return true;
      }
    }else{
      if ( $coupon->get_minimum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_minimum_amount', $coupon->get_minimum_amount() > $subtotal, $coupon, $subtotal )) {
        /* translators: %s: coupon minimum amount */
        throw new Exception( sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_minimum_amount() ) ), 108 );
      }else{
        return true;
      }
    }  
}
add_filter('validate_coupon_minimum_amount', 'check_minimum_parts_amount_in_cart');
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
M.Tian
  • 35
  • 6

1 Answers1

4

Updated:

Since Woocommerce 3.2+ use instead woocommerce_coupon_validate_minimum_amount filter hook… So you should try this revisited code (Where you will have to set your product category):

add_filter( 'woocommerce_coupon_validate_minimum_amount', 'custom_check_minimum_amount_in_cart', 10, 3 );
function custom_check_minimum_amount_in_cart( $valid, $coupon, $subtotal ) {
    // HERE below your settings (Coupon code and product category)
    $coupon_code = 'refresh18';
    $product_category = 'clothing'; // <== To be replaced by your targeted product category

    $cat_subtotal = 0;

    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
            $cat_subtotal += $cart_item['line_subtotal'];
    }

    if( $coupon->get_code() == $coupon_code && $coupon->get_minimum_amount() > $cat_subtotal ){
        throw new Exception( sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_minimum_amount() ) ), 108 );
    }
    return $valid;
}

This code goes on function.php file of your active child theme (or theme). It should work.

Note: For everybody, remember to set in this function the coupon code in lowercase.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you so much for your reply. The `$coupon->get_name()` throwing this error "Call to undefined method WC_Coupon::get_name()" so I changed it to get_code. But it still not working with `$coupon->get_minimum_amount() > $cat_subtotal`. When I have total more than minimum but not in category "clothing", the coupon still work. Any ideas? – M.Tian Mar 12 '18 at 22:21