pankaj
pankaj

Reputation: 74

How to get applied coupon amount in woocommerce?

I am trying to add a special discount when my cart subtotal is 0. This discount will be applied on cart total which include shipping charges.

function add_discount_line( $cart ) {
    global $woocommerce;

    if($woocommerce->cart->get_applied_coupons() && $cart->subtotal==0){

  $discount = 2; // here I want the amount of applied coupon
 $cart->add_fee( __( 'Special Discount', 'woocommerce' ) , -$discount );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'add_discount_line' );

$discount = 2; // How I can get the discount amount of applied coupon here.

Upvotes: 2

Views: 4474

Answers (1)

pankaj
pankaj

Reputation: 74

Solution I got for it.

add_action( 'woocommerce_cart_calculate_fees', 'add_discount_line' );
function add_discount_line( $cart ) {

    if($cart->get_applied_coupons() && $cart->subtotal==0):
    $getDetails = ( new WC_Coupon($cart->get_applied_coupons()));
    $discount  =  $getDetails->amount;

     $cart->add_fee( __( 'Special Discount', 'woocommerce' ) , -$discount );
    endif;
}

Upvotes: 3

Related Questions