0

I tried to add a product to cart programmatically, but it should be in different line with other product in the cart.

I already tried the code below but it only adds a product to cart and adds the qty if there's same product in the cart. How can I make it to a new line rather than add the qty?

Add product code :

$cartObj = $objectManager->create('Magento\Checkout\Model\Cart');
$cartObj->addProduct($_product,$params);
$cartObj->save();
Prince Patel
  • 22,708
  • 10
  • 97
  • 119
Edwin Widhiyanto
  • 846
  • 1
  • 14
  • 40
  • Please refer the following link https://magento.stackexchange.com/questions/115929/magento2-how-to-add-a-product-into-cart-programatically-when-checkout-cart-pro – ams Nov 10 '17 at 08:38
  • I know that link help to add product to cart programmatically, but it's not help to add product to cart programmatically in new line like i describe in my question. – Edwin Widhiyanto Nov 10 '17 at 08:42

1 Answers1

2

You must add custom options to the $_product with

$customOptions = [
    'label' => 'test',
    'value' => 'value different by product (line)'
];
$_product->addCustomOption('custom_option', serialize($customOptions));

Before add product in cart

Mohamed El Mrabet
  • 1,522
  • 13
  • 23