0

I have an instance of \Magento\Quote\Model\Quote\Item $item

I need to get the order ID of the order in which that particular item has been purchased.

For full disclosure, I am using a plugin function called afterConvert, which operates around the function "convert" in

Magento\Quote\Model\Quote\Item\ToOrderItem

Anyway, the table for quote_item doesn't seem to have the order_id in it.

Any idea how I could go about it?

public function aroundConvert(
    \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
    \Closure $proceed,
    \Magento\Quote\Model\Quote\Item $item,
    $additional = []
) {
    $order = null;

    $item->getQuote();

    $options = $this->configurationHelper->getOptions($item);

    foreach ($options as $option){
        if($option['label'] == 'Clarity'){
            $clarity = $option['value'];
        }
        if($option['label'] == 'Color'){
            $color = $option['value'];
        }
        if($option['label'] == 'Shape'){
            $shape = $option['value'];
        }
        if($option['label'] == 'Carat'){
            $carat = $option['value'];
        }
    }
    /** @var \Magento\ConfigurableProduct\Api\Data\ConfigurableItemOptionValueInterface $option */

    $qrcode = $this->qrcodeFactory->create();
    $orderID = $item->getOrderId();

But $item->getOrderId doesn't work.

2 Answers 2

0

The quote is an order that it has not been passed yet, so since it has not been passed yet, you can't get his order ID.

If you want to get the order id of some product from the quote, it's not like this, you have instead to get all orders, then in this all orders you get them products, afterthat you compare if the orders contain one of your current quote product_id, so you get his order_id or order_id's.

1
  • Yep, that's it. Problem was "orderId" didn't exist yet!
    – Luca D.R.
    Commented Nov 5, 2018 at 16:54
0

I got this.

The trick is to use this event:

sales_model_service_quote_submit_success

This way you are sure that:

A) The order actually went through

B) The orderId exists, because the event fires after

$order = $this->orderManagement->place($order);

in the function

protected function submitQuote(QuoteEntity $quote, $orderData = [])

that you can find here

Magento\Quote\Model\QuoteManagement

So I used an Observer instead of a Plugin!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.