Skip to content

Instantly share code, notes, and snippets.

@cmtickle
Last active January 28, 2020 09:22
Show Gist options
  • Save cmtickle/a7171f086734fb7ecd952cf8695835b5 to your computer and use it in GitHub Desktop.
Save cmtickle/a7171f086734fb7ecd952cf8695835b5 to your computer and use it in GitHub Desktop.
Magento 2 - Enable viewing of orders which contain a removed payment method.

Magento 2

Unable to view historical orders if a payment method is removed.

If you remove a payment method in Magento 2 you will be unable to view orders which have been placed using that payment method in the admin and customer dashboard.

The error logs will show a message similar to the below:

[2020-01-23 10:27:34] main.CRITICAL: Class Magenest\Stripe\Model\StripePaymentMethod does not exist {"exception":"[object] (ReflectionException(code: -1): Class Magenest\\Stripe\\Model
StripePaymentMethod does not exist at /var/www/vhosts/some_customer/vendor/magento/framework/Code/Reader/ClassReader.php:19

If you are willing to class these payments as legacy and complete refunds/other processes offline you can use the following solution.

  1. Create a module (I recommend using Mage2Gen to save time creating the base module). In our example this is installed as app/code/My/DummyPayment/. 'magenest_stripe' is the payment code for the payment method which has been removed.

  2. app/code/My/DummyPayment/etc/module.xml should be as below

<?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="My_DummyPayment" setup_version="1.0.0">
		<sequence>
			<module name="Magento_Sales"/>
			<module name="Magento_Payment"/>
		</sequence>
	</module>
</config>
  1. Create app/code/My/DummyPayment/etc/config.xml as below
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <magenest_stripe>
                <active>0</active>
                <email_customer>0</email_customer>
                <title>Legacy Stripe Payment</title>
                <model>My\DummyPayment\Model\DummyPayment</model>
            </magenest_stripe>
        </payment>
    </default>
</config>
  1. app/code/My/DummyPayment/Model/DummyPayment.php
<?php
namespace My\DummyPayment\Model;

class DummyPayment extends \Magento\Payment\Model\Method\AbstractMethod
{
    protected $_code = 'magenest_stripe';
}
  1. app/code/My/DummyPayment/Setup/Patch/Data/UpdateDummyPaymentInfo.php to update database values for the model and payment title
<?php
namespace My\DummyPayment\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;

class UpdateMagenestStripeModel implements DataPatchInterface
{

    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var \Vortex\UpgradeData\Setup\StoreConfig
     */
    protected $storeConfig;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
        \Vortex\UpgradeData\Setup\StoreConfig $storeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->storeConfig = $storeConfig;
        $this->_storeManager = $storeManager;
    }

    /**
     * Enabling config on all inventory options
     *
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->startSetup();

        $this->storeConfig->set('payment/magenest_stripe/model', 'My\DummyPayment\Model\DummyPayment', 'default', 0);
        $this->storeConfig->set('payment/magenest_stripe/title', 'Legacy Stripe Payment', 'default', 0);

        $this->moduleDataSetup->endSetup();
    }

    /**
     * No dependencies on any other patches
     *
     * @return array|string[]
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

}
  1. Run bin/magento setup:upgrade and you should now be able to view your orders again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment