Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Alexander-Pop/032e9ed186bc1256067e120f8328b40a to your computer and use it in GitHub Desktop.
Save Alexander-Pop/032e9ed186bc1256067e120f8328b40a to your computer and use it in GitHub Desktop.
Magento2 - Alter the payment methods list in checkout #checkout #magento2 #plugin
<?xml version="1.0"?>
<!-- MyNamespace\MyModule\etc\di.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\PaymentInformationManagement">
<plugin sortOrder="1" name="MyNamespaceMyModulePaymentInformationManagement"
type="MyNamespace\MyModule\Plugin\Model\PaymentInformationManagementPlugin"/>
</type>
</config>
<?php
namespace MyNamespace\MyModule\Plugin\Model;
class PaymentInformationManagementPlugin
{
/**
* Here is an example how you can alter the payment methods list in the checkout.
* Remove free and checkmo payment methods in the checkout
*/
public function afterGetPaymentInformation(\Magento\Checkout\Model\PaymentInformationManagement $subject, $result)
{
$paymentMethods = ['free','checkmo'];
$currentPaymentMethods = $result->getPaymentMethods();
foreach ($currentPaymentMethods as $key => $data) {
if (!in_array($data->getCode(), $paymentMethods)) {
unset($currentPaymentMethods[$key]);
}
}
$result->setPaymentMethods($currentPaymentMethods);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment