Magento 2 – How to Programmatically Add Product to Cart Multiple Times with Different Options

cartcustomitemsmagento2quote

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();

        }
    }

Best Answer

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);