1

I want to update the sales order right after it is inserted.

I want to reflect the change on the sales order conformation email also which is going to the customer.

So i have prepared the below code and inserted into \Magento\Sales\Model\Order\Email\Sender::checkAndSend

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $sqlx = "SELECT * FROM sales_order ORDER BY entity_id DESC LIMIT 1";
    $connection->query($sqlx);
    $resultx = $connection->fetchAll($sqlx);
    foreach($resultx as $resulx) { $entityid  = $resulx['entity_id']; }
    $sql = "SELECT * FROM sales_order_item WHERE order_id='$entityid'";
    $connection->query($sql);
    $results = $connection->fetchAll($sql);
    foreach($results as $result)
    {
        $item_id = $result['item_id'];
        $fullopt = $result['product_options'];
        $needle = 'Shoe Size';
        if (strpos($fullopt, $needle) !== false){
            $pieces = explode('Shoe Size","value":"', $fullopt);        
            $sizenum = explode('","option_id', $pieces[1]);
            if(strpos($fullopt, '"value":"EU"') !== false) {
                $rsizenum1 = $sizenum[0]+31;
                $finalque = $pieces[0].'Shoe Size","value":"'.$rsizenum1.'","option_id'.$sizenum[1];
            }
            else if(strpos($fullopt, '"value":"UK"') !== false) {
                $rsizenum2 = $sizenum[0]-1;
                $finalque = $pieces[0].'Shoe Size","value":"'.$rsizenum2.'","option_id'.$sizenum[1];
            }
            else{
                $finalque = $fullopt;
            }
            $upsql = "UPDATE sales_order_item SET product_options='$finalque' WHERE item_id='$item_id'";
            $connection->query($upsql);
        }
    }

The issue is that this dosent update the sales order emal which is goingout and also every time my order gets updated this is running again.

I only want this to run one time after order is inserted.

Anyone can give me a clue?

0

After tons of research and a good hand from a known developer, ive found out that the email takes the order info before it reaches \Magento\Sales\Model\Order\Email\Sender::checkAndSend

Thus i have put my code in \Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder::getItemOptions

This method returns all item options in array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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