Even after years of development, the quest to find a solution with Magento 2 is un-ending. The difficulties I face in every implementation are real and common pain points. The objective is not to just brag about problems, but to solve them and ask for feedback.
Though I still have faith in the Magento Community, still a lot of improvements are needed in the core for a better developer experience. Let's check out a few I faced (and fixed) in Magento 2!
-
Observing add to cart/qty update
-
Why do we need this?
- We don't want to use magento/inventory (i.e MSI) for stock management in multiple locations (though we have multiple warehouses) due to its performance, bugs and features (different than needed in our inventory system).
- We don't want to use magento/catalog-inventory for the same reason (unfulfilled requirements) and its deprecated in favour of magento/inventory .
- We needed the inventory as a service, decoupled from Magento 2.
- We needed to add validation to restricted minimum add to cart and maximum add to cart quantity.
- We needed to apply promotions/linked services (like gift items, warranty service item, etc) with the main product add to the cart.
-
How did we solve it?
- Using the
sales_quote_product_add_after
event for new product add - Using the
sales_quote_item_qty_set_after
event for qty change (will run on add new product also) -
Refer to magento/catalog-inventory (deprecated) observer for qty validation.
public function validate(Observer $observer) { /* @var $quoteItem Item */ $quoteItem = $observer->getEvent()->getItem(); if (!$quoteItem || !$quoteItem->getProductId() || !$quoteItem->getQuote() ) { return; } $product = $quoteItem->getProduct(); $qty = $quoteItem->getQty(); /* @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
- Using the
sales_quote_save_before
event to run validation before cart save.
- Using the
-
-
Observing order status/state change
-
Why do we need this?
- You might have heard about the common Magento 2 certification question, i.e
How do you sync/push orders to 3rd party ERP ?
(we needed it) but the Magento 2 order workflow provides no consistent way to observe orderstatus/state
change. - We need to run after the order promotions, like providing coupons to the customer on successful order delivery.
- We need to auto fulfil orders for few cases.
- You might have heard about the common Magento 2 certification question, i.e
-
How did we solve it?
- Using
sales_order_save_after
event and match theorig_data
with currentdata
but its not reliable. In zero-subtotal order, thesales_order_save_after
original status will benull
and the current status will beprocessing
but in other cases, it will benew/pending
for newly placed orders. -
Using
after
plugins can be added on the followings, but its do not cover all cases for example:pending -> processing
. or any custom status that is being added from admin.\Magento\Sales\Api\OrderManagementInterface::cancel($id)
\Magento\Sales\Api\OrderManagementInterface::hold($id)
\Magento\Sales\Api\OrderManagementInterface::unHold($id)
\Magento\Sales\Api\OrderManagementInterface::place($id)
- Using
-
-
Add multiple products in addition to the cart
-
Why do we need this?
- Bulk order or bulk wishlist to cart action requires multiple products add to cart APIs but Magento 2 is not designed to add multiple products together in the cart.
-
How did we solve it?
- Using the
around
plugin on add to cart.
- Using the
-
-
Consumers and their inconsistency with Repositories
-
Why do we need this?
- We needed to optimise the checkout flow and decouple few operations on place order and process them asynchronously but don't use the repository in
queue
consumers.
- We needed to optimise the checkout flow and decouple few operations on place order and process them asynchronously but don't use the repository in
-
How did we solve it?
- Using
OrderRepositoryInterfaceFactory
object insteadOrderRepositoryInterface
in the__construct
. Not the best solution, but was a quick fix.
- Using
-
-
APIs that don't support token refresh/revoke
- Yes, we preferred
REST
APIs overgraphql
, due to stability and security reasons but it misses some basic APIs, like customer logout via API or refreshing thecustomer token
with therefresh token
. -
How did we solve it?
- Added our own
logout
API by referring toGraphQL
logout implementation in Magento2. - Refresh token is still in backlog (maybe I will pick it up). [FEAT] Add token refresh API call to GraphQl and REST API
- Added our own
- Yes, we preferred
-
Core bugs that need test cases
- There were small bugs, like few admin customer form fields were not getting saved on submit, we raise PRs to the core, but was very difficult to merge them as a small change in
di.xml
would require test cases to be added.
- There were small bugs, like few admin customer form fields were not getting saved on submit, we raise PRs to the core, but was very difficult to merge them as a small change in
-
Bundled modules that are bloatware
-
Why do we need this?
- I don't think I need to explain this, there are around
157
modules that we generally disable in a project. Also, usedreplace
a few times incomposer.json
to totally remove them from the project (but make sure you know thatMagento Commerce
do not provide support if you remove i.ereplace
any bundled module). - We don't use
magento/inventory
,magento/graph-ql
, shipping module likedhl
,fedex
, payment method modules, 3rd party modules, etc so why keep them in the database and in vendor with all dependencies.
- I don't think I need to explain this, there are around
-
How did we solve it?
-
Jisse Reitsma from Yireo has already provided a great package to remove optional modules. Take reference from the following
composer.json
file and remove not needed module from the project.{ "name": "yireo/magento2-replace-all", "description": "Remove various packages from Magento", "require": { "magento/product-community-edition": "2.4.*" }, "replace": { "amzn/amazon-pay-and-login-magento-2-module": "*", "amzn/amazon-pay-and-login-with-amazon-core-module": "*", "amzn/amazon-pay-module": "*", "amzn/amazon-pay-sdk-php": "*", "amzn/login-with-amazon-module": "*", "astock/stock-api-libphp": "*", "braintree/braintree": "*", "braintree/braintree_php": "*", "dotmailer/dotmailer-magento2-extension": "*", "dotmailer/dotmailer-magento2-extension-b2b": "*", "dotmailer/dotmailer-magento2-extension-chat": "*", "dotmailer/dotmailer-magento2-extension-enterprise": "*", "dotmailer/dotmailer-magento2-extension-enterprise-package": "*", "dotmailer/dotmailer-magento2-extension-package": "*", "dotmailer/dotmailer-magento2-extension-sms": "*", "klarna/m2-payments": "*", "klarna/module-core": "*", "klarna/module-kp": "*", "klarna/module-kp-graph-ql": "*", "klarna/module-onsitemessaging": "*", "klarna/module-ordermanagement": "*",
-
-
-
TCO that is very high
- Magento 2 offers a million of features, but the large scale system uses none besides the framework. I must say that there is no other platform such as Magento 2 which offers such features out of the box at a very less price or free (open source). SFCC, Hybris, etc are no way near to Magento2 in terms of features.
- Large stores don't prefer monolith or say can use monolith due to certain factors. Inventory is managed from WMS, CMS is mostly not Magento OTB, CRM is external, PIM is external. Storefront is PWA/Headless. So what they really use is API and some part of the back office.