0

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

0

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;
    }
}
| improve this answer | |
0

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.

| improve this answer | |
  • Thankyou kishor ! Let me check and implement. – Hamendra Sunthwal Mar 30 at 13:46
  • Please accept the answer if it works for you, so it will help to others in Magento community. – Kishor Thummar Mar 31 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.