cancel
Showing results for 
Search instead for 
Did you mean: 

Adding 2 virtual products based on the same configurable product with different options

Adding 2 virtual products based on the same configurable product with different options

I'm creating order programmatically. I'm creating two different orders. The first one is sended to an external API and the second one is saved in Magento.

I have this problem :

If I'm having twice the same virtual product but with different option, my order is merging all the product with the same configurable product.

See the code at this address (the accepted answer) :

 

https://magento.stackexchange.com/questions/171786/how-to-programmatically-place-order-with-custom-o...

 

Same code but change this function into this :

 

    public function getCartItems()
    {
        $cart = $this->_objectManager->get('\Magento\Checkout\Model\Cart');

        // retrieve quote items collection
        $itemsCollection = $cart->getQuote()->getItemsCollection();

        // get array of all items what can be display directly
        $itemsVisible = $cart->getQuote()->getAllVisibleItems();

        // retrieve quote items array
        $items = $cart->getQuote()->getAllVisibleItems();
        $itemsInCart = array();
        foreach($items as $item) {
            $data = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
            echo "<pre>".print_r($data, true)."</pre>";
//            echo "<pre>".print_r($data["info_buyRequest"]["options"], true)."</pre>";
            $options = $data["info_buyRequest"]["options"];
            $super_attribute = $data["info_buyRequest"]["super_attribute"];
            $itemsInCart[] = array(
                'product_id' => $item->getProductId(),
                'qty' => $item->getQty(),
                'options' => $options,
                'super_attribute' => $super_attribute,
            );

        }
        die;
        return $itemsInCart;
    }

See the order I received (1 product * quantity total instead of 3 distincts products)

 

Capture d’écran 2019-07-19 à 11.12.33.png

 

EDIT : problably because I have the same base product ID, see this, it's a dump of my 3 products.

Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovL3BvcnRhaWxjbGllbnQueGFwaWVtYS5vcmcvZmx5ZXItMTUtMjFjbS10ZXN0Lmh0bWw,
[product] => 1746
[selected_configurable_option] =>
[related_product] =>
[super_attribute] => Array
(
[236] => 319
)

[options] => Array
(
[56] => Array
(
[0] => 328
)

[58] => 337
[57] => 334
[61] =>
)

[qty] => 500
)

[options] => Array
(
[0] => Array
(
[label] => Logo(s)
[value] => Aucun
[print_value] => Aucun
[option_id] => 56
[option_type] => multiple
[option_value] => 328
[custom_view] =>
)

[1] => Array
(
[label] => Votre offre
[value] => Silver
[print_value] => Silver
[option_id] => 58
[option_type] => drop_down
[option_value] => 337
[custom_view] =>
)

[2] => Array
(
[label] => Quantité
[value] => 500
[print_value] => 500
[option_id] => 57
[option_type] => drop_down
[option_value] => 334
[custom_view] =>
)

)

[attributes_info] => Array
(
[0] => Array
(
[label] => Ambiance
[value] => Ambiance illustrée
[option_id] => 236
[option_value] => 319
)

)

[simple_name] => flyer 15*21cm test-Ambiance illustrée
[simple_sku] => 34306m-Ambiance illustrée
[product_calculations] => 1
[shipment_type] => 0
)

Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovL3BvcnRhaWxjbGllbnQueGFwaWVtYS5vcmcvZmx5ZXItMTUtMjFjbS10ZXN0Lmh0bWw,
[product] => 1746
[selected_configurable_option] =>
[related_product] =>
[super_attribute] => Array
(
[236] => 319
)

[options] => Array
(
[56] => Array
(
[0] => 328
)

[58] => 347
[57] => 334
[61] =>
)

[qty] => 500
)

[options] => Array
(
[0] => Array
(
[label] => Logo(s)
[value] => Aucun
[print_value] => Aucun
[option_id] => 56
[option_type] => multiple
[option_value] => 328
[custom_view] =>
)

[1] => Array
(
[label] => Votre offre
[value] => Premium
[print_value] => Premium
[option_id] => 58
[option_type] => drop_down
[option_value] => 347
[custom_view] =>
)

[2] => Array
(
[label] => Quantité
[value] => 500
[print_value] => 500
[option_id] => 57
[option_type] => drop_down
[option_value] => 334
[custom_view] =>
)

)

[attributes_info] => Array
(
[0] => Array
(
[label] => Ambiance
[value] => Ambiance illustrée
[option_id] => 236
[option_value] => 319
)

)

[simple_name] => flyer 15*21cm test-Ambiance illustrée
[simple_sku] => 34306m-Ambiance illustrée
[product_calculations] => 1
[shipment_type] => 0
)

Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovL3BvcnRhaWxjbGllbnQueGFwaWVtYS5vcmcvZmx5ZXItMTUtMjFjbS10ZXN0Lmh0bWw,
[product] => 1746
[selected_configurable_option] =>
[related_product] =>
[super_attribute] => Array
(
[236] => 320
)

[options] => Array
(
[56] => Array
(
[0] => 328
)

[58] => 337
[57] => 334
[61] =>
)

[qty] => 500
)

[options] => Array
(
[0] => Array
(
[label] => Logo(s)
[value] => Aucun
[print_value] => Aucun
[option_id] => 56
[option_type] => multiple
[option_value] => 328
[custom_view] =>
)

[1] => Array
(
[label] => Votre offre
[value] => Silver
[print_value] => Silver
[option_id] => 58
[option_type] => drop_down
[option_value] => 337
[custom_view] =>
)

[2] => Array
(
[label] => Quantité
[value] => 500
[print_value] => 500
[option_id] => 57
[option_type] => drop_down
[option_value] => 334
[custom_view] =>
)

)

[attributes_info] => Array
(
[0] => Array
(
[label] => Ambiance
[value] => Ambiance traditionnelle
[option_id] => 236
[option_value] => 320
)

)

[simple_name] => flyer 15*21cm test-Ambiance traditionnelle
[simple_sku] => 34306m-Ambiance traditionnelle
[product_calculations] => 1
[shipment_type] => 0
)