1

I have created a plugin so that I can process the order and send to a 3rd party after it has been placed. I have hooked into the afterSave functionality and my plugin code looks like this

di.xml

<type name="Magento\Sales\Api\OrderRepositoryInterface">
    <plugin name="Module_OrderAfterSave" type="Vendor\Module\Plugin\OrderAfterSave" sortOrder="10" disabled="false" />
</type>

OrderAfterSave.php

namespace Vendor\Module\Plugin;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Framework\ObjectManagerInterface;

class OrderAfterSave
{
    protected $objectManager;
    protected $orderRepositoryInterface;

    public function __construct(
        ObjectManagerInterface $objectManager,
        OrderRepositoryInterface $orderRepositoryInterface
    ) {
        $this->objectManager = $objectManager;
        $this->orderRepositoryInterface = $orderRepositoryInterface;
    }

    public function afterSave($order)
    {
        //Insert into database
        $ordersModel = $this->objectManager->create('Vendor\Module\Model\Orders');
        $ordersModel->setSentTo3rdParty(1);
        $ordersModel->save();

        return $order;
    }
}

The issue I have is that when the customer places an order they are redirected back to the basket page instead of a success page.

When I check in the admin I can see the order has gone through and when I check the custom database table I can see its been updated.

There are no errors in any of the logs so not really sure why customers never see the order success page. Could anyone advise?

2 Answers 2

4

Try this:

public function afterSave(\Magento\Sales\Api\OrderRepositoryInterface $orderRepo, $order)
    {
        //Insert into database
        $ordersModel = $this->objectManager->create('Vendor\Module\Model\Orders');
        $ordersModel->setSentTo3rdPary(1);
        $ordersModel->save();

        return $order;
    }
3
0

It is not sure that the OrderRepositoryInterface will be used to save an order. I have exprienced a problem when some piece of code, even from the Magento core, was saving the order directly through Magento\Sales\Model\ResourceModel\Order class instead of OrderRepositoryInterface. I solved the issue creating an afterSave plugin on the ResourceModel class.

Vendor\Module\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <type name="Magento\Sales\Model\ResourceModel\Order">
            <plugin name="after_place_order_save" type="Vendor\Module\Plugin\Order"/>
        </type>
</config>

Vendor\Module\Plugin\Order.php

<?php
namespace Vendor\Module\Plugin;


class Order
{
    public function afterSave(
        \Magento\Sales\Model\ResourceModel\Order $subject,
                                                 $result,
        \Magento\Framework\Model\AbstractModel $object
    ) {
        // do stuff 
        return $result;
    }
}

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.