Magento – magento 2 Adding one more Add to cart button

addtocartmagento2

How to add one more add to cart button in magento 2 product view page and working of 2 buttons are independently [not conflict one button to another button] and validations also must to add product to cart. Please help me

Best Answer

You need to create new extension for this follow the following steps

1.Addtocart/Newbutton/composer.json

{
  "name": "addtocart/newbutton",
  "description": "Addtocart Newbutton",
  "require": {
    "php": "~5.5.0|~5.6.0",
    "magento/module-store": "0.74.0-beta4",
    "magento/module-theme": "0.74.0-beta4",
    "magento/module-widget": "0.74.0-beta4",
    "magento/module-backend": "0.74.0-beta4",
    "magento/module-catalog": "0.74.0-beta4",
    "magento/module-email": "0.74.0-beta4",
    "magento/module-ui": "0.74.0-beta4",
    "magento/module-variable": "0.74.0-beta4",
    "magento/module-media-storage": "0.74.0-beta4",
    "magento/framework": "0.74.0-beta4",
    "magento/magento-composer-installer": "*"
  },
  "type": "magento2-module",
  "version": "0.74.0-beta4",
  "license": [
      "OSL-3.0",
      "AFL-3.0"
  ],
  "extra": {
      "map": [
          [
              "*",
              "Addtocart/Newbutton"
          ]
      ]
  }
}

2.Addtocart/Newbutton/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Addtocart_Newbutton',
    __DIR__
);

3.Addtocart/Newbutton/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Addtocart_Newbutton" setup_version="1.0.0"></module>
</config>

4.Addtocart/Newbutton/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<body>    
    <referenceBlock name="product.info.form.options" htmlClass="css_class" htmlTag="div">
        <block class="Magento\Catalog\Block\Product\View"
            name="product.addtocart.info" as="product_addtocart_info"
            template="Addtocart_Newbutton::product/view/addtocart.phtml" 
            before="product.options.wrapper"
            >
        </block>
    </referenceBlock>
</body>
</page>

5.Addtocart/Newbutton/view/frontend/templates/product/view/addtocart.phtml

<?php
/**
 * Copyright © 2015 Magento. 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 if ($_product->isSaleable()): ?>
    <div class="box-tocart" style="border-bottom: 1px #ebebeb solid; margin-bottom: 15px; width: 100%;">
        <div class="fieldset" style="margin: 0 0 36px;">
            <?php if ($block->shouldRenderQuantity()): ?>
            <div class="no-display"><div class="field qty">
                <label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
                <div class="control">
                    <input type="number"
                           name="qty"
                           id="qty"
                           maxlength="12"
                           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>
                <div class="qty-changer">
                    <a href="javascript:void(0)" class="qty-inc"><i class="porto-icon-up-dir"></i></a>
                    <a href="javascript:void(0)" class="qty-dec"><i class="porto-icon-down-dir"></i></a>
                </div>
            </div></div>
            <?php endif; ?>
            <div class="actions">
                <button type="submit"
                        title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                        class="action primary tocart"
                        id="product-addtocart-button">
                    <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
                </button>
                <?php echo $block->getChildHtml('', true) ?>
            </div>
        </div>
    </div>
    <?php endif; ?>
    <?php if ($block->isRedirectToCartEnabled()) : ?>
    <script type="text/x-magento-init">
        {
            "#product_addtocart_form": {
                "Magento_Catalog/product/view/validation": {
                    "radioCheckboxClosest": ".nested"
                }
            }
        }
    </script>
    <?php else : ?>
    <script>
        require([
            'jquery',
            'mage/mage',
            'Magento_Catalog/product/view/validation',
            'Magento_Catalog/js/catalog-add-to-cart'
        ], function ($) {
            'use strict';

            $('#product_addtocart_form').mage('validation', {
                radioCheckboxClosest: '.nested',
                submitHandler: function (form) {
                    var widget = $(form).catalogAddToCart({
                        bindSubmit: false
                    });

                    widget.catalogAddToCart('submitForm', $(form));

                    return false;
                }
            });
        });
    </script>
<?php endif; ?>
Related Topic