Code giảm giá theo giá trị đơn hàng (WOO)

Nguyen Eric
0
07/12/24
23/05/2025
//Code thông báo điều kiện và giảm giá trên tổng đơn hàng
function custom_message() {
    // Initialize
    $message = __( 'Default message', 'woocommerce' );
    
    // True
    if ( WC()->cart ) {        
        // Get cart subtotal
        $subtotal = WC()->cart->get_subtotal();
        if ( $subtotal < 79000 ) {
            $message = sprintf( __( 'Đặt thêm <c>%s</c> để được giao hàng (Tối thiểu 79k)', 'woocommerce' ), wc_price( 79000 - $subtotal ) );
        } 
		elseif ( $subtotal >= 79000 && $subtotal < 99000 ) {
            $message = sprintf( __( '<div class="sces">Đơn sẵn sàn giao hàng</div>Thêm %s được giảm 5.000đ', 'woocommerce' ), wc_price( 99000 - $subtotal ) );
        }
		elseif ( $subtotal >= 99000 && $subtotal < 149000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm ngay (-5K)</div>Thêm %s giảm tiếp 5.000đ', 'woocommerce' ), wc_price( 149000 - $subtotal ) );
        }
		elseif ( $subtotal >= 149000 && $subtotal < 199000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-10K)</div>Thêm %s giảm tiếp 5.000đ', 'woocommerce' ), wc_price( 199000 - $subtotal ) );
        }
		elseif ( $subtotal >= 199000 && $subtotal < 299000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-10K)</div>Thêm %s trợ ship 10.000đ', 'woocommerce' ), wc_price( 299000 - $subtotal ) );
        }
		elseif ( $subtotal >= 299000 && $subtotal < 499000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-20K) & Trợ ship 10k</div>Thêm %s giảm tiếp 10.000đ', 'woocommerce' ), wc_price( 499000 - $subtotal ) );
        }
		elseif ( $subtotal >= 499000 && $subtotal < 699000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-20K) & Trợ ship 20k</div>Thêm %s trợ ship tiếp 10.000đ', 'woocommerce' ), wc_price( 699000 - $subtotal ) );
        }
		elseif ( $subtotal >= 699000 && $subtotal < 799000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-20K) & Trợ ship 30k</div>Thêm %s trợ ship tiếp 10.000đ', 'woocommerce' ), wc_price( 799000 - $subtotal ) );
        }
		elseif ( $subtotal >= 799000 && $subtotal <999000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-35K) & Trợ ship 30k</div>Thêm %s được giảm thêm 15.000đ', 'woocommerce' ), wc_price( 999000 - $subtotal ) );
        }
		elseif ( $subtotal > 999000 && $subtotal < 1499000 ) {
            $message = sprintf( __( '<div class="sces">Bạn được giảm (-50K) & Trợ ship 30k</div>Đơn từ %s được giảm 10% ', 'woocommerce' ), wc_price( 1499000 - $subtotal ) );
        } 
    }  
    return $message;
}

// Refreshing on cart ajax events
function filter_woocommerce_add_to_cart_fragments( $fragments ) {    
    $fragments['div.dsp_msge'] = '<div class="dsp_msge"><div class="dsp_msge-txt">' . custom_message() . '</div></div>';
    
    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1 );

