0

I have to override the following file

vendor/magento/module-payment/Model/Method/Adapter.php

but it's not working after writing the preference in etc/di.xml. If anyone knows please help. Below is the code I am using: Vendor/Module/etc/di.xml

<preference for="Magento\Payment\Model\Method\Adapter" type="Vendor\Module\Model\Adapter" />

Vendor/Module/Model/Adapter.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Command\CommandManagerInterface;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
use Magento\Payment\Model\InfoInterface;
use Magento\Payment\Model\MethodInterface;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\CartInterface;
use Psr\Log\LoggerInterface;


class Adapter extends \Magento\Payment\Model\Method\Adapter
{
    /**
     * @var ValueHandlerPoolInterface
     */
    private $valueHandlerPool;

    /**
     * @var ValidatorPoolInterface
     */
    private $validatorPool;

    /**
     * @var CommandPoolInterface
     */
    private $commandPool;

    /**
     * @var int
     */
    private $storeId;

    /**
     * @var string
     */
    private $formBlockType;

    /**
     * @var string
     */
    private $infoBlockType;

    /**
     * @var InfoInterface
     */
    private $infoInstance;

    /**
     * @var string
     */
    private $code;

    /**
     * @var ManagerInterface
     */
    private $eventManager;

    /**
     * @var PaymentDataObjectFactory
     */
    private $paymentDataObjectFactory;

    /**
     * @var \Magento\Payment\Gateway\Command\CommandManagerInterface
     */
    private $commandExecutor;

    /**
     * Logger for exception details
     *
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(ManagerInterface $eventManager, ValueHandlerPoolInterface $valueHandlerPool, PaymentDataObjectFactory $paymentDataObjectFactory, $code, $formBlockType, $infoBlockType, CommandPoolInterface $commandPool = null, ValidatorPoolInterface $validatorPool = null, CommandManagerInterface $commandExecutor = null, LoggerInterface $logger = null)
    {
        parent::__construct($eventManager, $valueHandlerPool, $paymentDataObjectFactory, $code, $formBlockType, $infoBlockType, $commandPool, $validatorPool, $commandExecutor, $logger);
    }

    public function isAvailable(CartInterface $quote = null){
        parent::isAvailable();
        die("fsdfsdfsdf");
        }


    /**
     * @inheritdoc
     */
    public function cancel(InfoInterface $payment)
    {
        die("core test");
        //$this->executeCommand('cancel', ['payment' => $payment]);
        return $this;
    }

}
2

You can override Adapter.php file using preference.

  1. Add preference code line in di.xml as per example

  2. Upload Adapter.php file in your module

    Vendor_Name\Module_Name\Model

Note: Please update namespace in Adapter.php file

Please check preference example. It will help you.

https://github.com/csvikram44/Preference-Example

11
  • If it help you then please mark it correct. Thanks – Vikram Kumar Feb 11 at 7:56
  • 1
    Its a perfect Way if You Want to Override Whole class Or Private Methods. – Suraj Prajapat Feb 11 at 8:00
  • @VikramKumar I've followed the same as per the github url but its not working, its calling the core file only. – Rahul pandey Feb 11 at 9:30
  • @Rahulpandey Yes, I just override file. You need customize code. What you want to do? – Vikram Kumar Feb 11 at 9:46
  • @VikramKumar I've to customize cancel method, I've to call void in cancel method. like this public function cancel(InfoInterface $payment) { $this->void($payment); } – Rahul pandey Feb 11 at 9:54
0

Try:-

Create File:-

path:- Vendor\Module\Model\Method\Adapter.php

    <?php
    
    
    namespace Vendor\Module\Model\Method;
    
    
    use Magento\Framework\Event\ManagerInterface;
    use Magento\Payment\Gateway\Command\CommandManagerInterface;
    use Magento\Payment\Gateway\Command\CommandPoolInterface;
    use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
    use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
    use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
    use Magento\Quote\Api\Data\CartInterface;
    use Psr\Log\LoggerInterface;
    
    class Adapter extends \Magento\Payment\Model\Method\Adapter
    {
        public function __construct(ManagerInterface $eventManager, ValueHandlerPoolInterface $valueHandlerPool, PaymentDataObjectFactory $paymentDataObjectFactory, $code, $formBlockType, $infoBlockType, CommandPoolInterface $commandPool = null, ValidatorPoolInterface $validatorPool = null, CommandManagerInterface $commandExecutor = null, LoggerInterface $logger = null)
        {
            parent::__construct($eventManager, $valueHandlerPool, $paymentDataObjectFactory, $code, $formBlockType, $infoBlockType, $commandPool, $validatorPool, $commandExecutor, $logger);
        }
    
        public function isAvailable(CartInterface $quote = null){
            parent::isAvailable();
            <!-- your code -->
        }
    }

Create di.xml

path:- 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\Payment\Model\Method\Adapter" type="Vendor\Module\Model\Method\Adapter" />
</config>
4
  • I've followed the same as per the github url but its not working, its calling the core file only – Rahul pandey Feb 11 at 9:32
  • please share your code. – Himanshu Feb 11 at 12:45
  • Himanshu I've updated the code in question. Please check – Rahul pandey Feb 11 at 13:10
  • I have to override the cancel method. – Rahul pandey Feb 11 at 13:26

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.