3

How to add multiple items (same product ID) with different options to cart ?

Following code is working for one product or different products.

But, with two items of same product ID, with different options, only one appears in cart with quantity = 2 and the options of first one...

Following function is executed on a custom form submit :

namespace Vendor\Module\Controller\Post;
use Magento\Framework\App\Action\Action;
class Checkout extends Action {

protected $_cart;
protected $_productRepositoryInterface;

public function __construct(
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,

) {
    $this->_cart = $cart;
    $this->_productRepositoryInterface = $productRepositoryInterface;
}

...

public function execute()
{
   $post = $this->getRequest()->getPostValue();
        if ($post) {
            unset($post['form_key']);

            $contacts = array();
            foreach ($post as $key => $values) {
                foreach($values as $id => $value) {
                    $contacts[$id][$key] = $value;
                }
            }

            foreach($contacts as $contact) {
                $productSku = "abcd";
                $_product = $this->_productRepository->get($productSku);

                $lastNameOptionId    = $this->getOptionIdByTitle('last_name', $_product);
                $firstNameOptionId   = $this->getOptionIdByTitle('first_name', $_product);
                $customOptionId     = $this->getOptionIdByTitle('custom', $_product);

                $customOptionTypeId = $this->getOptionTypeId('custom', $_product);

                $params = array (
                    //'form_key' => $this->formKey->getFormKey(),
                    'product' => $_product->getId(),
                    'qty' => 1,
                    'price' => $_product->getPrice(),
                    'options' => array(
                        $lastNameOptionId => $contact['last_name'],
                        $firstNameOptionId => $contact['first_name']
                    )
                );


                if(isset($contact['custom']))
                    $params['options'][$customOptionId] = $customOptionTypeId;


                $this->_cart->addProduct($_product, $params);
            }
            $this->_cart->save();

        }
    }
  • 1
    I couldn't reproduce this issue. I tried adding two of a product but with different custom options and it results in them being displayed separately in the cart (not on one line as you say). This is in 2.1.4 – Aaron Allen Mar 17 '17 at 9:49
  • Damnit, THANKS... Tried too, it's working, I'm doing it wrong, I'm trying programmatically, see my updated question... – DependencyHell Mar 17 '17 at 14:17
4

Somewhere in Magento 2 code is the answer :

/**
* We need to reload product in this place, because products
* with the same id may have different sets of order attributes.
*/

It means that when I used this function two times:

$_product = $this->_productRepository->get($productSku);

the product was 'cached' and served a second time, and not re-loaded!

We then need to use $forceReload = true:

function get($sku, $editMode = false, $storeId = null, $forceReload = false);

:

$_product = $this->_productRepository->get($productSku, false, $storeId, true);
0

Here I am using the example of color and size attributes.

protected $_cart;
protected $productRepository;

public function __construct(
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
)
{
    $this->_cart = $cart;
    $this->productRepository = $productRepository;
}

...
...

$productId = 10; // enter your product_id
$qty = 1; // enter number of quantites you want to add

/*
I am using below ids as configurable options for example purpose only.
You have to use your attribute id and option id.

90 = attribute_id of color 
53 = option_id of any specific color,

143 = attribute_id of size
170 = option_id of any specific size
*/

$options = array(
                90 => 53,
                143 => 170
            );

$params = array(
    'product' => $productId,
    'super_attribute' => $options,
    'qty' => $qty
);

$_product = $this->productRepository->getById($productId);
$this->_cart->addProduct($_product,$params);
$this->_cart->save();

Note: Programatically added product to cart will not be visible in minicart. Check your added products in shopping cart page. If you want to update your minicart after adding product programatically, Please visit below link

https://webkul.com/blog/update-cart-adding-product-programmatically-magento2/

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.