In Magento/Adobe Commerce
Before starting, follow the Customer specific catalog guide to add a allow_customer parameter.
To use the parameter in Magento:
- Create a custom NavigationRequest that adds the request parameter.
- Register the new NavigationRequest to actually use the new request object.
Create a custom NavigationRequest
If logged in, the allow_customer request parameter will be automatically added.
<?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>
Notes
- This is just for the navigation requests, the same can be done for the ProductSearchRequest.