How to override (vendor/magento/module-gift-card/Model/Plugin) Magento\GiftCard\Model\Plugin\QuoteItem afterConvert() function Magento 2

magento.stackexchange https://magento.stackexchange.com/questions/309267

Question

override a function (vendor/magento/module-gift-card/Model/Plugin) Magento\GiftCard\Model\Plugin\QuoteItem afterConvert() function Magento 2

Was it helpful?

Solution

Do not add Plugin on Plugin if it is not necessary.

First, depending on what are the requirements then you can go for it to add Plugin on Plugin.

One way to change the functionality of a plugin is to disable it entirely and redefine a plugin for the same type but not a suitable way until it is fix for existing problems.

To disable a plugin, one can easily do it using the disable attribute like below.

File: app/code/Custom/GiftCard/etc/di.xml

<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
    <!-- Check the name (append_gift_card_data_to_order) of the plugin from the vendor/magento/module-gift-card/etc/di.xml -->
    <plugin name="append_gift_card_data_to_order" disabled="true" />
</type>

Alternatively, you can give a try on below by adding plugin on the class Magento\Quote\Model\Quote\Item\ToOrderItem instead of Magento\GiftCard\Model\Plugin\QuoteItem

File: app/code/Custom/GiftCard/etc/di.xml

<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="own_gc_append_gift_card_data_to_order" type="Custom\GiftCard\Plugin\QuoteItemPlugin" sortOrder="50"/>
</type>

Change the value of sortOrder="50" small if you want below plugin executed earlier and set larger sortOrder if you want a plugin to be called lately.

File: app/code/Custom/GiftCard/Plugin/QuoteItemPlugin.php

<?php


namespace Custom\GiftCard\Plugin;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Quote\Model\Quote\Item\ToOrderItem as QuoteToOrderItem;
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\GiftCard\Model\Giftcard;
use Magento\Store\Model\ScopeInterface;

class QuoteItemPlugin
{
    /**
     * Psr Logger  instance
     *
     * @var LoggerInterface
     * @since 100.1.0
     */
    protected $logger;


    /**
     * @param ObjectManagerInterface $objectManager
     */
    public function __construct(
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;

    }

    /**
     * @param QuoteToOrderItem $subject
     * @param OrderItem $orderItem
     * @param AbstractItem $quoteItem
     * @param array $data
     */
    public function afterConvert(QuoteToOrderItem $subject, OrderItem $orderItem, AbstractItem $quoteItem, $data = [])
    {
        /**
         * Add/change your logic here
         */
        $this->logger->debug('afterGenerateWorks: ');
        $this->logger->debug(__METHOD__ . ' - ' . __LINE__);
        if ($orderItem->getId()) {
            $this->logger->debug( $orderItem->getId() );
        }

        //Check return type as per Magento\Sales\Model\Order\Item
        return $orderItem;
    }
}

Click on this link for a detailed guide on Plugin on Plugin

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top