Created
November 12, 2025 07:33
-
-
Save TanvirHasan19/4691900e2fb7bbeb764b4e0bb50fc796 to your computer and use it in GitHub Desktop.
Workaround for BOGO Coupon Not Working with Regular Coupons
This file contains hidden or 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 | |
| /** | |
| * Workaround for BOGO Coupon Not Working with Regular Coupons | |
| * | |
| * This snippet fixes the issue where BOGO coupons don't apply correctly | |
| * when a regular WooCommerce coupon is already in the cart. | |
| * | |
| * DO NOT MODIFY PLUGIN CORE FILES | |
| * Add this to your theme's functions.php or a custom plugin | |
| * | |
| * @package Advanced Coupons BOGO Fix | |
| * @version 1.0 | |
| */ | |
| // Prevent direct access | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| /** | |
| * Reset BOGO calculation when a coupon is applied | |
| * | |
| * This ensures BOGO recalculates when a new coupon is added to the cart, | |
| * fixing the issue where BOGO doesn't work when a regular coupon is already applied. | |
| */ | |
| function acfw_reset_bogo_calculation_on_coupon_apply( $coupon_code ) { | |
| // Only run if Advanced Coupons is active | |
| if ( ! class_exists( 'ACFWF\Models\BOGO\Frontend' ) ) { | |
| return; | |
| } | |
| // Get BOGO Frontend instance | |
| $bogo_frontend = \ACFWF()->BOGO_Frontend; | |
| if ( ! $bogo_frontend ) { | |
| return; | |
| } | |
| // Use reflection to access private _calculation property | |
| $reflection = new ReflectionClass( $bogo_frontend ); | |
| if ( ! $reflection->hasProperty( '_calculation' ) ) { | |
| return; | |
| } | |
| $property = $reflection->getProperty( '_calculation' ); | |
| $property->setAccessible( true ); | |
| $calculation = $property->getValue( $bogo_frontend ); | |
| if ( ! $calculation ) { | |
| return; | |
| } | |
| // Reset calculation using reflection | |
| $calc_reflection = new ReflectionClass( $calculation ); | |
| if ( $calc_reflection->hasMethod( 'reset_calculation' ) ) { | |
| // If method exists (after plugin update), use it | |
| $calculation->reset_calculation(); | |
| } else { | |
| // Otherwise, use reflection to reset the flag | |
| if ( $calc_reflection->hasProperty( '_calculation_done' ) ) { | |
| $calc_property = $calc_reflection->getProperty( '_calculation_done' ); | |
| $calc_property->setAccessible( true ); | |
| $calc_property->setValue( $calculation, false ); | |
| } | |
| // Clear session data to force fresh calculation | |
| if ( $calc_reflection->hasMethod( 'clear_session_data' ) ) { | |
| $calculation::clear_session_data(); | |
| } | |
| } | |
| } | |
| add_action( 'woocommerce_applied_coupon', 'acfw_reset_bogo_calculation_on_coupon_apply', 5 ); | |
| /** | |
| * Allow BOGO to work with items that have regular coupon discounts | |
| * | |
| * This filter ensures items with regular WooCommerce coupon discounts | |
| * are not excluded from BOGO calculations. | |
| */ | |
| function acfw_allow_bogo_with_regular_coupons( $is_valid, $cart_item ) { | |
| // Always allow items with regular coupon discounts | |
| // Only exclude items with Advanced Coupons-specific discounts | |
| if ( isset( $cart_item['acfw_bogo_discount'] ) || | |
| isset( $cart_item['acfw_add_products'] ) || | |
| isset( $cart_item['acfw_store_credits'] ) ) { | |
| return false; | |
| } | |
| // Allow items with regular WooCommerce coupon discounts | |
| return true; | |
| } | |
| add_filter( 'acfw_bogo_is_item_valid', 'acfw_allow_bogo_with_regular_coupons', 10, 2 ); | |
| /** | |
| * Clear BOGO session on cart update | |
| * | |
| * This ensures BOGO recalculates when cart contents change. | |
| */ | |
| function acfw_clear_bogo_session_on_cart_update() { | |
| if ( ! class_exists( 'ACFWF\Models\Objects\BOGO\Calculation' ) ) { | |
| return; | |
| } | |
| // Clear session when cart is updated | |
| \ACFWF\Models\Objects\BOGO\Calculation::clear_session_data(); | |
| } | |
| add_action( 'woocommerce_cart_updated', 'acfw_clear_bogo_session_on_cart_update', 5 ); | |
| add_action( 'woocommerce_add_to_cart', 'acfw_clear_bogo_session_on_cart_update', 5 ); | |
| add_action( 'woocommerce_cart_item_removed', 'acfw_clear_bogo_session_on_cart_update', 5 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment