We have come across an issue that I'm trying to solve. In order to overcome this bug https://github.com/magento/magento2/issues/10009 where discounted prices are not applied per customer group, I've created a module that will handle this. I have set up a database table which looks like this:
And I need to render the price on the product list and product view page. I want to use the built in price renderer but I am unable to figure out how to do this. Currently my code looks like this:
public function getCustomerGroupSpecialPrice($productId)
{
$collection = $this->customerGroupPricesCollectionFactory->create();
$collection->getSelect();
$collection->addFieldToFilter('product_id', array('eq' => $productId))
->addFieldToFilter('customer_group_id', array('eq' => $this->getCustomerGroupId()));
foreach ($collection as $item) {
$price = $item->getData('price');
}
return $price;
}
This does get the correct price but it looks like this:
and what I need is for the price to be rendered like this:
I've tried overriding the price like this:
class CustomerGroupSpecialPrice extends AbstractPrice
{
/**
* Price type identifier string
*/
const PRICE_CODE = 'customer_group_special_price';
protected $customerGroupPricesCollectionFactory;
public function __construct(
\Icansee\CustomerGroupsPrices\Model\ResourceModel\CustomerGroup\CollectionFactory $customerGroupPricesCollectionFactory
) {
$this->customerGroupPricesCollectionFactory = $customerGroupPricesCollectionFactory;
}
/**
* Get Base Price Value
*
* @return float|bool
*/
public function getValue()
{
if ($this->value === null) {
$this->value = false;
$collection = $this->customerGroupPricesCollectionFactory->create();
$collection->getSelect();
$collection->addFieldToFilter('product_id', array('eq' => $productId))
->addFieldToFilter('customer_group_id', array('eq' => $this->getCustomerGroupId()));
foreach ($collection as $item) {
$price = $item->getData('price');
}
$this->value = $price;
}
return $this->value;
}
}
but I get an error of class not found as the PRICE_CODE 'customer_group_special_price'
does not exist within Magento and so I can't do it this way. I don't really want to code in the HTML into the template to render the price correctly, as we need to render this price in multiple places, such as a quickview modal popup, product view etc. so I would need to hard code this in multiple places and that won't do. How can I render the price value correctly from my custom table?