Get orderId from quote_item
Question
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.
Solution 2
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!
OTHER TIPS
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
.