0
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.

enter image description here

Thanks in advance!

| improve this question | | | | |
  • Check addProduct method in class Magento\Quote\Model\Quote – Anshu Mishra Jan 24 '19 at 10:05
  • @Anshu Mishra Yeah, Seen.! Magento is restricting to create new item with same product. – Sainadh Mannem Jan 24 '19 at 10:11
0

You can achieve this via beforeAddProduct plugin of Magento\Quote\Model\Quote where you can assign addCustomOption to $product->addCustomOption('additional_options', $additionalInfo); where $additionalInfo is array like below

$additionalInfo[] = [
 'label' => 'Label you want to show',
 'value' => 'Value you want to show'
];  

on product add time check qty is greater than 3. then you can create new product with custom option so it will work as you want.

| improve this answer | | | | |
  • Thanks for quick reply! I have updated my question with my sample code what i have tried so far. Please check once and let me know. – Sainadh Mannem Jan 24 '19 at 10:03

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.