Magento 2 : Double qty before add to cart

how to change the functionality of add to cart button like I click on add to cart then qty should be doubled?

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php $isSaleable = $_product->isSaleable();?>
<?php //if($_product->getFinalPrice() > 0){ ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 2 ?>"
                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button" <?php echo ($_product->isVirtual() && $isSaleable)?'':'disabled'; ;?>>
                <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
            </button>
            <?php if($isSaleable):?>
                <?= $block->getChildHtml('', true) ?>
            <?php endif;?>
        </div>
    </div>
</div>
<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>

Answers 1

  • This should do it ! Don't forget to upvote/mark as answered :)

    On your custom module: /etc/events.xml

    <event name="controller_action_predispatch_checkout_cart_add">
            <observer name="restrict_sales_model_cart_add_before" instance="vendor\module\Observer\CartAddingConditionCheck" />
        </event>  
    

    /Observer/CartAddingConditionCheck.php

    <?php
    namespace vendor\module\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
    use Magento\Catalog\Model\Product;
    
    class CartAddingConditionCheck implements ObserverInterface
    {
        protected $request;
        protected $product;
    
        public function __construct(
           RedirectInterface $redirect,
           Product $product,
        )
        {
            $this->request         = $request;
            $this->product         = $product;
        }
        
        public function execute(
            \Magento\Framework\Event\Observer $observer
        )
        {   
            $postValues = $this->request->getPostValue();
            $item = $this->product;   
            if ($item->getParentItem()) 
            {
                $item = $item->getParentItem();
            }
            $quantity         = isset($postValues['qty']) ? $postValues['qty'] : 1;
            
    
            $item->setQty($quantity * 2);
         }
    }
    ?>
    

Related Questions