cancel
Showing results for 
Search instead for 
Did you mean: 

How to add the Add to Cart button?

How to add the Add to Cart button?

Hello Magento Forum Members! Good Work..
I would like to add the Add to cart button under the products. My theme does not have such a feature. How can I do that? There is also a lot of space on the left and right sides. Is it possible to shrink them?

Many thanks to everyone in advance.

My website: https://milanom.com/

addtocart.JPG

4 REPLIES 4

Re: How to add the Add to Cart button?

Hello @emre_onural

 

You can add button in custom phtml file as follow:

In your block file

use Magento\Catalog\Block\Product\ListProduct;
<?phppublic function __construct(        \Magento\Backend\Block\Template\Context $context,        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,         \Magento\Catalog\Block\Product\ListProduct $listProductBlock,        array $data = []
    ) {        parent::__construct($context, $data);        $this->_productCollectionFactory = $productCollectionFactory;        $this->listProductBlock = $listProductBlock;
    }

public function getProductCollection()
{
    /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */   $collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->load();
    return $collection;
}
public function getAddToCartPostParams($product)
{
    return $this->listProductBlock->getAddToCartPostParams($product);
}
?>

get productlist in view file

<?php const PARAM_NAME_BASE64_URL = 'r64';
const PARAM_NAME_URL_ENCODED = 'uenc';
use Magento\Framework\App\Action\Action; 
$_productCollection = $block->getProductCollection();
?>
<?php foreach ($_productCollection as $_product): ?>
    <?php $postParams = $block->getAddToCartPostParams($_product); ?>
    <?php echo $_product->getName()?>    <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" 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]; ?>">
        <?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">
                <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
            </button>
    </form>
<?php endforeach;?>
 
If found my answer useful? Please give Kudos and accept it as solution.
Magento Developer
Ankita Biswas

Re: How to add the Add to Cart button?

Hi @emre_onural,

 

You can use

<?php $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
?><button class="action tocart primary"
        data-post='<?php /* @escapeNotVerified */ echo $postData; ?>'
        type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart1') ?>">
    <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>

Where $_item is your product.

 

Thank You!

Prema M

Re: How to add the Add to Cart button?

Thanks but I haven't found where to add these codes. Could you please help?


@Ankita Biswas wrote:

Hello @emre_onural

 

You can add button in custom phtml file as follow:

In your block file

use Magento\Catalog\Block\Product\ListProduct;
<?phppublic function __construct(        \Magento\Backend\Block\Template\Context $context,        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,         \Magento\Catalog\Block\Product\ListProduct $listProductBlock,        array $data = []
    ) {        parent::__construct($context, $data);        $this->_productCollectionFactory = $productCollectionFactory;        $this->listProductBlock = $listProductBlock;
    }

public function getProductCollection()
{
    /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */   $collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->load();
    return $collection;
}
public function getAddToCartPostParams($product)
{
    return $this->listProductBlock->getAddToCartPostParams($product);
}
?>

get productlist in view file

<?php const PARAM_NAME_BASE64_URL = 'r64';
const PARAM_NAME_URL_ENCODED = 'uenc';
use Magento\Framework\App\Action\Action; 
$_productCollection = $block->getProductCollection();
?>
<?php foreach ($_productCollection as $_product): ?>
    <?php $postParams = $block->getAddToCartPostParams($_product); ?>
    <?php echo $_product->getName()?>    <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" 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]; ?>">
        <?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">
                <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
            </button>
    </form>
<?php endforeach;?>
 
If found my answer useful? Please give Kudos and accept it as solution.

Thanks but I haven't found where to add these codes. Could you please help?

Re: How to add the Add to Cart button?

This problem is continuing. Please help me.