Skip to content

Instantly share code, notes, and snippets.

@torunar
Created September 4, 2019 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torunar/8e40c39a9950dad3e1dc8e76121e9191 to your computer and use it in GitHub Desktop.
Save torunar/8e40c39a9950dad3e1dc8e76121e9191 to your computer and use it in GitHub Desktop.
diff --git a/app/addons/store_locator/func.php b/app/addons/store_locator/func.php
index 65176887a6..95092eb9c3 100644
--- a/app/addons/store_locator/func.php
+++ b/app/addons/store_locator/func.php
@@ -632,3 +632,17 @@ function fn_store_locator_calculate_cart_post($cart, $auth, $calculate_shipping,
}
}
}
+
+/**
+ * The "calculate_cart_content_before_shipping_calculation" hook handler.
+ *
+ * Actions performed:
+ * - Adds stores and pickup points table into caching condition
+ *
+ * @see \fn_calculate_cart_content()
+ */
+function fn_store_locator_calculate_cart_content_before_shipping_calculation($cart, $auth, $calculate_shipping, $calculate_taxes, $options_style, $apply_cart_promotions, &$shipping_cache_tables)
+{
+ $shipping_cache_tables[] = 'store_locations';
+ $shipping_cache_tables[] = 'store_location_descriptions';
+}
diff --git a/app/addons/store_locator/init.php b/app/addons/store_locator/init.php
index e6f4771bcd..945c8aba3a 100644
--- a/app/addons/store_locator/init.php
+++ b/app/addons/store_locator/init.php
@@ -26,5 +26,6 @@ fn_register_hooks(
'calculate_cart_taxes_pre',
'update_cart_by_data_post',
'pickup_point_variable_init',
- 'get_shipping_info_after_select'
+ 'get_shipping_info_after_select',
+ 'calculate_cart_content_before_shipping_calculation'
);
diff --git a/app/functions/fn.cart.php b/app/functions/fn.cart.php
index 9434c43c88..e15a35b961 100644
--- a/app/functions/fn.cart.php
+++ b/app/functions/fn.cart.php
@@ -3600,24 +3600,57 @@ function fn_calculate_cart_content(
$cart['stored_taxes'] = 'N';
}
+ $shipping_cache_tables = [
+ 'shippings',
+ 'shipping_descriptions',
+ 'shipping_rates',
+ 'shipping_services',
+ 'shipping_service_descriptions',
+ 'shipping_time_descriptions',
+ 'countries',
+ 'states',
+ ];
+
+ /** @var \Tygh\Storefront\Storefront $storefront */
+ $storefront = Tygh::$app['storefront'];
+ $shipping_cache_key = 'calculated_shipping_rates_' . $storefront->storefront_id;
+
/**
* Executes when the cart content is calculated before the shipping rates are calculated,
* allows you to modify the cart state.
*
- * @param array $cart Cart data
- * @param array $auth Authentication data
- * @param string $calculate_shipping 1-letter flag indicating how to calculate the shipping cost (not used):
- * A - calculate all available methods
- * E - calculate selected methods only (from cart[shipping])
- * S - skip calculation
- * @param bool $calculate_taxes Whether taxes should be calculated
- * @param string $options_style 1-letter flag indicating how to obtain options information:
- * F - full
- * S - skip selection
- * I - info
- * @param bool $apply_cart_promotions Whether promotions should be applied to the cart
+ * @param array $cart Cart data
+ * @param array $auth Authentication data
+ * @param string $calculate_shipping 1-letter flag indicating how to calculate the shipping cost:
+ * A - calculate all available methods
+ * E - calculate selected methods only (from cart[shipping])
+ * S - skip calculation
+ * @param bool $calculate_taxes Whether taxes should be calculated
+ * @param string $options_style 1-letter flag indicating how to obtain options information:
+ * F - full
+ * S - skip selection
+ * I - info
+ * @param bool $apply_cart_promotions Whether promotions should be applied to the cart
+ * @param string[] $shipping_cache_tables Database tables that cause shipping recalculation
+ * @param string $shipping_cache_key Shipping rates cache key
*/
- fn_set_hook('calculate_cart_content_before_shipping_calculation', $cart, $auth, $calculate_shipping, $calculate_taxes, $options_style, $apply_cart_promotions);
+ fn_set_hook(
+ 'calculate_cart_content_before_shipping_calculation',
+ $cart,
+ $auth,
+ $calculate_shipping,
+ $calculate_taxes,
+ $options_style,
+ $apply_cart_promotions,
+ $shipping_cache_tables,
+ $shipping_cache_key
+ );
+
+ // If shipping methods were changed, shipping recalculation is forced
+ Registry::registerCache(['checkout', $shipping_cache_key], $shipping_cache_tables, Registry::cacheLevel('user'));
+ if ($calculate_shipping === 'S' && !Registry::isExist('calculated_shipping_rates')) {
+ $calculate_shipping = 'A';
+ }
// $cart and $auth could be changed by addons, thus refresh is required
$location = fn_get_customer_location($auth, $cart);
@@ -3681,6 +3714,7 @@ function fn_calculate_cart_content(
if ($cart['calculate_shipping']) {
$rates = Shippings::calculateRates($shippings);
+ Registry::set($shipping_cache_key, $rates);
foreach ($rates as $rate) {
$group_key = $rate['keys']['group_key'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment