2

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

  • What you are try to archive? – Dhiren Vasoya Jan 20 '17 at 13:28
  • i want add one more add to cart button and if clicked that button it should display customer login pop up if customer not logged in – user49613 Jan 20 '17 at 13:39
  • Then what about the first button used? If customer not login? – Dhiren Vasoya Jan 20 '17 at 13:41
  • newly added button will show customer login form in pop up – user49613 Jan 21 '17 at 8:42
  • Then you can put simple link for customer instade of button, or you can make look of that link same like button. – Dhiren Vasoya Jan 21 '17 at 8:44
1

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; ?>
|improve this answer|||||
  • I don't think a module is necessary because you aren't overriding/adding any new classes. This can all be done in a theme override. Just do steps 4 and 5 in app/design/frontend/[Vendor]/[Theme]/Magento_Catalog – Steve Karwacki Oct 29 '18 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.