0

I am trying to create a secondary "Add to Cart" button which will add a product to cart, like "normal", but will add an additional Get/Post Parameter.

By "normal", I mean, it sends all form data; Custom Options, Configurable Options, Quantity, etc - like Magento does out the box.

I have successfully added a custom "Add to Cart" button, via an XML Layout and a .phtml Template File.

\Vendor\Module\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_configuration.xsd">
    <update handle="catalog_product_view"/>
    <body>
        <referenceBlock name="product.info.addtocart">
            <block name="product.info.addtocart.additionalButton"
                   class="Vendor\Module\Block\Product\View\Button"
                   template="Vendor_Module::button.phtml"
                   after="-" />
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <block name="product.info.addtocart.additionalButton.additional"
                   class="Vendor\Module\Block\Product\View\Button"
                   template="Vendor_Module::button.phtml"
                   after="-" />
        </referenceBlock>
    </body>
</page>

\Vendor\Module\view\frontend\templates\button.phtml

<?php
/**
 * @var $block \Vendor\Module\Block\Product\View\Button
 */
?>

<?php $buttonTitle = __('Add to Cart #2'); ?>

<button type="submit"
        title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
        class="action tocart"
        id="product-additionalparam-button">
    <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
</button>

This button works identically to the standard "Add to Cart" button, which is great; now I want to add the Get/Post Parameter - This is where I get stuck...

If I use a custom block, I can add a data-post attribute...

\Vendor\Module\Block\Product\View\Button

<?php

namespace Vendor\Module\Block\Product\View;

use Magento\Checkout\Helper\Cart as CartHelper;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class Button extends Template
{
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product;

    /**
     * @var CartHelper
     */
    protected $cartHelper;

    /**
     * @var PostHelper
     */
    protected $postHelper;

    /**
     * @param CartHelper $cartHelper
     * @param Context $context
     * @param PostHelper $postHelper
     * @param Registry $registry
     * @param array $data
     */
    public function __construct(
        CartHelper $cartHelper,
        Context $context,
        PostHelper $postHelper,
        Registry $registry,
        array $data = []
    )
    {
        $this->_product = $registry->registry('current_product');
        $this->cartHelper = $cartHelper;
        $this->postHelper = $postHelper;
        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function getPostData()
    {
        return $this->postHelper->getPostData(
            $this->cartHelper->getAddUrl($this->_product),
            [
                'product' => $this->_product->getEntityId(),
                'additional_param' => true
            ]
        );
    }
}

\Vendor\Module\view\frontend\templates\button.phtml

<?php
/**
 * @var $block \Vendor\Module\Block\Product\View\Button
 */
?>

<?php $buttonTitle = __('Add to Cart #2'); ?>

<button type="submit"
        title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
        class="action tocart"
        data-post="<?= $block->getPostData() ?>"
        id="product-additionalparam-button">
    <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
</button>

However now when I submit the form, it sends the Post Data from the data-post attribute, but this does not contain any of the form data the customer entered.

I have also tried adding name and value attributes to the button html, but Magento / jQuery seem to remove all submit buttons from the form object it uses.

I know I could create some custom JS to watch for a click on my custom "Add to Cart" button, have it change a value in a hidden input, but that all seems a bit clunky...

Any kind of suggestion would be greatly appreciated.

Your Answer

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

Browse other questions tagged or ask your own question.