Magento 2 Get Coupon Code Programmatically Jun 2026

To check if a coupon is valid for a specific quote without actually applying it, use Magento\SalesRule\Model\Coupon and Magento\SalesRule\Model\Validator .

public function getCouponCodeByOrderId(int $orderId): ?string magento 2 get coupon code programmatically

If you need to find the coupon currently applied to a user's session or a specific quote, use the Magento\Checkout\Model\Session object directly Using Checkout Session: // Inject \Magento\Checkout\Model\Session $checkoutSession To check if a coupon is valid for

Q: How do I get a list of all coupon codes in Magento 2? A: You can use the Magento\SalesRule\Api\CouponRepositoryInterface interface to retrieve a list of all coupon codes. magento 2 get coupon code programmatically

public function fromOrder(int $orderId): ?string

To check if a coupon is valid for a specific quote without actually applying it, use Magento\SalesRule\Model\Coupon and Magento\SalesRule\Model\Validator .

public function getCouponCodeByOrderId(int $orderId): ?string

If you need to find the coupon currently applied to a user's session or a specific quote, use the Magento\Checkout\Model\Session object directly Using Checkout Session: // Inject \Magento\Checkout\Model\Session $checkoutSession

Q: How do I get a list of all coupon codes in Magento 2? A: You can use the Magento\SalesRule\Api\CouponRepositoryInterface interface to retrieve a list of all coupon codes.

public function fromOrder(int $orderId): ?string