I am trying to add a Add to cart button in a custom block being added to the homepage.
I've looked at many SO / MSE posts and all refer to Object Manager but I know it's not meant to be used, and from the look of what's been said I can see it being removed a few versions down the line, or at least changed in a way where all the code being suggested will break.
The closest thing I've found to get a working button is:
<?php $collection = $this->getCollection(); ?> <?php $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper'); ?> <?php $postData = $postDataHelper->getPostData($block->getAddToCartUrl($prod), ['product' => $prod->getEntityId()]) ?>
but it returns a JS error in console:
TypeError: params.data is undefined params.data.form_key = formKey;
This makes me think, It expects few things to be in a form? So I guess my real question is, what is the Magento 2 equivalent of
<?php echo $this->getAddToCartUrl($_product); ?>