Created
December 4, 2024 14:37
-
-
Save goranefbl/07c406265b0cd147f786f5291b401bdd to your computer and use it in GitHub Desktop.
fees instead of coupons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* TEMPORARY FEES INSTEAD OF COUPONS CODE */ | |
add_action('woocommerce_cart_calculate_fees', 'apply_10_percent_discount_as_fee'); | |
function apply_10_percent_discount_as_fee($cart) { | |
// Avoid applying the fee in the admin or if cart is empty | |
if (is_admin() || !WC()->cart->get_cart_contents_count()) { | |
return; | |
} | |
// Calculate 10% discount | |
$discount = $cart->get_subtotal() * 0.10; | |
// Add negative fee (which acts as a discount) | |
$cart->add_fee(__('10% Discount', 'woocommerce'), -$discount); | |
} | |
add_filter('woocommerce_coupon_is_valid', 'disable_coupons_when_fee_applied', 10, 2); | |
function disable_coupons_when_fee_applied($valid, $coupon) { | |
return false; | |
} | |
add_filter('woocommerce_coupon_error', 'custom_coupon_error_message', 10, 3); | |
function custom_coupon_error_message($err, $err_code, $coupon) { | |
if ($err_code === 100) { | |
return __('Coupons are disabled as higher discount is applied.', 'woocommerce'); | |
} | |
return $err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment