I want to know how to add the link to add the product to the card.
I get all my product from magento, but I only display "add to card" but without link.
On my template, I display several products, but internet links that I've found show only how to add url to one product, if I'm right (such as : Magento 2: How to get add to cart url in custom templete file and add product to cart)
EDIT : i'm using magento 2.2.4. I'm using a custom template and I want to add the button inside a table of product, in the last of a
This is my block :
public function getProductsCollection(){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
// $appState->setAreaCode('frontend');
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
//$collection->setPageSize(3); // selecting only 3 products
$productListe = array();
foreach ($collection as $product) {
$productListe[] = $product->getData();
}
return $productListe;
}
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);
}
And my view :
$cadencier = $this->getClientCadencier($customerCode);
//$listProducts = $this->getProductsCollection();
$_productCollection = $this->getProductCollection();
foreach($cadencier as $article){
...
?>
<tr class="ligne">
<th scope="row"><input type="checkbox" title="checkboxes[]"></th>
<td>Image</td>
<td><?php echo $numArticle; ?></td>
<td><?php echo $numArticle; ?></td>
<td><?php echo $dateLast; ?></td>
<td><?php echo $derniereQuantite; ?></td>
<td>Unité commandé</td>
<td><input type="number" title="Quantité à commander" step="1" name="quantite[]" min="0" value="<?php echo $derniereQuantite; ?>" ></td>
<td>
<!-- --><?php //if($is_salable){ ?>
<!-- <form data-role="tocart-form" action="--><?php //echo $addToCartUrl; ?><!--" method="post">
<?php //echo $block->getBlockHtml('formkey')?>
<div class="btn">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</div>
</form>-->
<?php $postParams = $block->getAddToCartPostParams($article); ?>
<?php echo $article->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
// }else{
// echo "Produit indisponible";
// }; ?>
</td>
</tr>
I'm getting some info of a webservice. When I get the infos, I have an error of type ($cadencier contains array) and I can't go to the page (because of the exception).
When I'm using getProductCollection(), I can add to cart but my function for my webservice isn't working with getProductCollection. When I try to display the returned value of getProductCollection, I have an Out of memory error.