Magento 2 get all coupon code programmatically
Today we talk about how Magento 2 get all coupon code programmatically. You can get coupon data from the coupon Model.So let start with our example.
namespace QaisarSatti/HelloWorld/Block;
use Magento\Backend\App\Action;
class GetQuote extends Action
{
protected $coupon;
public function __construct(
\Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $coupon,
)
{
$this->coupon = $coupon;
}
public function getAllCoupon()
{
$couponCollection = $this->coupon->create();
foreach($couponCollection as $couponData)
{
echo $couponData->getCode();
}
}
}
use Magento\Backend\App\Action;
class GetQuote extends Action
{
protected $coupon;
public function __construct(
\Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $coupon,
)
{
$this->coupon = $coupon;
}
public function getAllCoupon()
{
$couponCollection = $this->coupon->create();
foreach($couponCollection as $couponData)
{
echo $couponData->getCode();
}
}
}