5

I am trying to get coupon code created in cart price rule from backend on success page. I am doing it as: In Block file

public function getCouponCollection() {

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

    $array = [];

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

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

return $array;

}

and with Magento\SalesRule\Model\RuleRepository injected in constructor

/** @var \Magento\SalesRule\Model\RuleRepository $ruleRepository **/

public function getCouponByRuleId ($id) {

    $rule = $this->ruleRepository->getById($id);

    $rule->getDescription();
    $rule->getCondition();
}

In template file, I have:

<?php $a=$block->getCouponCollection();


if (!empty($a)) {
foreach ($a as $v) {
    $rule = $block->getCouponByRuleId($v["value"]);
    if ($rule) {
        echo $rule->getCouponCode();
        }
    }
}
?>

and it is not working, what needs to be added/corrected here..any help should be much appreciated.

  • 1
    what you want to achive want to get list of coupon which in generate at admin or want to get current order coupon cocde? – Amit Bera Sep 4 '18 at 7:24
  • I have coupon rule collection created at admin side and displaying rule names in system configuration of 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. – Ajwad Syed Sep 4 '18 at 17:00
2

question is not clear about coupon code. but after reading the comment it make clear you want to get coupon code based on rule id.

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

|improve this answer|||||
3

TL;DR

Get the order collection on success page and filter it by the recent order increment id and get the coupon code of the order e.g.

$order->getCouponCode();

Good Luck!

|improve this answer|||||
2

Create File Coupon.php in app/code/Vendor/Module/Block

<?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();
    } 

}

2. Call above block function in your module template file

$coupons=$block->getCouponsList();
foreach($coupons as $coupon){
    echo $coupon['code'];
}
|improve this answer|||||
  • well it worked accept there is syntax error of comma ( , ) in constructor. I have coupon rule collection created at admin side and displaying rule names in system configuration of custom module. Actually I wanted to get coupon code of a cart rule that has been selected by admin in system configuration. By above provided solution I am getting all coupon codes on my success page. – Ajwad Syed Sep 4 '18 at 16:55
1

If you want to get coupon collection which is created at admin then try below solution:

protected $couponRepository;

protected $_searchCriteriaBuilder;

public function __construct(
    \Magento\SalesRule\Api\CouponRepositoryInterface $couponRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
    $this->couponRepository = $couponRepository;
    $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
}

public function getCouponList()
{
    $searchCriteria = $this->_searchCriteriaBuilder->create();
    $searchResults = $this->couponRepository->getList($searchCriteria);
    $salesrules = $searchResults->getItems();
    foreach($salesrules as $eachCouponrules)
    {
        $coupon->getCode();
        $coupon->getExpirationDate();
    }

}

This solution only is given coupon code

|improve this answer|||||

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.