| 1 |
<?php |
| 2 |
|
| 3 |
// Ensure the file is not accessed directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use Disco\App\Disco; |
| 9 |
|
| 10 |
if ( ! function_exists( 'disco_add_free_shipping_rate' ) ) { |
| 11 |
|
| 12 |
/** |
| 13 |
* Add free shipping rate to the package. If any campaign matches, then apply free shipping. |
| 14 |
* |
| 15 |
* @param array $rates Array of rates found for the package. |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
function disco_add_free_shipping_rate( $rates ) { |
| 19 |
$is_free_shipping = ( new Disco )->apply_free_shipping( WC()->cart ); |
| 20 |
|
| 21 |
if ( ! $is_free_shipping ) { |
| 22 |
return $rates; |
| 23 |
} |
| 24 |
|
| 25 |
foreach ( $rates as $rate_id => $rate ) { |
| 26 |
if ( 'free_shipping' === $rate->method_id ) { |
| 27 |
return $rates; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// If free shipping is not available, override rates with free shipping |
| 32 |
// TODO: Add Label from campaign |
| 33 |
$free_shipping_rate = new WC_Shipping_Rate( 'disco_free_shipping', __( 'Free Shipping', 'disco' ), 0, array(), 'disco_free_shipping' ); |
| 34 |
$rates['disco_free_shipping'] = $free_shipping_rate; |
| 35 |
|
| 36 |
return $rates; |
| 37 |
} |
| 38 |
|
| 39 |
add_filter( 'woocommerce_package_rates', 'disco_add_free_shipping_rate', 100, 1 ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( ! function_exists( 'disco_refresh_shipping_cache_on_cart_checkout' ) ) { |
| 43 |
|
| 44 |
/** |
| 45 |
* Force WooCommerce to recalculate shipping rates on the cart and checkout pages. |
| 46 |
* |
| 47 |
* Disco adds/removes the disco_free_shipping rate through the |
| 48 |
* woocommerce_package_rates filter based on the current campaign state, but |
| 49 |
* WooCommerce caches the calculated package rates in the customer session |
| 50 |
* keyed by a hash of the cart contents only - campaign state is NOT part of |
| 51 |
* that hash. So when a campaign is activated or deactivated, an existing |
| 52 |
* session keeps serving the stale cached rates: a lingering disco_free_shipping |
| 53 |
* rate after deactivation, or a chosen shipping method that no longer exists, |
| 54 |
* which makes the shipping options appear hidden or broken until the cart |
| 55 |
* contents change. |
| 56 |
* |
| 57 |
* Clearing this session's cached package rates whenever the cart or checkout |
| 58 |
* page is viewed forces a fresh calculation that reflects the current campaign |
| 59 |
* state, for this customer only (no global cache flush). |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
function disco_refresh_shipping_cache_on_cart_checkout() { |
| 64 |
if ( is_admin() || ! function_exists( 'is_cart' ) || ! function_exists( 'WC' ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! is_cart() && ! is_checkout() ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$session = WC()->session; |
| 73 |
$cart = WC()->cart; |
| 74 |
|
| 75 |
if ( ! $session instanceof WC_Session || ! $cart instanceof WC_Cart ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Invalidate the cached rates for every shipping package in this session. |
| 80 |
// The package keys match the keys WooCommerce uses when it stores rates |
| 81 |
// under 'shipping_for_package_{key}', so setting them to false makes |
| 82 |
// WC_Shipping::calculate_shipping_for_package() recalculate instead of |
| 83 |
// returning the stale cached rates. |
| 84 |
foreach ( array_keys( $cart->get_shipping_packages() ) as $package_key ) { |
| 85 |
$session->set( 'shipping_for_package_' . $package_key, false ); |
| 86 |
} |
| 87 |
|
| 88 |
$cart->calculate_shipping(); |
| 89 |
} |
| 90 |
|
| 91 |
add_action( 'template_redirect', 'disco_refresh_shipping_cache_on_cart_checkout' ); |
| 92 |
} |
| 93 |
|