I have created custom field i.e details field in quote_item
and sales_order_item
table. Now I want to store custom field data in details field. How to do that?
5 Answers
Use Extension Attributes.
Extension attributes are new in Magento 2. They are used to extend functionality and often use more complex data types.
For more info,
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html http://devdocs.magento.com/guides/v2.0/extension-dev-guide/extension_attributes/adding-attributes.html
You create a new column in 'sales_quote_item' & 'sales_order_item' table and save the value corresponding to each item there.
or
You have to create 2 custom table with following fields: -
'entity_id','item_id', 'attribute_value'
I am experiencing the same problem. I added the same field to sales_order_item by adding a quote_item table custom field. It will be saved automatically. I hope I can help you.
To save quote table in etc/di.xml
<type name="Magento\Checkout\Api\PaymentInformationManagementInterface">
<plugin name="save-name" type="Your\Module\Model\Checkout\PoInformationManagementPlugin"/>
then after write to your save logic.
public function beforeSavePaymentInformation(
\Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
$cartId,
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress
) {
$additionaldata = $paymentMethod->getAdditionalData();
$netsixty_upload_path = isset($additionaldata['netsixty_upload_path']) ? $additionaldata['netsixty_upload_path'] : '';
$quote = $this->quoteRepository->getActive($cartId);
if (isset($netsixty_upload_path)) {
$quote->setNetsixtyUploadPath($netsixty_upload_path);
}
}
Then after to save sales order table in etc/event.xml
<event name="sales_order_save_before">
<observer name="save_additional_data_name" instance="Your\Module\Observer\Saveadditionaldata"/>
then after write to your save logic.
public function execute(\Magento\Framework\Event\Observer $observer)
{
// write your code
}
A little late, but I just came across your question and I think I have your solution.
First, get the quote from session:
$quote = $this->session->getQuote()
Then, get the items from the quote:
$quoteItems = $quote->getAllVisibleItems()
From here, you can call 'setData' on each item:
foreach ($quoteItems as $item) { $item->setData('custom_field', val) }
Then, update the quote with the updated items:
$quote->setItems($quoteItems)
Lastly, just save the quote (here using Magento\Quote\Api\CartRepositoryInterface):
$this->cartRepositoryInterface->save($quote)
Hope that helps. Let me know if I misunderstood or can clarify anything.