I have a module, that output's products with discounts (discounts provided by catalog price rules).
How get each product (\Magento\Catalog\Model\Product
) discount end date? What should I inject into constructor?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have a module, that output's products with discounts (discounts provided by catalog price rules).
How get each product (\Magento\Catalog\Model\Product
) discount end date? What should I inject into constructor?
I used the same approach, that was suggested in the comment.
How to get the product collection based on catalog & cart price rules?
Here is code of my block.
protected $rules;
protected $dateHelper;
//...
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\CatalogRule\Model\Rule $rules,
\Magento\Framework\Stdlib\DateTime\DateTime $dateHelper,
//...
array $data = []
) {
$this->rules = $rules;
$this->dateHelper = $dateHelper;
//...
return parent::__construct($context, $data);
}
/**
* Get passed product discount end date at the passed format
*
* @param \Magento\Catalog\Model\Product $product
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Zend_Db_Statement_Exception
*/
public function getProductDiscountEndDate($product, $format = 'd F')
{
foreach ($this->rules->getResourceCollection() as $rule) {
/**
* @var \Magento\CatalogRule\Model\Rule $rule
*/
if ($rule->getMatchingProductIds()[$product->getId()][1]) {
{
return $this->dateHelper->date($format, $rule->getToDate());
}
}
}
return '';
}