Magento 2 Get Coupon Code Programmatically Online

public function __construct( CartRepositoryInterface $quoteRepository, CustomerSession $customerSession, CheckoutSession $checkoutSession ) $this->quoteRepository = $quoteRepository; $this->customerSession = $customerSession; $this->checkoutSession = $checkoutSession;

public function execute()

<?php namespace YourNamespace\YourModule\Controller\Index; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\Controller\Result\JsonFactory; use YourNamespace\YourModule\Model\GetCouponCodeService; magento 2 get coupon code programmatically

try $quote = $this->quoteRepository->get($quoteId); return $quote->getCouponCode(); catch (NoSuchEntityException $e) return null;

public function __construct(CollectionFactory $couponCollectionFactory) CheckoutSession $checkoutSession ) $this-&gt

public function getAppliedCouponForItem($itemId)

use Magento\Quote\Api\CartRepositoryInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; quoteRepository = $quoteRepository

public function fromCurrentSession( \Magento\Checkout\Model\Session $checkoutSession, \Magento\Customer\Model\Session $customerSession ): ?string if ($customerSession->isLoggedIn()) $quote = $this->quoteRepository->getActiveForCustomer($customerSession->getCustomerId()); else $quote = $checkoutSession->getQuote(); return $quote->getCouponCode();

public function getAllCoupons(): array

class GetCouponFromQuote

class GetCouponCodeService

magento 2 get coupon code programmatically

Need online bookings on your site? Try Book24Seven free — no card, no commitment.

X
Cookie settings
magento 2 get coupon code programmatically

Cookies allow us to better serve you. We collect information about the use of our website. You can manage your cookie settings below.

Essential cookies

Essential cookies are necessary for the functioning and development of our website. They do not store information that could immediately identify you.

Visitor measurement and analytics

We use the Google Analytics cookies to track visitor behavior and measure our website performance.