1

Is it possible to access an observed methods arguments in an after[Method]() plugin? For example, I want to write an after plug in for:

namespace Magento\Some_Core\Model\Example;
class Test {
  public function usualMethod(\Magento\Sales\Model\Order $order) {
    // get order information and attach to $this
    return $this;
  }
}

I have my Custom Module's di.xml set up and my plug in class as:

namespace Vendor\Module\Plugin\Example;
class TestPlugin {
  public function afterUsualMethod(\Magento\Some_Core\Model\Example\Test $subject, $return) {
    $order = $subject->getArguments(0); // ??
    $result->setAdditionalOrderInfo($order->getIAlsoNeedThis());
    return $result;
 }
}

Is there any way to access the $order object passed in the original method? I can easily extend the original class and call parent::usualMethod() but wanted to know if it was possible to use a plugin for this scenario.

1 Answer 1

2

Try following way:


namespace Vendor\Module\Plugin\Example;
class TestPlugin {
    public function afterUsualMethod(\Magento\Some_Core\Model\Example\Test $subject, $result, $order) {
        $result->setAdditionalOrderInfo($order->getIAlsoNeedThis());
        return $result;
    }
}

More detail

1
  • Worked like a charm! thx!
    – dbcn
    Commented May 9, 2018 at 18:58

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.