Hello Friends,
In this blog, I will explain to you how you can pass any custom data in your created custom field during add to cart or before place an order process.
When we add an item to the cart that will be stored into the quote object. If you want to pass any value which is accessible to admin as order object then you must need to convert your quote field to order so you can able to access that value using order object.
As eCommerce business development improving very much nowadays, I can say as Magento developer in India. Every developer should know these steps.
Quick Tip:- Create a UI Component Without Database Collection
Before starting below code, just make sure you have created “testfield” name column on the two tables: quote & sales_order
I will just show you very easy simple steps, please check.
Step 1: First, create extension attribute file in the following location
app/code/[Namespace]/[Module]/etc/extension_attributes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\CartInterface">
<attribute code="testfield" type="string" />
</extension_attributes>
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
<attribute code="testfield" type="string" />
</extension_attributes>
</config>
Using the above code we create testfield named extension attribute for cart and order objects.
Step 2: Now, create fieldset.xml file in the following file
app/code/[Namespace]/[Module]/etc/fieldset.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
<fieldset id="sales_convert_quote">
<field name="testfield">
<aspect name="to_order" />
</field>
</fieldset>
</scope>
</config>
Step 3: Now, create observer events.xml file in the following file
app/code/[Namespace]/[Module]/etc/events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_model_service_quote_submit_before">
<observer name="[Namespace]_[Module]_sales_model_service_quote_submit_before" instance="[Namespace]\[Module]\Observer\TestfieldObserver" />
</event>
</config>
Step 4: Just create the TestfieldObserver.php file in the following file
app/code/[Namespace]/[Module]/Observer/TestfieldObserver.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class SaveOrderBeforeSalesModelQuoteObserver implements ObserverInterface
{
protected $objectCopyService;
public function __construct(
\Magento\Framework\DataObject\Copy $objectCopyService,
) {
$this->objectCopyService = $objectCopyService;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getData('order');
$quote = $observer->getEvent()->getData('quote');
$this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
return $this;
}
}
Now, just RUN the below command:
- php bin/magento setup:di:compile
- php bin/magento c:f
As you can see in the above code, we have finally converted a quote to order one custom field which is useful in the order object.
Now, you can set any value on “testfield” during add to cart or when placing the order so once you get the data in quote object then it will be automatically set into the order object so you can easily access that field value on order object.
Thanks for reading this post!