How to override (vendor/magento/module-gift-card/Model) Magento\GiftCard\Model\AccountGenerator generate() function Magento 2

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

Question

How to override (vendor/magento/module-gift-card/Model) Magento\GiftCard\Model\AccountGenerator generate() function Magento 2

Was it helpful?

Solution

Try a plugin approach if suits.

I have added beforeGenerate and afterGenerate methods you can use any of them.

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

<type name="Magento\GiftCard\Model\AccountGenerator">
        <plugin name="GiftCard_Custom" type="Custom\GiftCard\Plugin\GCAccountGenPlugin"></plugin>
</type>

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

<?php


namespace Custom\GiftCard\Plugin;

use Magento\GiftCard\Model\AccountGenerator as MageGiftcardModel;
use Magento\Sales\Model\Order\Item as OrderItem;
use Psr\Log\LoggerInterface;


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


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

    }

    /**
     *
     * @param MageGiftcardModel $subject
     * @param OrderItem $orderItem
     * @param int $qty
     * @param array $options
     */

    public function beforeGenerate(MageGiftcardModel $subject,  OrderItem $orderItem, int $qty, array $options){

        /**
         * Add/change your logic here
         */
        $this->logger->debug('beforeGenerateWorks: ');
        $this->logger->debug(__METHOD__ . ' - ' . __LINE__);
        if ($orderItem->getId()) {
            $this->logger->debug( $orderItem->getId() );
        }



    }

    /**
     * @param MageGiftcardModel $subject
     * @param $result
     * @param OrderItem $orderItem
     * @param int $qty
     * @param array $options
     */
    public function afterGenerate(
        MageGiftcardModel $subject, $result, OrderItem $orderItem, int $qty, array $options)
    {
        if ($orderItem->getId()) {
            $this->logger->debug( $orderItem->getId() );
        }
        /**
         * Add/change your logic here
         */
        $this->logger->debug(__METHOD__ . ' - ' . __LINE__);
        $this->logger->debug('afterGenerateWorks: ');
        return $result;
    }
}

OTHER TIPS

To override the generate() function of Magento\GiftCard\Model\AccountGenerator, You have to create a module like below.

app/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_GiftCard"/>
        </sequence>
    </module>
</config>

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\GiftCard\Model\AccountGenerator" type="Vendor\Module\Rewrite\Magento\GiftCard\Model\AccountGenerator"/>
</config>

app/code/Vendor/ModuleRewrite/Magento/GiftCard/Model/AccountGenerator.php

<?php
namespace Vendor\Module\Rewrite\Magento\GiftCard\Model;

/**
 * Class AccountGenerator
 *
 * @package Vendor\Module\Rewrite\Magento\GiftCard\Model
 */
class AccountGenerator extends \Magento\GiftCard\Model\AccountGenerator
{

    /**
     * Generates giftcard accounts.
     *
     * @param OrderItem $orderItem
     * @param int $qty
     * @param array $options
     * @return void
     */
    public function generate(OrderItem $orderItem, int $qty, array $options)
    {
        //Your code here
    }   
}

Hope this will work for you.

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