How to set custom attribute value in m2epro extension once order create in Magento admin Magento2

magento.stackexchange https://magento.stackexchange.com/questions/278314

Вопрос

I am using M2epro for Amazon and eBay. We want to set custom attribute value(static) once order creates in Magento admin. Can anyone have any idea how we can do it?

Это было полезно?

Решение

Here is the solution to add a custom attribute in M2epro order.

Once the order has been placed in Magento using M2epro, it will trigger one event ess_order_place_success after order success in which you will get order object. You can create an observer using that event.

Below is the code for the event.xml

<?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="ess_order_place_success">
        <observer name="package_module_after" instance="PackageName\ModuleName\Observer\CreateOrderAfterObserver" />
    </event>
</config>

and create an observer file name with CreateOrderAfterObserver.php

<?php
namespace PackageName\ModuleName\Observer;

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

class CreateOrderAfterObserver implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        try {
                $order = $observer->getEvent()->getOrder()->getMagentoOrder();
                // do what ever you want to do
        } catch (\Exception $e) {
             echo $e->getMessage();
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top