0

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 my code :

 public function createMageOrder($orderData) {
        $store=$this->_storeManager->getStore();
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        $customerTemp=$this->customerFactory->create();
        $customerTemp->setWebsiteId($websiteId);
        $customerTemp->loadByEmail($orderData['email']);// load customet by email address
        $quote=$this->quote->create(); //Create object of quote
        $quote->setStore($store); //set store for which you create quote
        $customer= $this->customerRepository->getById($customerTemp->getEntityId());
        $quote->setCurrency();
        $quote->assignCustomer($customer); //Assign quote to customer
        $objectManager = ObjectManager::getInstance();
        $codeClient = $orderData["code_client"];
        //add items in quote
        $comment = $orderData["comment"];
        $i = 0;
        foreach($orderData['items'] as $item){
            $this->separator();
            echo $i;
            $this->separator();
            $parentId = "";
            $parent = null;
            $productTemp = $this->getProduct($item['product_id']);
            echo "PRODUCT NAME : ".$productTemp->getName();
            $qty = $item["qty"];
            $addLot = ($item["realQty"] != $qty) ? " - Lot de ".$item["realQty"] : "";
            $hasBroderie  = $item["hasBroderie"];
            if($hasBroderie){
//                $qty = $qty/$qty ;
                $addLot = ($item["realQty"] != $qty) ? " - Lot de ".$item["realQty"] : "";
            }

            $optionProd = $item["options_prod"];
            $temp = [];

            if(!empty($optionProd)){
                $opts = [];
                foreach ($optionProd as $va) {
                    foreach ($va as $k => $v) {
                        $opts[$k] = $v;
                    }
                }
                $temp = array(
                    "qty" => $qty,
                    "options" => $opts,
                );

                if($item["prodOPT"] != ""){
                    $temp["options"] = $item["prodOPT"];
                }

                $temp["product_id"] = $productTemp->getId();
                $buyRequest = new \Magento\Framework\DataObject();
                if($item["parentId"] != $item['product_id']){
                    echo "<br>ok condition<br>";
                    var_dump($item["parentId"]);
                    $productTemp = $this->getProduct($item["parentId"]);
                    $temp["super_attribute"] = $item['super_attribute'];
                    $productTemp->setSku($item["sku"]);
                }
                $i++;

//                continue;
//
                $name = $productTemp->getName().$addLot;
//                echo "<br>nom : $name <br> ";
                $productTemp->setName($name);
//
                if($parentId != "") $temp["product_id"] = $productTemp->getId();
//                echo "<br>";
//                continue;
                var_dump($productTemp->getName());
                $buyRequest->setData($temp);
                $quote->addProduct($productTemp,$buyRequest);
            }
            else{
                echo "ok condition 2";
                $productTemp->setName($productTemp->getName().$addLot);
                $quote->addProduct(
                    $productTemp,
                    $qty);
            }
//            unset($product);

        }

        $temp = $this->productRepository->get("000000000001");
        $quote->addProduct($temp, 1);
        //Set Address to quote
        $quote->getShippingAddress()->addData($orderData['shipping_address']);
        $billings = $quote->getBillingAddress()->addData($orderData['shipping_address']);
        // Collect Rates and Set Shipping & Payment Method
        $shippingAddress=$quote->getShippingAddress();
        $shippingAddress->setCollectShippingRates(true)->collectShippingRates();
        $this->shippingRate->setCode('flatrate_flatrate');
        $shippingAddress->setShippingMethod('flatrate_flatrate'); //shipping method
        $quote->getShippingAddress()->addShippingRate($this->shippingRate);
        $quote->setCouponCode("")->collectTotals();
        $this->quoteRepository->save($quote);
        $quote->getPayment()->importData(['method' => 'checkmo']);
        // Collect Totals & Save Quote
        $quote->collectTotals();
        $this->quoteRepository->save($quote);
        // Create Order From Quote
        $order = $this->quoteManagement->submit($quote);
        $state = $order->getState();
        $order->setState($state);
        $order->setStatus('en_cours_de_traitement');
        $name = $customer->getFirstname() . " " . $customer->getLastname();


        if($comment != ""){
            $order->addStatusHistoryComment($comment)->setIsCustomerNotified(false)->setEntityName('order');
        }
        $this->orderRepository->save($order);

If I dump the result I have this :

    ---------------
0
---------------
PRODUCT NAME : flyer 15*21cm test-Ambiance illustrate
OK condition
string(4) "1746" string(32) "Vos Flyers 15*21cm - Lot de 500"
---------------
1
---------------
PRODUCT NAME : flyer 15*21cm test-Ambiance illustrate
OK condition
string(4) "1746" string(45) "Vos Flyers 15*21cm - Lot de 500 - Lot de 500"
---------------
2
---------------
PRODUCT NAME : flyer 15*21cm test-Ambiance traditional
OK condition
string(4) "1746" string(58) "Vos Flyers 15*21cm - Lot de 500 - Lot de 500 - Lot de 500"
---------------
3
---------------
PRODUCT NAME : Polo + Tablier (Homme)string(22) "Polo + Tablier (Homme)" 

And this is my order (and the last product is not showing) and the product 0,1,2 are based on the same configurable product (all the product are virtual products) :

enter image description here

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
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.