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

}
}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!