Magento 2 Get Coupon Code Programmatically Apr 2026

public function __construct(CollectionFactory $couponCollectionFactory)

$collection = $this->couponCollectionFactory->create(); $collection->addFieldToSelect(['code', 'usage_limit', 'times_used', 'expiration_date']); $coupons = []; foreach ($collection as $coupon) $coupons[] = [ 'code' => $coupon->getCode(), 'usage_limit' => $coupon->getUsageLimit(), 'times_used' => $coupon->getTimesUsed(), 'expires_at' => $coupon->getExpirationDate() ]; return $coupons;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $quoteItem = $objectManager->create(\Magento\Quote\Model\Quote\Item::class)->load($itemId); $quote = $quoteItem->getQuote(); return $quote->getCouponCode(); // Same as quote-level coupon magento 2 get coupon code programmatically

$collection = $orderCollectionFactory->create() ->addFieldToFilter('entity_id', ['in' => $orderIds]) ->addFieldToSelect(['entity_id', 'coupon_code']);

$result = $this->resultJsonFactory->create(); $couponCode = $this->couponService->fromCurrentSession(); return $result->setData([ 'success' => true, 'coupon_code' => $couponCode ?: 'No coupon applied' ]); $coupons = []

<?php namespace YourNamespace\YourModule\Model; use Magento\Sales\Api\OrderRepositoryInterface;

class GetCouponFromOrder

public function __construct( CartRepositoryInterface $quoteRepository, OrderRepositoryInterface $orderRepository ) $this->quoteRepository = $quoteRepository; $this->orderRepository = $orderRepository;

public function fromOrder(int $orderId): ?string $quoteItem = $objectManager-&gt