0

I have made 2 custom columns named selected_delivery_date and selected_delivery_time in quote_item and sales_order tables. I successfully added the values of these 2 columns in quote_item table using a controller as follows:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
        $itemsCollection = $cart->getQuote()->getItemsCollection();
        $itemsVisible = $cart->getQuote()->getAllVisibleItems();
        $items = $cart->getQuote()->getAllItems();

    foreach($items as $item) {
        $item->setData('selected_delivery_date', $selected_day);
        $item->setData('selected_delivery_time', $selected_time);
        $item->save();
    }

To save the data to my custom columns in sales_order table, I made an observer but I do not know how to save data to my columns. This is my observer code:

public function execute(\Magento\Framework\Event\Observer $observer){

    $order = $observer->getEvent()->getOrder();
    $orderId = $order->getId();

    $QuoteId = $order->getQuoteId(); /*Quote Id*/

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($QuoteId);
    $logger->info($orderId);
}

It successfully writes the quoteId and orderId to the log file. I just want to save the data to my columns.

This is the events.xml file I am using.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="mymodule_controller_success_action" instance="A4\CustomDelivery\Observer\GetQuoteId" />
    </event>
</config>

1 Answer 1

1

Please specify events which are you use for modify data, this will help with understand the context and correct suggestion.

Doesn't make sense to use quote item for set the same data for all items in the same quote and convert this data to order. You should use quote instead of quote item.

If you want to save only one or few attributes for order you can use following logic

/** @var $order \Magento\Sales\Model\Order */
/** @var $resource \Magento\Sales\Model\ResourceModel\Order */
$order->setData('some_attribute', 'value');
$resource->saveAttribute($order, 'some_attribute');
// or
$resource->saveAttribute($order, ['some_attribute', 'another_attribute']);

For handle order place you can use plugin like described in https://magento.stackexchange.com/a/356524/91782

5
  • Can I get the data from the same way as well? For example order->getData('some_attribute')? Aug 16, 2022 at 12:45
  • 1
    Sure, you can use getData or getSomeAttribute() Aug 16, 2022 at 14:35
  • Thankyou it works! Aug 17, 2022 at 5:27
  • Instead of using $resource->saveAttribute($order, ['some_attribute', 'another_attribute']); I found a better way which is to just save the details using $order->save() Aug 17, 2022 at 5:39
  • 1
    $order->save()calls a lot of additional logic on save, saveAttribute allows to save one or few attributes in fast way without call all save logic. Aug 17, 2022 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.