Question

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.

Était-ce utile?

La solution

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

Autres conseils

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!

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

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top