1

I am facing a problem to save custom field value from the quote table to sales order table. I have the custom field in quote table and sales order table, but the value is not saving in the sales order table. I am using observer with “sales_order_place_before” and here is my code

namespace Tristar\AffiliateCode\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteFactory;
class Placeorder implements ObserverInterface
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $_logger;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $quoteFactory;

    /**
     * Constructor
     *
     * @param \Psr\Log\LoggerInterface $logger
     */

    public function __construct(\Psr\Log\LoggerInterface $logger,
                               \Magento\Quote\Model\QuoteFactory $quoteFactory) {
        $this->_logger = $logger;
        $this->quoteFactory =   $quoteFactory;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getOrder();
        $quoteId = $order->getQuoteId();
        $quote  =   $this->quoteFactory->create()->load($quoteId);
        $affileatevalue =   $quote->getAffiliateCode();        
        $order->setAffiliateCode($quote->getAffiliateCode());
        $order->save();
    }
}

Here is my event.xml file and the file location is "etc/frontend/"

 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
    <observer name="tristar_affiliate_code" instance="Tristar\AffiliateCode\Observer\AddToCartAfter"/>
</event>
<event name="sales_model_service_quote_submit_before">
    <observer name="tristar_affiliate_code_order_place_before_action" instance="Tristar\AffiliateCode\Observer\PlaceOrder"/>
</event>

  • is it event.xml or events.xml? if it is event.xml then rename to events.xml – Rakesh Varma Nov 5 '18 at 12:47
  • It's events.xml – GeorgeM Nov 5 '18 at 14:36
  • do as @amitebera suggest – Rakesh Varma Nov 5 '18 at 14:48
  • change your class name from Placeorder to PlaceOrder or change class name in xml file. – Rakesh Varma Nov 5 '18 at 14:53
  • @RakeshVarma - I already change the class name to PlaceOrder. what you meant by change the class name in xml file ? – GeorgeM Nov 5 '18 at 14:57
1

instead of sales_order_place_before event use sales_model_service_quote_submit_before event and below code in your observer

public function execute(Observer $observer)
    {
        $order = $observer->getOrder();
        $quoteId = $order->getQuoteId();
        $quote  =   $this->quoteFactory->create()->load($quoteId);
        $affileatevalue =   $quote->getAffiliateCode();        
        $order->setAffiliateCode($quote->getAffiliateCode());
    }
| improve this answer | |
  • I tried this but still not working – GeorgeM Nov 5 '18 at 1:50
  • Check if you are getting value from quote object or for testing set any value in it . If possible add your events.xml file location and XML code in question – Rakesh Varma Nov 5 '18 at 4:27
  • I added the events.xml and location to the question – GeorgeM Nov 5 '18 at 12:37
  • Looks like this is a bug in Magento 2.2.6 because the same event is triggering in 2.2.4 – GeorgeM Nov 12 '18 at 16:40
0

You have defined the sales_model_service_quote_submit_before at frontend area in eventts.xml.

You have to defined at global scope. Means you have to create events.xml at app\code\{Vendorname}\{Modulename}\etc\ as Magento checkout page is Magento api means Rest API area.

| improve this answer | |
  • I tried your method but no luck, i tried with "die" to check my observer is firing or not and it's not. I am not sure what i am missing. – GeorgeM Nov 5 '18 at 14:44
  • can you please help – GeorgeM Nov 6 '18 at 21:23

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.