Magento – Magento 2 – Add Quantity Field

addtocartextensionsmagento2quantity

I'm trying to edit a related products extension by adding a quantity box next to the add to cart button. Here is the block of code it needs to be added to:

<?php if ($showCart): ?>
  <div class="actions-primary">
  <?php if ($_item->isSaleable()): ?>
    <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
      <button class="action tocart primary" data-mage-init='{"redirectUrl": {"url": "<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
        <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
      </button>
    <?php else: ?>
  <?php $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
?>
    <button class="action tocart primary"
            data-post='<?php /* @escapeNotVerified */ echo $postData; ?>'
            type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
      <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
    </button>
   <?php endif; ?>
 <?php else: ?>
<?php if ($_item->getIsSalable()): ?>
    <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
   <?php else: ?>
<div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
   <?php endif; ?>
  <?php endif; ?>
</div>
<?php endif; ?>

I have a quantity field in the template now, but no matter what quantity I enter, it always adds 1 to the cart.

Best Answer

You need define new observer

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_collection_load_after">
        <observer name="Your_Module_Add_Quantity" instance="Your\Module\Observer\AddQuantityToCollectionObserver"/>
    </event>
</config>

With code like

namespace Your\Module\Observer;
class AddQuantityToCollectionObserver implements ObserverInterface
{
    /**
     * Add information about product Quantity to collection
     * Used for product collection after load
     *
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $productCollection = $observer->getEvent()->getCollection();
        $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
        foreach ($productCollection as $product) {
            $productId = $product->getId();
            $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
            $product->setQty($stockStatus->getQty());
        }
    }
}

You can also take refernce from https://magento.stackexchange.com/a/98085/33354

Related Topic