smaex/customer-group-payments

This package is abandoned and no longer maintained. No replacement package was suggested.

Restricts payment methods to specific customer groups in Magento 2.

2.0.0 2019-03-27 21:18 UTC

This package is auto-updated.

Last update: 2023-06-04 09:21:00 UTC


README

Restricts payment methods to specific customer groups in Magento 2.

Intro

Adding additional checks to payment methods – to decide if a certain payment method is applicable to a certain customer or not – is pretty easy and straight forward in Magento 2.

Magento provides a rather simplistic interface for custom payment method checks, and uses a composite check to process these individual checks. Adding a custom check is therefore just a matter of injecting it into Magento’s composite check via dependency injection.

This extension implements such a custom check to decide if a certain payment method is applicable to a customer based on the customer group, along with a corresponding system configuration field for payment methods.

Screenshot: Magento system configuration field for payment methods that restricts a payment method to selected customer groups.

Prerequisite

Unfortunately, there’s currently a limitation in Magento’s code regarding this mechanism that requires us to use a small workaround, so this extension depends on another extension providing this workaround. See smaex/additional-payment-checks for more information.

How to install

Simply require the extension via Composer.

$ composer require smaex/customer-group-payments ^2.0

Finally, enable the module via Magento’s CLI.

$ magento module:enable Smaex_CustomerGroupPayments

How to use

While the extension provides you with all the tools you need to restrict payment methods to specific customer groups, it doesn’t do anything without some custom configuration on your part. That’s because adding system configuration fields is done via XML configuration in Magento.

To properly configure your payment methods, the best way going forward is probably to set up your own custom module (e.g., Acme_Payment) under app/code.

Declaring a dependency on Magento_Payment and Smaex_CustomerGroupPayments in your module.xml is kinda mandatory, dependencies on other modules like Magento_OfflinePayments depend on the payment methods actually used in your project.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Acme_Payment" setup_version="1.0.0">
        <sequence>
            <module name="Magento_OfflinePayments"/>
            <module name="Magento_Payment"/>
            <module name="Smaex_CustomerGroupPayments"/>
        </sequence>
    </module>
</config>

The next and already final step is then to provide your own system.xml under etc/adminhtml in your custom module and extend the existing configuration for each payment method used in your project.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="payment">
            <!--
            Check / Money Order
            -->
            <group id="checkmo">
                <field id="customer_groups" type="multiselect" translate="label comment" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1" sortOrder="52">
                    <include path="Smaex_CustomerGroupPayments::system/customer_groups.xml"/>
                </field>
            </group>
            <!--
            Cash On Delivery Payment
            -->
            <group id="cashondelivery">
                <field id="customer_groups" type="multiselect" translate="label comment" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1" sortOrder="52">
                    <include path="Smaex_CustomerGroupPayments::system/customer_groups.xml"/>
                </field>
            </group>
        </section>
    </system>
</config>

Alternative

If you’re a lazy sloth this looks like too much work (i.e., XML configuration), there’s also another extension tackling the same problem from a very different angle.

We’re hiring!

Wanna work for one of Germany’s leading Magento partners? With agile methods, small teams and big clients? We’re currently looking for experienced masochists PHP & Magento developers in Munich. Sounds interesting? Just drop me a line via j.scherbl@techdivision.com