elementor / elementor/elementor

🚀 Feature Request: Extend caching logic for the off canvas cart

Open
#12,534 2 comments 0 reactions 0 assignees View on GitHub
compatibility/3rd-party integration/woocommerce mod* mod/e* mod/r* product/pro request/enhancement widget/pro/wc/menu-cart
Dominant language
PHP
Stars
7.1k
Forks
1.6k
Avg merge
1d 5h
Merged PRs (30d)
193

Description

## Prerequisites

- [x] I have searched for similar features requests in both open and closed tickets and cannot find a duplicate.
- [x] The feature is still missing in the latest stable version of Elementor ( Elementor Pro. )

Issue
--
The off canvas cart caches the amounts from the cart without taking into account the currency in which such amounts were calculated. Due to that, when the site is displayed in another currency, the totals are the cached ones, although they are formatted with the new currency symbol. Adding, removing or updating cart items refreshes the cache, showing the correct amounts.

Suggested solution
--
Alter the key used to cache the values for the off canvas cart by taking into account the active currency. That is:
`$cache_key = 'existing_cache_key' . get_woocommerce_currency();`
Function `get_woocommerce_currency()` is part of WooCommerce core.

By doing that, when the currency changes, the cached data will be different as well. For example:
1. Item added to cart, 100 USD. The custom cart element should cache the data, keeping track of the currency (i.e. USD). Cache key: `some_key_USD`.
2. The currency changes to EUR.
3. The off canvas cart should fetch the totals using "EUR" as the currency (cache key: `some_key_EUR`). Since such cached data doesn't exist, the cart should fetch fresh totals, just like it does when an item is added to the cart, or removed from it, then cache the new total in EUR.
4. The currency changes back to USD. The off canvas should fetch the USD amounts stored at step 1, using cache key `some_key_USD`.
5. The currency changes to GBP. The cart should follow the same logic described at step 3, i.e. fetch the fresh totals and store them for GBP, using cache key `some_key_GBP`.
6. An item is added to the cart, or removed. The cache should be cleared for all currencies (i.e. `some_key_*`), for example by using the cache versioning logic offered by WooCommerce (see `WC_Cache_Helper::get_transient_version()`).

**Additional improvements**
Adding a filter for the cache key, such as apply_filter('elementor_offcanvas_cart_cache_key', $cache_key) would allow 3rd parties to handle custom conditions that could require a recalculation, and separate caching, of the cart totals. For example, the cart totals could change with the billing or shipping country. That information could be

Context details
--
This is an issue reported by customer E. Berg, who is using Elementor with our Aelia Currency Switcher. Still, the proposed solution is fully backward compatible and works fine in a "plain" WooCommerce site, as it doesn't rely on the actual presence of a multi-currency solution.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.