Magento 2 Get Coupon Code Programmatically Online

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; public function getCouponCodesFromOrders(CollectionFactory $orderCollectionFactory, array $orderIds)

<?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

use Magento\Quote\Api\CartRepositoryInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; array $orderIds) &lt

class GetCoupon implements HttpGetActionInterface ?php namespace YourNamespace\YourModule\Controller\Index

public function __construct(OrderRepositoryInterface $orderRepository)

try $order = $this->orderRepository->get($orderId); return $order->getCouponCode(); catch (NoSuchEntityException $e) return null;

public function __construct( GetCouponCodeService $couponService, JsonFactory $resultJsonFactory ) $this->couponService = $couponService; $this->resultJsonFactory = $resultJsonFactory;