0

I need to save a string in a new column I created in the order table but I can not get any positive results, it does not save anything.

First I created the column:

<?php
namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Symfony\Component\Config\Definition\Exception\Exception;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\DeploymentConfig;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * @var ResourceConnection
     */
    protected $_resource;
    /**
     * @var DeploymentConfig
     */
    protected $_deploymentConfig;
    public function __construct(ResourceConnection $resource,DeploymentConfig $deploymentConfig)
    {
        $this->_resource = $resource;
        $this->_deploymentConfig = $deploymentConfig;
    }

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();

        if ($connection->tableColumnExists('sales_order', 'ahimsa_selected_shipping') === false) {
            $connection
                ->addColumn(
                    $setup->getTable('sales_order'),
                    'ahimsa_selected_shipping',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'length' => 0,
                        'comment' => ''
                    ]
                );
        }
        $installer->endSetup();
    }
}

Create the event:

<?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="sales_order_shipment_save_after">
        <observer name="sales_order_save_after" instance="\Vendor\Module\Observer\Sales\Order\SaveAfter"/>
    </event>
</config>

This event calls a observer class:

<?php

namespace Vendor\Module\Observer\Sales\Order;

use Vendor\Module\Helper\Data;
use Vendor\Module\Model\ShippingAndQuotes;
use Magento\Framework\App\RequestInterface;

class SaveAfter implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Ebizmarts\MailChimp\Helper\Data
     */
    private $_helper;
    private $_request;
    private $_quote;

    /**
     * SaveAfter constructor.
     * @param \Ebizmarts\MailChimp\Helper\Data $helper
     */
    public function __construct(
        Data $helper,
        RequestInterface $request,
        ShippingAndQuotes $quote

    ) {
        $this->_helper = $helper;
        $this->_request = $request;
        $this->_quote = $quote;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getData('data_object');
        $order_id = $order->getOrderId();
        $request  = $this->_request->getParams();
        $selected_shipping_method = $request['shipment']['selected_shipping'];
        $this->_quote->saveSelectedShippingMethod($selected_shipping_method, $order_id);
    }
}

And in the saveSelectedShippingMethod is the logic to save:

<?php
namespace Vendor\Module\Model;
use Magento\Sales\Model\Order;

class ShippingAndQuotes
{
    private $_connection;
    private $_order;

    public function __construct(
        Order $order

    )
    {
        $this->_order = $order;
    }

    public function saveSelectedShippingMethod($quote_id, $order_id)
    {
        $quote = $this->getQuoteOrder($quote_id);
        $shipping_method = $quote[0]['method_title'];
        $order = $this->_order->load($order_id);
        $order->setData('ahimsa_selected_shipping', $shipping_method);
        $order->save();
    }
}

But when I look in the table, nothing was saved!

What I'm doing wrong?

Thanks!

  • Few things to confirm- have you cleared cache after installing module ? does newly created column exist in table ? any error in error log files ? – Shashank Kumrawat Nov 1 '18 at 13:38
  • @ShashankKumrawat I executed setup:upgrade, bin/magento c:c, bin/magento c:f...and the column is in table.....no log error. – Diego Queiroz Nov 1 '18 at 13:48
  • in your execute function you are passing $selected_shipping_method as first variable in function saveSelectedShippingMethod but in observer file you are taking it as $quote_id. it seems unrelated and suspicious. can you check it? – Shashank Kumrawat Nov 1 '18 at 13:52
  • @ShashankKumrawat no error, $selected_shipping_method is a shipping quote, and is passing the id of this quote to saveSelectedShippingMethod() – Diego Queiroz Nov 1 '18 at 14:00

Your Answer

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

Browse other questions tagged or ask your own question.