0

I'm trying to add to the cart the simple products of the programmatically configurable product.

For now I have this script that only puts 1 item in the cart and does not update the minicart:

<?php
namespace Catalog\Product\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Data\Form\FormKey;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;
class Index extends Action
{
    protected $resultPageFactory;
    protected $formKey;   
    protected $cart;
    protected $product;
    public function __construct(
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        Context $context,
        FormKey $formKey,
        Cart $cart,
        Product $product) {
            $this->formKey = $formKey;
            $this->cart = $cart;
            $this->product = $product;     
            $this->resultPageFactory = $resultPageFactory;
            parent::__construct($context);
    }
    public function execute()
     { 
        //INSERIMENTO NEL CARRELLO PER PROD CONFIGURABILE   
        if($_POST['productType'] == 'configurable'){    
            $resultRedirect = $this->resultRedirectFactory->create();
            $prodIdColl = $this->product->load($_POST['productID']);
            $simple_collection = array('valore1','valore2','valore3');
            $count = 1;
        foreach($simple_collection as $simple_product){

            ${'prodAddId'.$count} = $_POST['prodCol'.$count.''];
            if( $_POST['qtyCol'.$count.''] &gt; 0 ){
                ${'params'.$count} = array(
                            'form_key' =&gt; $this-&gt;formKey-&gt;getFormKey(),
                            'product' =&gt; ${'prodAddId'.$count} , 
                            'qty'   =&gt; $_POST['qtyCol'.$count.'']
                        );              
                ${'productCart'.$count} = $this-&gt;product-&gt;load( ${'prodAddId'.$count} ); 
                $this-&gt;_cart-&gt;addProduct( ${'productCart'.$count} , ${'params'.$count} );

            } 

        $count++;
        }
        $this-&gt;_cart-&gt;save();
        return $resultRedirect-&gt;setPath('checkout');
    }

    //INSERIMENTO NEL CARRELLO PER PROD SEMPLICE
    if($_POST['productType'] == 'simple'){
        $resultRedirect = $this-&gt;resultRedirectFactory-&gt;create();
        $productId = $_POST['productID'];
        $params = array(
                    'form_key' =&gt; $this-&gt;formKey-&gt;getFormKey(),
                    'product' =&gt; $productId, 
                    'qty'   =&gt; $_POST['qtySimple']
                );              
        $product = $this-&gt;product-&gt;load($productId);       
        $this-&gt;_cart-&gt;addProduct($product, $params);
        $this-&gt;_cart-&gt;save();
        return $resultRedirect-&gt;setPath('checkout');
    }
 }

} ?>

How can I put all the various simple products in the cart?

Issue with my script: The problem is that this script only adds one product to the cart and not all the other simple products. The quantities instead the sums. For example, if I want to add 3 simple products to the cart, I only add the first one and add the quantities of all 3

Thank you

Jackom
  • 493
  • 7
  • 22

3 Answers3

3

I FOUND THE SOLUTION!

This is the complete code for add to cart multiple items programmatically that i write. I hope this help someone ;)

<?php
namespace Catalog\Product\Controller\Index;

use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Data\Form\FormKey; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Checkout\Model\Cart; use Magento\Catalog\Model\Product;

class AddCart extends Action { protected $resultPageFactory; /** * @var FormKey */ protected $formKey;

/**
 * @var Cart
 */
protected $cart;

/**
 * @var Product
 */
protected $product;

/**
 * Constructor.
 *
 * @param Context $context
 * @param \Magento\Checkout\Model\Session $checkoutSession
 * @param \Magento\Customer\Model\Session $customerSession
 * @param FormKey $formKey
 * @param Cart $cart
 * @param Product $product
 */
public function __construct(
    Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    FormKey $formKey,
    Cart $cart,
    Product $product
) {
    $this-&gt;formKey = $formKey;
    $this-&gt;cart = $cart;
    $this-&gt;product = $product;
    $this-&gt;resultPageFactory = $resultPageFactory;
    $this-&gt;_resultFactory = $context-&gt;getResultFactory();
    parent::__construct($context);
}

public function execute()
{
        $_product = $this-&gt;product-&gt;load($_POST['productID']);

        //INSERIMENTO NEL CARRELLO PER PROD CONFIGURABILE
        if($_POST['productType'] == 'configurable'){    
            $resultRedirect = $this-&gt;resultRedirectFactory-&gt;create();
            $simple_collection = $_product-&gt;getTypeInstance()-&gt;getUsedProducts($_product);
            $count = 1;

            //AGGIUNTA NEL CARRELLO DEI COLORI
            foreach($simple_collection as $simple_product){

                if($_POST[&quot;qtyCol&quot;.$count.&quot;&quot;] &gt; 0){
                    ${'params'.$count.''} = $_POST[&quot;qtyCol&quot;.$count.&quot;&quot;];
                    ${'product'.$count.''} = $_POST[&quot;prodCol&quot;.$count.&quot;&quot;];
                    $this-&gt;cart-&gt;addProduct( ${'product'.$count.''} , ${'params'.$count.''} );
                }

                $count++;
            }


            $this-&gt;cart-&gt;save();

            return $resultRedirect-&gt;setPath('checkout');
        }

        //INSERIMENTO NEL CARRELLO PER PROD SEMPLICE
        if($_POST['productType'] == 'simple'){
            $resultRedirect = $this-&gt;resultRedirectFactory-&gt;create();

            //AGGIUNTA NEL CARRELLO PROD SINGOLO
            $params = $_POST['qtySimple'];              
            $product = $_POST['productID'];  

            $this-&gt;cart-&gt;addProduct($product, $params);


            $this-&gt;cart-&gt;save();
            return $resultRedirect-&gt;setPath('checkout');
        }

}

}

Jackom
  • 493
  • 7
  • 22
1

For Minicart update, You need to add action URL in sections.xml file like below ex.

Create a sections file app/code/Vendor/Module/etc/frontend/sections.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="cartupdate/items/add">
        <section name="cart"/>
    </action>
</config>

Update your action url with this cartupdate/items/add

Banna Online
  • 1,059
  • 2
  • 15
  • 33
  • Ok, thanks man! And for add multiple products can you help me? – Jackom Jul 24 '20 at 13:02
  • What is the issue that come with your script in multiple products adding in cart? – Banna Online Jul 27 '20 at 04:38
  • The problem is that this script only adds one product to the cart and not all the other simple products. The quantities instead the sums. For example, if I want to add 3 simple products to the cart, I only add the first one and add the quantities of all 3... – Jackom Jul 27 '20 at 07:03
  • Try this link https://magento.stackexchange.com/a/258688/13832 – Banna Online Jul 28 '20 at 04:14
  • Thanks but I tried this code in this link but i have error "cannot use the create function an object" – Jackom Jul 28 '20 at 07:18
0

I was looking for issue as describe @Jackom And I found some one. If you want to add 2 simples with one configurable parrent use object with clone on addProduct method

$this->_cart->addProduct(clone $product, $params);
ilia plat
  • 1
  • 1