Magento 2 Get Coupon Code Programmatically Direct

public function fromQuote(int $quoteId): ?string

If a customer applied SAVE20 , the method returns "SAVE20" . If none applied, returns null . magento 2 get coupon code programmatically

return (bool) $this->getAppliedCouponCode(); public function fromQuote(int $quoteId):

/** * Get applied coupon code for a given quote ID * * @param int $quoteId * @return string returns null . return (bool) $this-&gt

You can call these endpoints using cURL or any HTTP client.

There are several scenarios where you might need to get a coupon code programmatically in Magento 2. For example:

use Magento\Checkout\Model\Session as CheckoutSession; // ... Inside your class ... public function getCurrentAppliedCoupon() $quote = $this->checkoutSession->getQuote(); return $quote->getCouponCode(); // Returns the string code Use code with caution.