How to get Shopping cart Coupon data by id Magento 2?

Magento 2, Get Shopping cart rule data by the coupon id to retrieve information of coupon code, the coupon used details, rule id and usage limit of the coupon.

Coupon details are saved in the salesrule_coupon table of the database.

Coupon code is the actual code which user will add from the Cart or Checkout page to get a discount.

rule_id is the parent id of the sales rule table. You can retrieve the details of coupons using Coupon Repository Interface.

<?php
namespace Jesadiya\RuleIds\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\SalesRule\Api\Data\CouponInterface;
use Magento\SalesRule\Api\CouponRepositoryInterface;

class CouponData
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var CouponRepositoryInterface
     */
    private $couponRepository;

    public function __construct(
        LoggerInterface $logger,
        CouponRepositoryInterface $couponRepository
    ) {
        $this->logger = $logger;
        $this->couponRepository = $couponRepository;
    }

    /**
     * Get Coupon rule data
     * @return CouponInterface|null
     */
    public function getCouponDataById()
    {
        $shoppingCartRuleData = null;
        try {
            $couponId = 10;
            $shoppingCartRuleData = $this->couponRepository->getById($couponId);
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }

        return $shoppingCartRuleData;
    }
}

Use the getById() method of the Magento\SalesRule\Api\CouponRepositoryInterface interface to retrieve the details of Coupon code.

You need to pass Coupon id to fetch data of the specific coupon.

Output: if coupon id is correct,

array (size=6)
  'coupon_id' => string '10' (length=2)
  'rule_id' => string '43' (length=2)
  'code' => string '15OFF' (length=5)
  'times_used' => string '5' (length=1)
  'is_primary' => string '1' (length=1)
  'type' => string '0' (length=1)

Coupon id is not found, the result will be null.