2

Magento give me this error when I create order using the backend. When creting the order with frontend, it works fine. I am using multi-website functionality in my project.

I have creted custom quote and sales attributes and saved them in quote and sales_order table.

Vendor/OrderAttributes/Plugin/ShippingInformationManagementPlugin.php


<?php

namespace Vendor\OrderAttributes\Plugin;

class ShippingInformationManagementPlugin
{



    protected $_transportBuilder;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\Escaper
     */
    protected $_escaper;
    protected $quoteRepository;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    ) {
        $this->quoteRepository = $quoteRepository;
    }

    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {

        $request_body = file_get_contents('php://input');
        $delivery_details = json_decode($request_body, true);
        $quote = $this->quoteRepository->getActive($cartId);

        $delivery_date = null;
        $crew_members = null;
        $travelling_days = null;
        $port_id = null;
        $port_name = null;
        if (isset($delivery_details['delivery_details']['delivery_date'])) {
            $delivery_date = $delivery_details['delivery_details']['delivery_date'];
        }
        if (isset($delivery_details['delivery_details']['crew_members'])) {
            $crew_members = $delivery_details['delivery_details']['crew_members'];
        }
        if (isset($delivery_details['delivery_details']['travelling_days'])) {
            $travelling_days = $delivery_details['delivery_details']['travelling_days'];
        }
        if (isset($delivery_details['delivery_details']['port_id'])) {
            $port_id = $delivery_details['delivery_details']['port_id'];
        }
        if (isset($delivery_details['delivery_details']['port_name'])) {
            $port_name = $delivery_details['delivery_details']['port_name'];
        }
        $quote->setDeliveryDate($delivery_date);
        $quote->setCrewMembers($crew_members);
        $quote->setTravellingDays($travelling_days);
        $quote->setPortId($port_id);
        $quote->setPortName($port_name);
        $quote->save();
    }
}

Vendor/OrderAttributes/Observer/SaveShippingDeliveryToOrderObserver.php


<?php
/**
 * Created by PhpStorm.
 * User: eugen
 * Date: 27.11.2015
 * Time: 17:53
 */

namespace Vendor\OrderAttributes\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SaveShippingDeliveryToOrderObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectmanager
     */
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectmanager, \Magento\Quote\Model\QuoteRepository $quoteRepository, \Psr\Log\LoggerInterface $logger)
    {
        $this->_objectManager = $objectmanager;
        $this->quoteRepository = $quoteRepository;
        $this->logger = $logger;
    }

    public function execute(EventObserver $observer)
    {
        $order = $observer->getOrder();

        $quote = $this->quoteRepository->getActive($order->getQuoteId());
         $level = 'DEBUG';
        $this->logger->log($level, 'errorlog1234-delivery-details', [$quote->getDeliveryDate(),$quote->getCrewMembers(),$quote->getTravellingDays(),$quote->getPortId(),$quote->getPortName()]);

        $order->setDeliveryDate($quote->getDeliveryDate());
        $order->setCrewMembers($quote->getCrewMembers());
        $order->setTravellingDays($quote->getTravellingDays());
        $order->setPortId($quote->getPortId());
        $order->setPortName($quote->getPortName());
        $order->save();
        return $this;
    }
}

Vendor/OrderAttributes/Plugin/OrderRepositoryPlugin.php


<?php

namespace Vendor\OrderAttributes\Plugin;

use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
 * Class OrderRepositoryPlugin
 */
class OrderRepositoryPlugin
{
    /**
     * Order feedback field name
     */
    const DELIVERY_DATE = 'delivery_date';
    const CREW_MEMBERS = 'crew_members';
    const TRAVELLING_DAYS = 'travelling_days';
    const PORT_ID = 'port_id';
    const PORT_NAME = 'port_name';

    /**
     * Order Extension Attributes Factory
     *
     * @var OrderExtensionFactory
     */
    protected $extensionFactory;

    /**
     * OrderRepositoryPlugin constructor
     *
     * @param OrderExtensionFactory $extensionFactory
     */
    public function __construct(OrderExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    /**
     * Add "crew_members" extension attribute to order data object to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderInterface $order
     *
     * @return OrderInterface
     */
    public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)
    {
        $DeliveryDate = $order->getData(self::DELIVERY_DATE);
        $CrewMembers = $order->getData(self::CREW_MEMBERS);
        $TravellingDays = $order->getData(self::TRAVELLING_DAYS);
        $PortId = $order->getData(self::PORT_ID);
        $PortName = $order->getData(self::PORT_NAME);
        $extensionAttributes = $order->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setDeliveryDate($DeliveryDate);
        $extensionAttributes->setCrewMembers($CrewMembers);
        $extensionAttributes->setTravellingDays($TravellingDays);
        $extensionAttributes->setPortId($PortId);
        $extensionAttributes->setPortName($PortName);
        $order->setExtensionAttributes($extensionAttributes);

        return $order;
    }

    /**
     * Add "crew_members" extension attribute to order data object to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderSearchResultInterface $searchResult
     *
     * @return OrderSearchResultInterface
     */
    public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
    {
        $orders = $searchResult->getItems();

        foreach ($orders as &$order) {
            $DeliveryDate = $order->getData(self::DELIVERY_DATE);
            $CrewMembers = $order->getData(self::CREW_MEMBERS);
            $TravellingDays = $order->getData(self::TRAVELLING_DAYS);
            $PortId = $order->getData(self::PORT_ID);
            $PortName = $order->getData(self::PORT_NAME);
            $extensionAttributes = $order->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setDeliveryDate($DeliveryDate);
            $extensionAttributes->setCrewMembers($CrewMembers);
            $extensionAttributes->setTravellingDays($TravellingDays);
            $extensionAttributes->setPortId($PortId);
            $extensionAttributes->setPortName($PortName);
            $order->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}

Vendor/OrderAttributes/etc/extension_attributes.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="delivery_date" type="string" />
        <attribute code="crew_members" type="integer" />
        <attribute code="travelling_days" type="integer" />
        <attribute code="port_id" type="integer" />
        <attribute code="port_name" type="string" />
    </extension_attributes>
</config>

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.