2

I have coupon rule collection created at admin side and displaying rule names in the system configuration of the custom module. Actually, I wanted to get coupon code of a cart rule that has been selected by admin in system configuration and display it on success page for customer to use it in next order. Getting Rules Coupon Code Collection by this:

<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;

class Coupon extends Template
{    

protected $_salesRuleCoupon;

public function __construct(
    Context $context,        
    \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $salesRuleCoupon
)
{    
    $this->_salesRuleCoupon = $salesRuleCoupon;
}

 public function getCouponsList()
{
   $collection= $this->_salesRuleCoupon->create();
   return $collection->getData();
} 

}

and in template:

$coupons=$block->getCouponsList();
foreach($coupons as $coupon){
echo $coupon['code'];
}

Rules Collection in admin side (System Configuration) is retrieved by doing this:

<?php
namespace Vendor\Module\Model\Config;

class CouponCode implements \Magento\Framework\Option\ArrayInterface
{
/**
 * @return array
 */
public function toOptionArray()
{

    $om = \Magento\Framework\App\ObjectManager::getInstance();

    $arr = [];

    $couponCollection = $om->create('Magento\SalesRule\Model\Rule')->getCollection();

    foreach ($couponCollection as $rule) {
    $arr[] = ["value" => $rule->getId(), "label" => __( $rule->getName() ) ];
}

return $arr;

}
}

calling it in source model as:

<field id="coupon_code" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use Coupon Code Rule</label>

<source_model>Vendor\Module\Model\Config\CouponCode</source_model>
<depends>
         <field id="display">1</field>
</depends> 
</field>

when I retrieve the configuration value of a selected Rule in the custom module, it displays its ID as in Cart Price Rules Grid in the backend. All I need is to get Coupon Codes through their Cart Price Rules' ID and display only those rules in the system configuration that have coupon codes and display the coupon code of selected Rule from system configuration on success page.

Any help is highly needed and will be positively appreciated!

  • can you add screen shot for it "any cart rule that has been selected by admin in system configuration" ? i am little confuse – Ansar Husain Sep 5 '18 at 6:43
1

you can get coupon code with following code

namespace QaisarSatti\Module\Block;

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

  protected $rule;  


  public function __construct(

        \Magento\SalesRule\Model\RuleFactory $rule

    ) {


        $this->rule = $rule;

    }
    public function getCouponCode()
    {
        $ruleId = 7;
        $couponCodeData = $this->rule->create()->load($ruleId);
        echo $couponCodeData->getCouponCode();
    }
 }

Reference

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.