How to Get Applied rule ids of the item from the Order Magento 2?

You can get applied rule ids of the product item from the Sales Order by order item id.

A customer has applied the shopping cart rule for the order and if any item contains that specific rules, after place order, the sales item persists the shopping cart rule id in the sales_order_item database table for the item level.

applied_rule_ids field from the sales order item table persists all the applicable shopping cart rules for the order.

Lets example, you have applied the discount from the cart page, Discount code will be “15Off”, code will be applied to the entire cart and when you place the order, each item of the order contains the id of the rule of 15Off.

<?php
namespace Jesadiya\RuleIds\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;

class GetAppliedRuleId
{
    /**
     * @var OrderItemRepositoryInterface
     */
    private $orderItem;

    public function __construct(
        OrderItemRepositoryInterface $orderItem
    ) {
        $this->orderItem = $orderItem;
    }

    /**
     * result
     *
     * @return string
     */
    public function getRuleIds(int $itemId): string
    {
        $ruleIds = null;
        try {
            $orderItem = $this->orderItem->get($itemId);
            $ruleIds = $orderItem->getAppliedRuleIds();
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }

        return $ruleIds;
    }
}

Now Call the metod with required data,

$itemId = 1;
echo $ruleId = $this->getRuleIds($itemId);

Output:
10 (applied rule id)

Where 10 is the id of the “15Off” discount code in our case. You can fetch the detail of Coupon code from the id by Get Shopping cart Coupon data by id.

If Multiple rules applied to the single item, the result will be comma-separated all the applicable rule ids.