0

I have setup a plugin which runs after a customer is saved. This means that the plugin runs on the following situations

Frontend

  • When a customer register
  • When a customer updates their name

Backend

  • When an admin registers a customer
  • When an admin updates a customer

What I would like to know is, how can I see which one of the 4 options has triggered the plugin to run?

The reason for this is that I would like to have slightly different logic in the plugin if the admin saves a customer

1 Answer 1

0

You could always check which area the plugin is run in by asking the app state:

namespace Vendor\Module\Plugin;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\App\State as AppState;

class CustomerPlugin
{
    protected $appState;

    public function __construct(
        AppState $appState
    ) {
        $this->appState = $appState;
    }

    public function afterSave(CustomerInterface $customer, $result)
    {
        try {
            $area = $this->appState->getArea();
        } catch (\Exception $e) {
            // area has not been set, either exit or assume some other state or area
        }

        switch ($area) {
            case "adminhtml":
                // do admin stuff
                break;
            case "frontend":
                // do frontend stuff
                break;
        }

        return $result;
    }
}
1
  • Thanks for the code example - I have tweaked it so that it works as it needs to call $this->appState->getAreaCode(); and not getAreaCode(). Though the only issue is that I was hoping I would be able to see which 1 of the 4 has triggered the plugin. Is there a way to do this?
    – Goose84
    Commented Dec 19, 2017 at 7:38

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.