0

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?

1

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 '';
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.