I want to add a product with two line items.
Say for example if i add Product A with qty of 4.
I need to split cart into two line items.
3 qty as one line item and 1 is in another.
How Can I achieve this?
I have overridden Checkout/Controller/Cart/Add.php
and here is my sample code.
foreach ([3,1] as $qty) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_control = $objectManager->create('\Vendor\Module\Observer\Custom');
$additionalOptions = [];
if($value == 1){
$additionalOptions[] = array(
'label' => 'Discount',
'value' => 'No'
);
}else{
$additionalOptions[] = array(
'label' => 'Discount',
'value' => 'Yes'
);
}
$product->addCustomOption('additional_options',$_control->unserializeCustomOption($additionalOptions));
$v_params = array(
'product' => $product->getId(),
'qty' => $qty,
'price' => ($qty == 3 ? 76 : 86)
);
$this->cart->addProduct($product, $v_params);
}
Request which is coming to Quote model is correct only.
{"product":"727","qty":3,"price":76}
{"product":"727","qty":1,"price":86}
But in the cart its not correct.
Thanks in advance!
addProduct
method in classMagento\Quote\Model\Quote
– Anshu Mishra Jan 24 '19 at 10:05