0

i have created a custom module in magento 2. In which i want to show some banners, Countdown timer and extra details on product page, also label or text on category page. for this i extended the catalog sales rule module. Added some extra field according to my requirements. Now i want to show get all those products ids on which sale rule has applied with all other details like Top Banner, Countdown timer according to sale etc.

My Block Code :

<?php
namespace Custom\Sales\Block;

class Flash extends \Magento\Framework\View\Element\Template
{

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory $ruleFactory,
    array $data = []
) {
    $this->_ruleFactory = $ruleFactory;
    parent::__construct($context, $data);
}


public function getCatalogeRuleId()
{
    $catalogRuleCollection = $this->_ruleFactory->create()
        ->addFieldToFilter('is_active',1);
//        return $catalogRuleCollection;
    $resultProductIds = [];
    foreach ($catalogRuleCollection as $catalogRule) {
        $productIdsAccToRule = $catalogRule->getMatchingProductIds();
//            echo json_encode($productIdsAccToRule);exit;
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();

        foreach ($productIdsAccToRule as $productId => $ruleProductArray)
        {
            if (!empty($ruleProductArray[$websiteId])) {
                $resultProductIds[$productId] = $catalogRule->getData();
            }
        }
        return $resultProductIds;
    }
}
}

Now when i print my array i get only one sale rule data , however i have created 3 different sales enter image description here

enter image description here

Your Answer

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

Browse other questions tagged or ask your own question.