0

In a custom widget that displays on main page and category pages, I need to have a button clicking on which should add product catalog (having it's own sku) to the cart. Can you please suggest how to go about this?

Path of the custom widget is

app/code/<vendorname>/<modulename>/view/frontend/templates/widget/<widgetname.phtml>
0

You can try the below code.

just add the below code in your widget phtml file where ever you want.

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

In your block you can get the product collection like below code.

public function __construct(
 \Magento\Catalog\Block\Product\Context $context,
 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\ProductRepository $productRepository,
  array $data = []
    ){
............
}
public function getProductDetails($productId)
    {
        $product = $this->_productRepository->getById($productId);
        return $product;
    }

Please make ensure product collections $_product getting from widget block.

  • Thanks for the quick response. Where is $_product defined? OR Where would the value for $_product come from? – Karts Jul 10 '17 at 5:36
  • please check my updated code. it is come from widget block – Jjo Jul 10 '17 at 5:39
  • please tell if you have any issue. – Jjo Jul 10 '17 at 6:03
  • Is there no need to create the instance of the constructor before I call getProductDetails? If we need to create instance, what should be passed to the constructor? – Karts Jul 10 '17 at 6:17
0

I was able to add the catalog product to the cart with the following code from add to cart button in CMS page

        <?php
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $product = $objectManager->get('Magento\Catalog\Model\Product')->load(6109);
            $listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
            $addToCartUrl =  $listBlock->getAddToCartUrl($product);
        ?>
        <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post">
            <?php echo $block->getBlockHtml('formkey')?>
            <button type="submit"
            title="Add to Cart"
            class="action tocart primary dark">
            <h2>Add to Cart</h2>
            </button>
        </form>

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.