Skip to content

Update qty of cart item – Magento 2.4

We want to create a input field, that loads the qty of the latest item added to the cart and when changing this input field, it should change the qty of that item in the cart.

The first part, to check the qty that works fine. But how can we also make it possible to use this input field, to update the qty of that item in the cart?

See code below.

<?php $cart->getQuote()->getItemsCollection();
$latestItem = $itemsCollection->getLastItem();?>

<input id="cart-<?= /* @escapeNotVerified */ $latestItem->getId() ?>-qty"
        name="cart[<?= /* @escapeNotVerified */ $latestItem->getId() ?>][qty]"
        data-cart-item-id="<?= /* @escapeNotVerified */ $latestItem->getSku() ?>"
        value="<?= /* @escapeNotVerified */ $latestItem->getQty() ?>"
        type="number"
        size="2"
        title="<?= $block->escapeHtml(__('Qty')) ?>"
        class="input-text qty"
        data-validate="{required:true,'validate-greater-than-zero':true}">