// Shortcode
function display_my_message() {
    return '<div class="dsp_msge">' . custom_message() . '</div>';
} 
// Register shortcode
add_shortcode( 'display_message', 'display_my_message' );
//Code giảm giá tự động bằng COUPON
//Lưu ý: Cần tạo mã coupon và chỉ định mã coupon được áp dụng
add_action( 'woocommerce_before_calculate_totals' , 'discount_based_on_cart_items_subtotal' );
function discount_based_on_cart_items_subtotal( $cart ) { 
    if ( is_admin() && !defined('DOING_AJAX') )
        return;
    
    $subtotal = 0; // Initializing

    // Loop through cart items to get the subtotal excluding taxes
    foreach ( $cart->get_cart() as $item ) {
        $subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
    }
    
    if ( $subtotal >= 149000 && $subtotal < 248000 ) { 
        if ( ! $cart->has_discount('DON149') ) {
            $cart->apply_coupon( 'DON149' );
			$notice = __('GIẢM 10k CHO ĐƠN HÀNG '.wc_price(149000).'');

        }
        if ( $cart->has_discount('DON249') ) {
            $cart->remove_coupon( 'DON249' );
        }
		if ( $cart->has_discount('DON499') ) {
            $cart->remove_coupon( 'DON499' );
        }
		if ( $cart->has_discount('DON899') ) {
            $cart->remove_coupon( 'DON899' );
        }
    } elseif ( $subtotal >= 249000 && $subtotal < 498000 ) {
        if ( ! $cart->has_discount('DON249') ) {
            $cart->apply_coupon( 'DON249' );
		   $notice = __('GIẢM 20k CHO ĐƠN HÀNG '.wc_price(249000).'');

        }
        if ( $cart->has_discount('DON149') ) {
            $cart->remove_coupon( 'DON149' );
        }
		if ( $cart->has_discount('DON499') ) {
            $cart->remove_coupon( 'DON499' );
        }
		if ( $cart->has_discount('DON899') ) {
            $cart->remove_coupon( 'DON899' );
        }
	} elseif ( $subtotal >= 499000 && $subtotal < 898000 ) {
        if ( ! $cart->has_discount('DON499') ) {
            $cart->apply_coupon( 'DON499' );
			$notice = __('GIẢM 30k CHO ĐƠN HÀNG '.wc_price(499000).'');

        }
        if ( $cart->has_discount('DON149') ) {
            $cart->remove_coupon( 'DON149' );
        }
		if ( $cart->has_discount('DON249') ) {
            $cart->remove_coupon( 'DON249' );
        }
		if ( $cart->has_discount('DON899') ) {
            $cart->remove_coupon( 'DON899' );
        }
	} elseif ( $subtotal >= 899000 ) {
        if ( ! $cart->has_discount('DON899') ) {
            $cart->apply_coupon( 'DON899' );
			$notice = __('GIẢM 50k CHO ĐƠN HÀNG '.wc_price(899000).'');

        }
		if ( $cart->has_discount('DON149') ) {
            $cart->remove_coupon( 'DON149' );
        }
        if ( $cart->has_discount('DON249') ) {
            $cart->remove_coupon( 'DON249' );
        }
		if ( $cart->has_discount('DON499') ) {
            $cart->remove_coupon( 'DON499' );
        }
    } else {
		if ( $cart->has_discount('DON899') ) {
            $cart->remove_coupon( 'DON899' );
        }
        if ( $cart->has_discount('DON499') ) {
            $cart->remove_coupon( 'DON499' );
        }
        if ( $cart->has_discount('DON249') ) {
            $cart->remove_coupon( 'DON249' );
        }
			if ( $cart->has_discount('DON149') ) {
            $cart->remove_coupon( 'DON149' );
        }
     
        wc_clear_notices();
    }
    
    if ( isset($notice) ) {
        wc_clear_notices();
        wc_add_notice( $notice, 'notice' );
    }
}
//Code giảm giá (BẰNG PHÍ) theo giá trị đơn hàng
add_action( 'woocommerce_cart_calculate_fees','cart_price_progressive_discount' );
function cart_price_progressive_discount() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $has_discount = false;
    $stotal_ext = WC()->cart->subtotal_ex_tax;

    // Discount percent based on cart amount conditions
    if( $stotal_ext >= 99000 && $stotal_ext < 149000  ) {
		$cost = 5000;
        $percent = -0.02;
        $percent_text = ' 2%';
        $has_discount =true;
    } elseif( $stotal_ext >= 149000 && $stotal_ext < 499000  ) {
	$cost = 10000;
        $percent = -0.1;
        $percent_text = 'Giảm 10%';
        $has_discount =true;
	}
	elseif( $stotal_ext >= 499000 && $stotal_ext < 799000  ) {
	$cost = 20000;
        $percent = -0.1;
        $percent_text = 'Giảm 10%';
        $has_discount =true;
	}
	elseif( $stotal_ext >= 799000 && $stotal_ext < 999000  ) {
	$cost = 35000;
        $percent = -0.1;
        $percent_text = 'Giảm 10%';
        $has_discount =true;
	}
    // Calculation (Chỉ chọn 1 trong 3 cách)
    $discount = $cost * $percent; //Áp dụng giảm giá % theo giá trị được chỉ định sẵn
    //$discount = $stotal_ext * $percent;//ÁP dụng giảm giá % theo giá trị đơn hàng
    //$discount = $stotal_ext - $cost;//ÁP dụng giảm giá cố định theo giá trị đơn hàng
    
    // Displayed text
    $discount_text = __('Discount', 'woocommerce') . $percent_text;

    if( $has_discount ) {
        WC()->cart->add_fee( $discount_text, $discount, false );
    }
    // Last argument in add fee method enable tax on calculation if "true"
}
//Code giảm giá(tối đa) theo giá trị đơn hàng chỉ định
//Ví dự giảm 10% tối đa 100k cho áp dụng đơn từ 999k
add_action( 'woocommerce_cart_calculate_fees', 'custom_limited_discount', 10, 1 );
function custom_limited_discount($cart_object) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
	
	$stotal_ext = WC()->cart->subtotal_ex_tax;

    // Here 20 % of discount
    $discount_percent = 0.1;

    // Here the max discounted amount
    $max_discount = 200000;

    // Here are some different cart totals
    $cart_subtotal_excl_tax = WC()->cart->subtotal_ex_tax;
    $cart_subtotal = WC()->cart->subtotal;
    $cart_total = WC()->cart->total;

    $discount = 0;

    // CALCULATION with subtotal excluding taxes
    $calculation = $cart_subtotal_excl_tax * $discount_percent;

    // Limiting the discount to $max_discount
     if( $stotal_ext >= 1500000  ) {
    if ( $calculation > $max_discount ) {
        $discount -= $max_discount;
    } else {
        $discount -= $calculation;
    }

    $discount_text_output = __( 'Discount (10 %)', 'woocommerce' );

    // Adding the discount
    $cart_object->add_fee( $discount_text_output, $discount, false );
    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}
}
//Code giảm giá(tối đa) theo giá trị đơn hàng
//Ví dự giảm 10% tối đa 100k
add_action( 'woocommerce_cart_calculate_fees', 'custom_limited_discount', 10, 1 );
function custom_limited_discount($cart_object) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Here 20 % of discount
    $discount_percent = 0.1;

    // Here the max discounted amount
    $max_discount = 200000;

    // Here are some different cart totals
    $cart_subtotal_excl_tax = WC()->cart->subtotal_ex_tax;
    $cart_subtotal = WC()->cart->subtotal;
    $cart_total = WC()->cart->total;

    $discount = 0;

    // CALCULATION with subtotal excluding taxes
    $calculation = $cart_subtotal_excl_tax * $discount_percent;

    // Limiting the discount to $max_discount
     
    if ( $calculation > $max_discount ) {
        $discount -= $max_discount;
    } else {
        $discount -= $calculation;
    }

    $discount_text_output = __( 'Discount (10 %)', 'woocommerce' );

    // Adding the discount
    $cart_object->add_fee( $discount_text_output, $discount, false );
    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}

//Giảm giá theo giá trị đơn hàng (Áp dụng cho thành viên chỉ định)
//Chỉ chọn (1) "dùng cho 1 loại TV" hoặc (2) "dành cho nhiều loại thành viên"
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart ) {
    //(1) HERE we target other user roles than 'distributor_prices' (allowing guests)
    if ( current_user_can('distributor_prices') && is_user_logged_in() )
        return;
    //(2) HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
    if ( ! array_intersect( wp_get_current_user()->roles, array('wholesale_prices', 'wholesale_vat_exc') && is_user_logged_in() )
        return;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart->get_cart_contents_total(); // Cart total

    if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent > 0 ) {
        $discount   = $cart_total * $percent / 100;
        $label_text = sprintf( __("Bulk Order Discount %s"), '('.$percent.'%)' );
        $cart->add_fee( $label_text, -$discount, true );
    }
}
Tags :
Tags: