3

Following is the event observer for adding multiple products into cart:

<?php

namespace Namespace\Module\Observer;

class ThirdPartyAdd implements \Magento\Framework\Event\ObserverInterface {

    public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Checkout\Model\Cart $cart, \Magento\Catalog\Model\Product $product, \Magento\Quote\Model\QuoteFactory $quote
    ) {
        $this->_storeManager = $storeManager;
        $this->_cart = $cart;
        $this->_product = $product;
        $this->quote = $quote;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {


        if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
            for ($i = 0; $i < sizeof($_POST['prod_id']); $i++) {

                $qty = $_POST['prod_qty'][$i];

                $product = $this->_product->load($_POST['prod_id'][$i]);

                $product_type = $product->getTypeID();

                if($product_type == 'configurable') {
                    $simple_products = $product->getTypeInstance()->getUsedProducts($product); //get all simple products
                    $attributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product); //get all possible attributes

                    foreach ($simple_products as $simple_product) {
                        $super_attributes = array();
                        foreach ($attributes as $attribute) {
                            $text = $simple_product->getAttributeText('size');
                            //echo $text;
                            $t1 = 'Black';
                            $t2 = 'M';
                            foreach ($attribute['values'] as $value) {
                            $super_attributes[$attribute['attribute_id']] = $value['value_index'];
                                if ($value['label'] == $t1) {
                                    $super_attributes[$attribute['attribute_id']] = $value['value_index'];
                                }
                                if ($value['label'] == $t2) {
                                    $super_attributes[$attribute['attribute_id']] = $value['value_index'];
                                }
                            }
                        }
                    }
                    $this->_cart->addProduct($product, array(
                        'qty' => $_POST['prod_qty'][$i],
                        'super_attribute' => $super_attributes
                    ));
                    $this->_cart->save();
                }
            }
        }
    }

}

When above code is executed and cart is empty only one product is added to cart with summation of quantity for all the product.

For eg if I add: Prod 1 - 2 quantity Prod 2 - 1 quantity

Cart will contain prod 1 with 3 quantity.

This only happens if cart is empty unless cart is empty code will work as expected.

Please let me know what I am missing here.

  • Product quantity in the minicart is not updated. Did you find a solution for it? – vedu Mar 2 '18 at 14:21
0

Since you are running in loop new instance of product is not created so you are getting the old product instance again which increase the qty

Get new product instance every time.

$productModel = $this->_product->create();
$product = $productModel->load($_POST['prod_id'][$i]);
  • Getting error Invalid method Magento\Catalog\Model\Product\Interceptor::create when tried the above code. – DEEP JOSHI Oct 11 '17 at 10:43
  • 1
    Here I am able to add product to cart but in mini cart quantity of first product is displayed instead of total quantity. – DEEP JOSHI Oct 17 '17 at 11:33
  • Here product quantity in mini cart is not updated. – DEEP JOSHI Nov 24 '17 at 13:25
0

You have to use ProductFactory to load product.

protected $product;

public function __construct(
\Magento\Framework\App\Action\Context $context, \Magento\Framework\Data\Form\FormKey $formKey, \Magento\Checkout\Model\Cart $cart, \Magento\Catalog\Model\ProductFactory $product) {
    $this->formKey = $formKey;
    $this->cart = $cart;
    $this->product = $product;
}

public function postSaveAndFinalize($param) {
    $params[0] = array(
        'form_key' => $this->formKey->getFormKey(),
        'product' => 2,
        'qty' => 1
    );
    $params[1] = array(
        'form_key' => $this->formKey->getFormKey(),
        'product' => 1,
        'qty' => 1
    );

    foreach ($params as $product) {
        //Load the product based on productID
        $productModel = $this->product->create();
        $productModel->load($product['product']);
        $this->cart->addProduct($productModel, $product);
    }
    $this->cart->save();
}

In this way you can add multiple products into cart.

0

You'd place cart save() out of products iteration

Example

foreach ($products as $product){
    // ...
    $this->_cart->addProduct(...)
}
$this->_cart->save();

Source: https://magento.stackexchange.com/a/232665/3566

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.