Created
June 26, 2022 18:21
-
-
Save Alexander-Pop/032e9ed186bc1256067e120f8328b40a to your computer and use it in GitHub Desktop.
Magento2 - Alter the payment methods list in checkout #checkout #magento2 #plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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