3

In Magento 1 EE, I was able to get sales rule information from product list page with the following:

$rule = Mage::getModel('salesrule/rule')->load($ruleId);
$isActive = $rule->getIsActive(); 
$fromDate = $rule->getFromDate();
$toDate = $rule->getToDate();
$label = $rule->getStoreLabel();

How can I accomplish the same thing in Magento 2?

  • How about your current issue? – Khoa TruongDinh Sep 15 '16 at 13:18
  • I tried the examples today without any luck. I did find a solution, but I'm not sure if it's the best way to go about it. See below... – Mike Sep 15 '16 at 18:56
2

We can try:

/** @var \Magento\SalesRule\Model\Rule $rule **/
$rule->load($ruleId);
$isActive = $rule->getIsActive(); 
$fromDate = $rule->getFromDate();
$toDate = $rule->getToDate();
$label = $rule->getStoreLabels();

Or with API:

/** @var \Magento\SalesRule\Api\RuleRepositoryInterface $rule **/
$rule->getById($ruleId);
$isActive = $rule->getIsActive(); 
$fromDate = $rule->getFromDate();
$toDate = $rule->getToDate();
$label = $rule->getStoreLabels();

Remember to inject \Magento\SalesRule\Api\RuleRepositoryInterface or \Magento\SalesRule\Model\Rule on your constructor.

/** @var \Magento\SalesRule\Api\RuleRepositoryInterface $rule **/

protected $rule;

public function __construct(
    \Magento\SalesRule\Api\RuleRepositoryInterface $rule

) {

   $this->rule = $rule;
}
-1

This seemed to work for me, but I'm not sure it's exactly the way it should be handled or not. Open to suggestions. Thanks.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$rule = $objectManager->create('Magento\SalesRule\Model\Rule')->load($ruleId);
$isActive = $rule->getIsActive(); 
  • I'm pretty sure that my answer works well. We need to inject the sale rule model in your constructor. – Khoa TruongDinh Sep 16 '16 at 0:22
  • How do I inject the constructor? Thanks. – Mike Sep 16 '16 at 12:53
  • See my update answer. – Khoa TruongDinh Sep 24 '16 at 2:00

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.