Pregunta

I have created an observer listening to the event controller_action_predispatch on a clean Magento 2.4.2 instance, and I tried to get the module name and full action name from within the observer. It worked well for all pages except the product pages.

For example, for the registration page:

$observer->getEvent()->getRequest()->getModuleName() // it gives "customer"
$observer->getEvent()->getRequest()->getFullActionName() // it gives "customer_account_create"

and for shopping cart page:

$observer->getEvent()->getRequest()->getModuleName() // it gives "checkout"
$observer->getEvent()->getRequest()->getFullActionName() // it gives "checkout_cart_index"

but when I tried to capture the above info for the product pages

$observer->getEvent()->getRequest()->getModuleName() // it gives ""
$observer->getEvent()->getRequest()->getFullActionName() // it gives "__" (2 underscores)
¿Fue útil?

Solución

By the looks of it, the pages that work with your approach are pages which are never cached, however the product page is and the request is processed differently (as confirmed by you that the data is properly retrieved when you disable FPC).

I found in the Magento core the idea of using a plugin that could help you. You will basically attach a plugin on the class that processes the request relative to page cache. So check this snippet from vendor/magento/module-page-cache/etc/di.xml where this plugin is registered:

<type name="Magento\Framework\App\PageCache\Cache">
    <plugin name="fpc-type-plugin" type="Magento\PageCache\Model\App\PageCachePlugin"/>
</type>

You could create a plugin of your own and inside it, add a beforeLoad() method relative to Magento\Framework\App\PageCache\Cache's load() method. There you will probably to check with a debugger which is the data you can access and perform your logic based on the page/ URL you are requesting.

Otros consejos

Try this

public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->_request = $request;
}

 $this->_request->getFullActionName()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top