We want to display customer specific prices on all product listings and product pages on the front end.
So far what we've done is created a collection plugin for the resource model which fetches a price based on the customers id.
namespace Vendor\Module\Plugin\Catalog\Model\ResourceModel\Product;
// some imports
class CollectionPlugin
{
protected $_customerSession;
protected $_customerFactory;
protected $_productPriceFactory;
protected $_objectManager;
protected $_storeManager;
public function __construct(
CustomerFactory $customerFactory,
ProductPriceFactory $productPriceFactory,
StoreManagerInterface $storeManager,
Session $customerSession
) {
$this->_customerFactory = $customerFactory;
$this->_productPriceFactory = $productPriceFactory;
$this->_customerSession = $customerSession;
$this->_storeManager = $storeManager;
}
public function afterLoad(Collection $subject, $result, $printQuery = false, $logQuery = false)
{
$items = $subject->getData();
$this->fetchCustomerPrice($items);
return $result;
}
private function fetchCustomerPrice($items) {
// some code
}
}
This works fine, however now we are stuck with the problem of caching. The price for the customer who visits a product page first gets shown to everyone.
We've tried following the instructions here: How Magento manages store-wise cache. For our purposes we use customer id instead of zip code. The PageCache Identifier plugin seems to work or at least it's run each time the page loads regardless of caching. Identity plugin does not work as its value gets cached the same way product prices do.
We think that the problem lies with the block and/or model cache, but we are not sure.
How should we continue?