How to retrieve Coupon details by Coupon code Magento 2?

Coupon code used to get a discount from the order. Different types of coupons available for the eCommerce store like, only specific Item has a discount, a discount for the whole cart and so.

If you want to get coupon data by coupon code, You can retrieve the data of coupon by its code.

You need to Retrieve coupon information using the specified search criteria with the getList() method of Coupon Repository Interface.

<?php
namespace Jesadiya\Coupon\Model;

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

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

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

    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

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

    /**
     * Get Coupon rule data
     * @return CouponInterface|null
     */
    public function getCouponDataByCode()
    {
        $couponData = null;
        $couponCode = '20%Off';
        $searchCriteria = $this->searchCriteriaBuilder->addFilter('code', $couponCode)->create();
        try {
            $couponList = $this->couponRepository->getList($searchCriteria);
            if ($couponList->getTotalCount()) {
                $couponData = $couponList->getItems();
            }

        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }
        return $couponData;
    }
}

Pass Coupon code you want to retrieve the data for the Coupon and get the value.

$coupondData = $this->getCouponDataByCode();
foreach ($couponData as $coupon) {
    $couponId = $coupon->getCouponId();
}