0

I am creating a plugin for Quotemanagement (placeOrder) with after method, but some reason it’s not triggering. I try with die but no luck

Please help, I am using Magento 2.2.6

Here is my code

namespace Vendor\Module\Plugin;
class QuoteManagement {
protected $logger;
protected $quoteFactory;
protected $orderFactory;

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

}
/**
 * Before order success action
 */
public function afterPlaceOrder($quoteManagement, $result){
    $order  =   $this->orderFactory->create()->load($result);
    $quote  =   $this->quoteFactory->create()->load($order->getQuoteId());
    $addvalue =   $quote->getAdcodeValue();
    $order->setAdcodeValue($quote->getAdcodeValue());
    $order->save();

    return $result;
}

}

also here is my di.xml file

 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Action\Action">
    <plugin name="Module_Name::urlstringcheck" type="Vendor\modulename\Plugin\ActionUrlValueCheck" />
</type>
<type name="Magento\Quote\Model\QuoteManagement">
    <plugin name="Module_Name::checktheorder" type="Vendor\modulename\Plugin\QuoteManagement" />
</type>

4
  • 1
    what is the location of di.xml file? Nov 15, 2018 at 3:38
  • @RamkishanSuthar - /etc/frontend/di.xml also i tried in /etc/di.xml
    – GeorgeM
    Nov 15, 2018 at 12:45
  • Please keep your module name's first character in upper case Nov 15, 2018 at 13:06
  • @RamkishanSuthar - My module name start with upper case
    – GeorgeM
    Nov 15, 2018 at 14:42

2 Answers 2

0

Using checkout_submit_all_after event to handle after checkout

<?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="checkout_submit_all_after">
        <observer name="Vendor_Module::checkout_after_do_something" instance="Vendor\Module\Observer\SaveOrderAfterSubmitObserver" />
    </event>
</config>

The Observer

<?php

namespace Vendor\Module\Observer;

class SaveOrderAfterSubmitObserver implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /* @var $order \Magento\Sales\Model\Order */
        $order = $observer->getEvent()->getOrder();
        /* @var $quote \Magento\Quote\Model\Quote */
        $quote = $observer->getEvent()->getQuote();

        // Do crazy stuff here

        return $this;

    }

}
1
  • I tried observer first and it's not working that's why i try to write plugin
    – GeorgeM
    Nov 15, 2018 at 12:48
0

Update your method with below code.

di.xml

<type name="Magento\Quote\Model\QuoteManagement">
    <plugin name="Module_Name::checktheorder" type="Vendor\Modulename\Plugin\QuoteManagement" />
</type>

QuoteManagement.php

public function afterPlaceOrder(\Magento\Quote\Model\QuoteManagement $quoteManagement, $result){
    $order  =   $this->orderFactory->create()->load($result);
    $quote  =   $this->quoteFactory->create()->load($order->getQuoteId());
    $addvalue =   $quote->getAdcodeValue();
    $order->setAdcodeValue($quote->getAdcodeValue());
    $order->save();

    return $result;
}

I hope it will work for you

5
  • Thanks for the help and I tried your method and not working but I have two plugin and is this cause the issue?
    – GeorgeM
    Nov 15, 2018 at 15:04
  • I think thats not any issue. Make sure your di.xml is in global scope. Nov 15, 2018 at 15:59
  • - you mean etc/di.xml correct, if yes i did that also. i am using M2.2.6
    – GeorgeM
    Nov 15, 2018 at 16:16
  • Everything is fine. I hope you already verified that your module is enabled. Nov 15, 2018 at 16:40
  • yes module is enabled
    – GeorgeM
    Nov 15, 2018 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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