Repository Forums Support WooCommerce Extended Coupon Features PRO Restriction AND product Reply To: Restriction AND product

#3966
Soft79
Keymaster

The following snippet restores the ‘Cart Percent Discount’ which unfortunately was removed from WooCommerce in version 3.0.


add_filter( 'woocommerce_cart_coupon_types', function ( $coupon_types ) {
  if ( ! in_array( 'percent_cart', $coupon_types ) ) $coupon_types[] = 'percent_cart';
  return $coupon_types;
}, 10, 1 );

add_filter( 'woocommerce_coupon_discount_types', function ( $coupon_discount_types ) {
  $coupon_discount_types['percent_cart'] = __( 'Percentage cart discount', 'your-language-domain' );
  $coupon_discount_types['percent'] = __( 'Percentage product discount', 'your-language-domain' );
  return $coupon_discount_types;
}, 10, 1 );

add_filter( 'woocommerce_coupon_get_discount_amount', function( $discount_amount, $discounting_amount, $cart_item, $single, $coupon ) {
  if ( $coupon->is_type( 'percent_cart' ) ) {
    $discount = (float) $coupon->get_amount() * ( $discounting_amount / 100 );
    $discount_amount = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
  }
  return $discount_amount;
}, 10, 5 );