0

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.

  • That is hard to give an answer without much information. Could you please give more infor about that? Which Magento version do you use? Where do you try to display add to card button (category, produc, cart ... ) What did you try so far in which template ? – Tiikeri Jul 3 '18 at 11:22
  • Yes sorry, please see my updated post – Morgan Tartreau Jul 3 '18 at 11:32
  • HI, in which page you have try that? could you give the name of template that you overwrite it? – Tiikeri Jul 3 '18 at 11:36
  • My theme extends luma, but the page is a custom page so it doesn't override any a template – Morgan Tartreau Jul 3 '18 at 11:50
0

There are several things in your code which I see may need to improve :).

First, Magento encourage to use dependence injection, so using Object manager is not best practice.

Secondly, you can follow this link to get product collection and loop through it so that you will have multiple product instead of one.

https://www.mageplaza.com/how-get-product-collection-magento-2.html

Third thing is, if you check that file magento/module-catalog/Block/Product/AbstractProduct.php I believed it should has a function like getAddToCartUrl($product) (2.1.9) so you can use that also in the loop to get right url.

Hope that help :), feel free to reach me if you need something more about that.

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.