How to add quote Item id as new field?
-
06-10-2020 - |
Question
I have a new field varient_id
in sales_flat_quote_item
and sales_flat_order_item
table.
I want to save item_id
as varient_id in sales_flat_quote_item
and sales_flat_order_item
table. ie,quote item_id
as quote varient_id
and order item_id
as order item_id
.
How can I do it?
Solution
Beste way i think is creating a observer on the sales_model_service_quote_submit_after
action and there manipulate the database to process the data needed. hope this helps.
In config.xml
<sales_model_service_quote_submit_after>
<observers>
<set_itemid>
<type>singleton</type>
<class>Company_Module_Model_Observer</class>
<method>setVariantId</method>
</set_itemid>
</observers>
</sales_model_service_quote_submit_after>
In Observer
public function setVariantId($observer)
{
$order = $observer->getEvent()->getOrder();
$quote = $observer->getEvent()->getQuote();
$orderId = $order->getId();
$orderItems = $order->getAllItems();
foreach($orderItems as $item){
$parentId = 0;
$orderItemCollection = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('quote_item_id', $item->getVariantId())
->addFieldToFilter('order_id', $orderId);
foreach($orderItemCollection as $col){
$parentId = $col->getItemId();
}
if($parentId){
$item->setVariantId((int)$parentId);
$item->save();
}
}
}
OTHER TIPS
I have a solution here for Magento 2. I am sharing this for basic Idea. You can create an observer in 1.9 for sales_model_service_quote_submit_after
in your XML file.
<events>
<sales_model_service_quote_submit_after>
<observers>
<onepagecheckout>
<type>model</type>
<class>onepagecheckout/observer</class>
<method>saveData</method>
</onepagecheckout>
</observers>
</sales_model_service_quote_submit_after>
</events>
In observer file you can update your item ID. Here is the code that we need to add observer file.
protected $_checkoutSession;
public function __construct ( \Magento\Checkout\Model\Session $_checkoutSession ) {
$this->_checkoutSession = $_checkoutSession;
}
public function saveData(\Magento\Framework\Event\Observer $observer)
{
$cartData = $this->_checkoutSession->getQuote()->getAllVisibleItems();
foreach( $cartData as $item ):
/* Here we can manipulate data */
endforeach;
$cartData->save();
}
for manipulate database and to process data you need block or model files. you also need to initialize resource, then you need to call connection. I hope it will give you an idea to save varient_id
. Also here is a link can help you that How to manipulate db tables helplink1 helplink2