use Magento\Framework\App\ResourceConnection;
$rule = $this->ruleFactory->create()->load($ruleId); $coupons = $rule->getCoupons(); // Returns an array of coupon objects Use code with caution. Copied to clipboard 4. Create Coupons Programmatically For bulk generation or custom automation, use \Magento\SalesRule\Model\CouponGenerator Basic Rule Creation: RuleInterfaceFactory magento 2 get coupon code programmatically
$quote = $quoteFactory->create()->load($quoteId); return $quote->getCouponCode(); $rule = $this->ruleFactory->create()->load($ruleId)
Useful for generating a dropdown of all existing coupon codes in admin or API. $coupons = $rule->getCoupons()
Retrieving coupon codes programmatically in Magento 2 is a routine but powerful task. Whether you need to display an applied coupon to a customer, export order data to an ERP, or validate coupons from an external system, Magento 2 provides clean, object-oriented ways to fetch this data.