1

I have a custom value (In a Magento 2.3.3 installation )that gets added to the quote_item table when I add an item to the basket. When I go to checkout I want to pass that value to the same-named column in the sales_order_item table. I am using the event sales_model_service_quote_submit_before to copy my quote data to the sales order table but for some reason, the data to be inserted into the sales_order_item table won't store. Below is the code I am using.

foreach($order->getItems() as $item) { $quoteItem = $quote->getItemById($item->getQuoteItemId()); if ($quoteItem) { if($quoteItem->hasData('deposit')){ $item->setData('deposit', $quoteItem->getDeposit()); } } } 

If I debug straight after the contents of $item then I get the value but it is not storing in the table.

1

2 Answers 2

1
foreach($order->getItems() as $item) { $quoteItem = $quote->getItemById($item->getQuoteItemId()); if ($quoteItem) { if($quoteItem->hasData('deposit')){ $item->setData('deposit', $quoteItem->getDeposit()); } $item->save(); } 

Try this code, It may help you!!

3
  • I thought I could do that to but I get the error:"success":false,"response":null,"error_message":"Something went wrong while generating the Sage Pay request: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Commented Feb 17, 2020 at 15:13
  • @MartinPeasley, can you state your complete problem, I hope above code doesn't make any issue Commented Feb 18, 2020 at 5:56
  • So using the event sales_model_service_quote_submit_before I am saving a custom value that gets stored in the quote_item table. This correctly adds to the corresponding column in the sales_order table. If I also want to save data to a custom column in the sales_order_item table the value doesnt get saved. To save data to sales_order_item do you have to use extension attributes as I didn't need to to add data to a custom column in the sales_order table? Commented Feb 18, 2020 at 9:06
0

So it looks like the issue was that I needed to use extension_attributes to save a custom value to the sales_order_item table.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.