Skip to main content
added 6 characters in body
Source Link

You can achieve this by using below code:

namespace Vendor\ModuleName\Controller\Index;

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\SalesRule\Model\RuleFactory;

class YOURClass extends \Magento\Framework\App\Action\Action
{
     /**
     * @var OrderCollection
     */
    private $orderCollection;
    /**
     * @var RuleFactory
     */
    private $rule;

    public function __construct(
        Context $context,
        CollectionFactory $orderCollection,
        RuleFactory $rule,
        \Psr\Log\LoggerInterface $logger
    )
    {
        parent::__construct($context);
        $this->orderCollection = $orderCollection;
        $this->rule = $rule;
        $this->logger = $logger;
    }


    public function execute()
    {
        $collection = $this->orderCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('increment_id', '000000005');

        foreach ($collection->getItems() as $order) {

            foreach($order->getAllItems() as $item) {

                $appliedRule = $item->getAppliedRuleIds();

                if (!empty($appliedRule)) {

                    $appliedRuleArr = explode(",", $appliedRule);

                    foreach ($appliedRuleArr as $ruleId) {

                        $couponCodeData  = $this->rule->create()->load($ruleId);

                        $this->logger->log('600', print_r($couponCodeData->getData(), true));

                    }

                }

            }

        }

    }

}

You can achieve this by below code:

namespace Vendor\ModuleName\Controller\Index;

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\SalesRule\Model\RuleFactory;

class YOURClass 
{
     /**
     * @var OrderCollection
     */
    private $orderCollection;
    /**
     * @var RuleFactory
     */
    private $rule;

    public function __construct(
        Context $context,
        CollectionFactory $orderCollection,
        RuleFactory $rule,
        \Psr\Log\LoggerInterface $logger
    )
    {
        parent::__construct($context);
        $this->orderCollection = $orderCollection;
        $this->rule = $rule;
        $this->logger = $logger;
    }


    public function execute()
    {
        $collection = $this->orderCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('increment_id', '000000005');

        foreach ($collection->getItems() as $order) {

            foreach($order->getAllItems() as $item) {

                $appliedRule = $item->getAppliedRuleIds();

                if (!empty($appliedRule)) {

                    $appliedRuleArr = explode(",", $appliedRule);

                    foreach ($appliedRuleArr as $ruleId) {

                        $couponCodeData  = $this->rule->create()->load($ruleId);

                        $this->logger->log('600', print_r($couponCodeData->getData(), true));

                    }

                }

            }

        }

    }

}

You can achieve this by using below code:

namespace Vendor\ModuleName\Controller\Index;

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\SalesRule\Model\RuleFactory;

class YOURClass extends \Magento\Framework\App\Action\Action
{
     /**
     * @var OrderCollection
     */
    private $orderCollection;
    /**
     * @var RuleFactory
     */
    private $rule;

    public function __construct(
        Context $context,
        CollectionFactory $orderCollection,
        RuleFactory $rule,
        \Psr\Log\LoggerInterface $logger
    )
    {
        parent::__construct($context);
        $this->orderCollection = $orderCollection;
        $this->rule = $rule;
        $this->logger = $logger;
    }


    public function execute()
    {
        $collection = $this->orderCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('increment_id', '000000005');

        foreach ($collection->getItems() as $order) {

            foreach($order->getAllItems() as $item) {

                $appliedRule = $item->getAppliedRuleIds();

                if (!empty($appliedRule)) {

                    $appliedRuleArr = explode(",", $appliedRule);

                    foreach ($appliedRuleArr as $ruleId) {

                        $couponCodeData  = $this->rule->create()->load($ruleId);

                        $this->logger->log('600', print_r($couponCodeData->getData(), true));

                    }

                }

            }

        }

    }

}
Source Link

You can achieve this by below code:

namespace Vendor\ModuleName\Controller\Index;

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\SalesRule\Model\RuleFactory;

class YOURClass 
{
     /**
     * @var OrderCollection
     */
    private $orderCollection;
    /**
     * @var RuleFactory
     */
    private $rule;

    public function __construct(
        Context $context,
        CollectionFactory $orderCollection,
        RuleFactory $rule,
        \Psr\Log\LoggerInterface $logger
    )
    {
        parent::__construct($context);
        $this->orderCollection = $orderCollection;
        $this->rule = $rule;
        $this->logger = $logger;
    }


    public function execute()
    {
        $collection = $this->orderCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('increment_id', '000000005');

        foreach ($collection->getItems() as $order) {

            foreach($order->getAllItems() as $item) {

                $appliedRule = $item->getAppliedRuleIds();

                if (!empty($appliedRule)) {

                    $appliedRuleArr = explode(",", $appliedRule);

                    foreach ($appliedRuleArr as $ruleId) {

                        $couponCodeData  = $this->rule->create()->load($ruleId);

                        $this->logger->log('600', print_r($couponCodeData->getData(), true));

                    }

                }

            }

        }

    }

}