Advanced
This guide contains several tips and tricks for advanced usage of the Magento 2 integration in a Front-Commerce project.
Caching customers' cart
By default, the customer cart is always fetched from Magento, ensuring it's up
to date but also adding significant load on Magento and slowing down operations
in Front-Commerce. To improve performance, we introduced a cart cache. You can
enable it by setting the FRONT_COMMERCE_CART_CACHE_ENABLE
environment
variable:
FRONT_COMMERCE_CART_CACHE_ENABLE=true
The cart handling is a critical feature on an eCommerce website, if you enable the cart cache we strongly advice to fully test your project before going live with it.
When enabled, Front-Commerce caches the cart and automatically expires it when an operation affects the cart (e.g., adding an item). If you're using an API that modifies the cart, you'll need to manually expire the cache. See our updated guide on Cart Caching for more details.
To avoid any cache corruption, make sure to always use the CartCache
instance
created by the Magento2/Cart
GraphQL module (see for instance
the initialization of the StoreCredit
loader)
Caching current customers' information
By default, fetching customer data (Query.me in GraphQL) always queries Magento, ensuring fresh data but causing unnecessary load and delays. Since customer data rarely changes without user interaction, Front-Commerce introduced session-based cache to improve performance. Enable it by setting:
FRONT_COMMERCE_CURRENT_CUSTOMER_CACHE_ENABLE=true
If you enable current customer's cache, we strongly advice to fully test your
project before going live with it. You can use the
DEBUG=front-commerce:customer:cache
flag to monitor what happens in your
process output.
When active, the cache is cleared automatically when relevant changes occur
(e.g., adding an address). If an external API updates customer info, you must
manually expire the cache. To do that, you can use
makeCurrentCustomerCache
factory to create an instance of this cache, and expire()
it. You can have a
look at how it is used in
CustomerLoader
where the customer cache is expired after some operations.
Learn more in our Customer Data Caching guide.
Optimized add multiple products to the cart
By default, the
addMultipleItemsToCart
mutation relies on the Magento2 REST API. When this mutation is called,
Front-Commerce triggers sequentially as many REST API calls as there are
different products to add to the cart. With a high number of products, this
process can be quite slow.
By setting the FRONT_COMMERCE_OPTIMIZED_ADD_MULTIPLE_CART_ITEMS
environment
variable to true
, the addMultipleItemsToCart
mutation leverages the Magento2
GraphQL API to add many products to the cart in a maximum 3 API calls no matter
the number of products. This feature is not enabled by default because it comes
with 2 limitations:
- the Magento2 mutation used behind the scene does not support bundle products
where several products can be picked for an item (input type
checkbox
) - With Magento 2.4.6, the mutation is not able to set customizable options.
Additional headers in Magento API calls
Front-Commerce allows you to send additional headers in all API calls. To do so,
you must define the magento.api.extraHeaders
(for storefront API) and/or
magento.api.extraAdminHeaders
(for admin API) configuration values from a
configuration provider.
These additional headers could be useful if you want to add additional context to your queries, depending on the request or to detect Front-Commerce requests from your Magento server.
You can refer to our Quanta module example for a working example using this feature.
Clearing Front-Commerce cache
There are two ways to clear the magento cache:
-
Run the below command from the command line on the magento server:
bin/magento cache:clean -- front-commerce
-
From the admin interface go to System > Cache Management then select Front-Commerce, make sure that refresh is selected and then click the Submit button. (see below screenshot for more details).
Deactivating URL rewrites in emails
Since version Magento2 module 2.10.0
For Merchants also using their Magento2 backend in a non-headless way, it is possible to disable URL rewrites for emails related to the monolithic Magento2 frontend while rewriting emails sent from a Front-Commerce API call.
This feature is an opt-in feature, which means that merchants can choose to enable or disable it based on their requirements.
To do so, go to the "Stores > Configuration > General > Front-Commerce" section and update the configuration as illustrated below:
- when this configuration is
Yes
(default), URLs in all emails sent are rewritten to use the Front-Commerce frontend URL. - when this configuration is
No
, URLs in emails are not rewritten except if the email is directly sent from a Front-Commerce API call.