add_action('woocommerce_before_calculate_totals','apply_coupon_if_eligible');functionapply_coupon_if_eligible($cart) {if (is_admin() && !defined('DOING_AJAX')) {return; }//Đảmbảochỉchạymộtlầnif (did_action('woocommerce_before_calculate_totals') >= 2) {return; }$coupon_code='COUPON_CODE'; //ThayCOUPON_CODEbằngmãgiảmgiácủabạn$minimum_amount=500000; //Sốtiềntốithiểuđểápdụngmãgiảmgiá (nếu có)//Kiểmtrađiềukiệnápdụngif ($cart->subtotal >= $minimum_amount) {//Kiểmtraxemmãđãđượcápdụngchưaif (!$cart->has_discount($coupon_code)) {$cart->apply_coupon($coupon_code);wc_add_notice(__('Mã giảm giá đã được tự động áp dụng cho đơn hàng của bạn!'), 'success'); } } else {//Gỡmãgiảmgiánếukhôngđủđiềukiệnif ($cart->has_discount($coupon_code)) {$cart->remove_coupon($coupon_code);wc_add_notice(__('Đơn hàng không còn đủ điều kiện áp dụng mã giảm giá.'), 'notice'); } }}
add_action('woocommerce_before_calculate_totals','apply_multiple_coupons_if_eligible');functionapply_multiple_coupons_if_eligible($cart) {if (is_admin() && !defined('DOING_AJAX')) {return; }//Đảmbảochỉchạymộtlầnif (did_action('woocommerce_before_calculate_totals') >= 2) {return; }//Danhsáchmãgiảmgiávàđiềukiện$coupons= [ ['code'=>'DISCOUNT10', // Mã giảm giá 1'minimum_amount'=>500000, // Điều kiện số tiền tối thiểu'categories'=> [12, 15], // Áp dụng cho danh mục cụ thể (IDs) ], ['code'=>'FREESHIP', // Mã giảm giá 2'minimum_amount'=>300000, // Điều kiện số tiền tối thiểu'products'=> [101, 102], // Áp dụng cho sản phẩm cụ thể (IDs) ], ['code'=>'SALE20', // Mã giảm giá 3'minimum_amount'=>700000, // Điều kiện số tiền tối thiểu'all_products'=>true, // Áp dụng cho toàn bộ đơn hàng ], ];foreach ($coupons as $coupon) {$coupon_code=$coupon['code'];//Kiểmtraxemmãgiảmgiáđãđượcápdụngchưaif ($cart->has_discount($coupon_code)) {continue; }$eligible=false;//Kiểmtrađiềukiệnsốtiềntốithiểuif (!empty($coupon['minimum_amount']) && $cart->subtotal >=$coupon['minimum_amount']) {$eligible=true; }//Kiểmtradanhmụcif (!empty($coupon['categories'])) {$category_in_cart=false;foreach ($cart->get_cart() as $cart_item) {$product_id=$cart_item['product_id'];$categories=wp_get_post_terms($product_id, 'product_cat', ['fields'=>'ids']);if (array_intersect($categories,$coupon['categories'])) {$category_in_cart=true;break; } }$eligible=$eligible && $category_in_cart; }//Kiểmtrasảnphẩmif (!empty($coupon['products'])) {$product_in_cart=false;foreach ($cart->get_cart() as $cart_item) {if (in_array($cart_item['product_id'],$coupon['products'])) {$product_in_cart=true;break; } }$eligible=$eligible && $product_in_cart; }//Kiểmtraápdụngchotấtcảsảnphẩmif (!empty($coupon['all_products'])) {$eligible=$eligible && true; }//Ápdụngmãgiảmgiánếuđủđiềukiệnif ($eligible) {$cart->apply_coupon($coupon_code);wc_add_notice(__('Mã giảm giá "'.$coupon_code.'" đã được tự động áp dụng.'), 'success'); } else {//Gỡbỏmãgiảmgiánếukhôngcònđủđiềukiệnif ($cart->has_discount($coupon_code)) {$cart->remove_coupon($coupon_code);wc_add_notice(__('Đơn hàng không còn đủ điều kiện cho mã giảm giá "'.$coupon_code.'".'), 'notice'); } } }}