Tuesday, 20 April 2021

Add custom discount on subtotal in woocommerce by hook woocommerce_cart_calculate_fees

 





add_action( 'woocommerce_cart_calculate_fees', 'elex_discount_price' );

function elex_discount_price() 

    global $woocommerce; 

    $discount_price = 0; 

    foreach (WC()->cart->get_cart() as $cart_item_key => $values) {


        if(isset($values["custom_data"]) && $values["custom_data"] == "Free Product" && $values["quantity"] == 1){

            $discount_price = $values["line_total"];

        }

    }

    $woocommerce->cart->add_fee( 'Discounted Price', -$discount_price, true, 'standard' ); 


No comments:

Post a Comment