Apply request parameters

In Magento/Adobe Commerce

To use the request parameters in Magento on navigation requests:

  • Create a custom NavigationRequest that adds the request parameter.
  • Register the new NavigationRequest to actually use the new request object.

Create a custom NavigationRequest

As an example, we'll apply the allow_customer parameter as described in Show a customer specific catalog:

<?php

namespace Vendor\Module\Model\Client\Request;

use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductNavigationRequest;
use Magento\Customer\Model\Session as CustomerSession;

class CustomProductNavigationRequest extends ProductNavigationRequest
{
    /**
     * @var CustomerSession
     */
    private $customerSession;

    /**
     * CustomProductNavigationRequest constructor.
     *
     * @param CustomerSession $customerSession
     */
    public function __construct(CustomerSession $customerSession)
    {
        $this->customerSession = $customerSession;
        $this->addAllowCustomerParameter();
    }

    /**
     * Add 'allow_customer' parameter if the customer is logged in.
     *
     * @return $this
     */
    public function addAllowCustomerParameter()
    {
        if ($this->customerSession->isLoggedIn()) {
            $userId = $this->customerSession->getCustomerId();
            $this->addHiddenParameter('allow_customer', $userId);
        }

        return $this;
    }
}

Note: the customerId should be the same as provided in the feed. More about this in Customer specific catalog.

Registration

Overwrite the regular ProductNavigationRequest with this custom navigation request.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductNavigationRequest" type="Vendor\Module\Model\Client\Request\CustomProductNavigationRequest" />
</config>