Magento 2: Updating Product Quantity on the List Page
Dear Matt,
I understand your requirement of updating product quantity directly from the list page, similar to how it’s done in the cart or minicart. This is indeed possible with a bit of customization in Magento 2. Let me guide you through the process.
The Default Behavior
In the default Magento setup, when you add a product to the cart from the list page (list.phtml), only one quantity is added at a time, and the quantity field remains static. To change this behavior, we need to modify the list.phtml file and create an observer for updating the cart item.
Step 1: Modify the List Page
First, let’s update the product list template in vendor/magento/module-catalog/view/frontend/templates/product/list.phtml.
<div class="actions-primary">
<div class="product-add-to-cart">
<?php if (!$block->isSetOptionEmpty('add_to_cart')): ?>
<label for="<?php /* @escapeNotVerified */ echo $block->getAddToCartLabelId() ?>" class="product-label"><?= /* @escapeNotVerified */ __('Quantity') ?></label>
<input type="number" id="<?php /* @escapeNotVerified */ echo $block->getAddToCartLabelId() ?>" name="<?= /* @escapeNotVerified */ $block->getProduct()->getSku() ?>"
value="<?= /* @escapeNotVerided */ $block->getProduct()->getStockItem()->getQty() ?>" data-validate="<?= /* @escapeNotVerified */ $block->quoteJs(__('Required Number')) ?>" class="input-text cart-qty" title="<?= /* @escapeNotVerified */ __('Qty') ?>"
required
style="width: 50px;"
max="<?= /* @escapeNotVerified */ $block->getProduct()->getStockItem()->getQty() ?>">
<?php endif; ?>
</div>
</div>
We’ve added a quantity field and set its maximum value to the available stock quantity.
Step 2: Create an Observer
Next, we need to create an observer to handle the form submission for updating the cart item. This involves creating a new module and an event observer.
In your custom module (app/code/Vendor/CustomModule/etc/events.xml), define the following:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_checkout_cart_add_product">
<observer name="update_cart_item_quantity" instance="Vendor\CustomModule\Observer\UpdateCartItemQuantity"/>
</event>
</config>
Then, create the observer file in app/code/Vendor/CustomModule/Observer/UpdateCartItemQuantity.php:
namespace Vendor\CustomModule\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
class UpdateCartItemQuantity implements ObserverInterface
{
protected $cart;
public function __construct(\Magento\Checkout\Model\Cart $cart)
{
$this->cart = $cart;
}
public function execute(EventObserver $observer)
{
$request = $observer->getRequest();
$sku = $request->getParam('product');
$qty = (int) $request->getParam('qty');
if ($request->isXmlHttpRequest()) {
$cartItem = $this->cart->getQuote()->getItemBySku($sku);
if ($cartItem && $qty > 0) {
$this->cart->updateItemQty($cartItem->getId(), $qty);
}
}
return $this;
}
}
This observer checks for an XMLHTTPRequest, which is sent when the form is submitted through AJAX. It retrieves the cart item by SKU and updates the quantity if it exists.
Step 3: Flush Cache
After making these changes, make sure to flush your Magento cache using a command like:
php bin/magento cache:flush
Conclusion
With this setup, you should now be able to update the product quantity in real-time on the list page. This solution provides a similar experience to updating quantities in the cart or minicart.
Remember, when working with Magento 2 customizations like these, it’s essential to follow best practices for maintaining upgrade compatibility and code quality. For more advanced topics and professional services related to Magento development, I recommend checking out our services at Laravel Company.
Happy coding! Let me know if you have any further questions.
Best, Matthew