Apply Discount by Cart Amount

If you want to apply a discount by cart amount, this article tells how to do this and gives a snippet. Shortly, if the cart total is bigger than your target value, the cart total will be discounted.

Copy & paste the below code to your function.php:

// Woofocus - Discount snippet starts

add_action( 'woocommerce_cart_calculate_fees', 'wf_apply_discount_by_cart_amount' );

function wf_apply_discount_by_cart_amount() {
    $wf_min_amount = get_option('wf_min_amount');
    $wf_min_amount_discount = get_option('wf_min_amount_discount');

    if(WC()->cart->subtotal >  $wf_min_amount){
        WC()->cart->add_fee( 'Fee', -$wf_min_amount_discount );
    }
}
add_action( 'customize_register', 'wf_customizer_register' );


function wf_customizer_register( $wp_customize ) {

    // Create our panels

    $wp_customize->add_panel( 'wfdiscountpanel', array(
        'title'          => __('WooFocus discount Panel', 'wf'),
    ) );

    // Create our sections

    $wp_customize->add_section( 'wf_discount_by_amount' , array(
        'title'             => __('Discount by amount', 'wf'),
        'priority'          => 1,
        'panel'             => 'wfdiscountpanel',
    ) );

    // Create our settings

    $wp_customize->add_setting( 'wf_min_amount' , array(
        'type'          => 'option',
        'transport'     => 'refresh',
    ) );

    $wp_customize->add_control( 'wf_min_amount_control', array(
        'label'      => __('Minimum Amount for Discount', 'wf'),
        'section'    => 'wf_discount_by_amount',
        'settings'   => 'wf_min_amount',
        'priority'   => 1,
        'type'       => 'text',
    ) );

    $wp_customize->add_setting( 'wf_min_amount_discount' , array(
        'type'          => 'option',
        'transport'     => 'refresh',
    ) );

    $wp_customize->add_control( 'wf_min_amount_discount_control', array(
        'label'      => __('Discount Fee', 'wf'),
        'section'    => 'wf_discount_by_amount',
        'settings'   => 'wf_min_amount_discount',
        'priority'   => 1,
        'type'       => 'text',
    ) );

}

// Woofocus - Discount snippet ends

After paste it, WooFocus Discount Panel will be added to the customizer menu as you can see on the picture.

Click “Discount by amount” buton

You can set your discount rule here.For example,

the subtotal on the image is 762.00, so it is bigger than 20 (Minumum Amount for Discount). It means this cart totals will be affected by discount.

762.00 – 400 ( Discount Fee ) equals the new cart total.

Don’t forget to click on the Save button to apply the discount by cart amount. Otherwise, you lose your changes.

For more WooCommerce Tutorials & Tricks, you can follow WooFocus.com.

Leave a Comment