Magento – How to get the value of quantity entered in the input field in single product page to input field in cart page in magento

addtocartmagento2product

I have build a ecommerce website in magento. In the website, I have a single product page for all products which has a quantity input field, When I am entering a value in the quantity input field and clicking the add to cart button, it goes to a cart which also has a quantity input field but it doesn't show the value inputted in the quantity field in the single product page. Can anyone tell how to solve this issue?

In the database, sales_flat_item has a quantity attribute but has a default value 1 being stored.

Best Answer

You have just add form in your item.phtml file magento 2

I have put my form please see that and change field as you required.

<form  data-role="tocart-form" action="<?php echo $block->getAddToCartUrl($_item); ?>" method="post">
<input type="hidden" name="product" value="<?php /* @escapeNotVerified */echo $postParams['data']['product']; ?>">
<input type="hidden" name="<?php /* @escapeNotVerified */echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">

<div class="control">
    <input type="number" name="qty" id="qty" maxlength="12" style="width:150px;float: left;margin-right: 35px;"
    value="<?php /* @escapeNotVerified */echo $block->getProductDefaultQty() * 1 ?>" title="<?php /* @escapeNotVerified */echo __('Qty') ?>" class="input-text qty" data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"/>
</div>

<?php echo $block->getBlockHtml('formkey') ?>
<?php $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface');?>

<button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary" style="float:left;">

    <span><?php /* @escapeNotVerified */echo __('Add to Cart') ?></span>
</button>

Related Topic