Magento 2 Get Coupon Code Programmatically (8K 2024)

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

public function __construct(CollectionFactory $couponCollectionFactory) magento 2 get coupon code programmatically

return $result; Useful for generating a dropdown of all existing coupon codes in admin or API. $result = $this-&gt

public function execute()

public function getAppliedCouponForItem($itemId) $couponCode = $this-&gt

private CartRepositoryInterface $quoteRepository; private OrderRepositoryInterface $orderRepository;