Active an extension based on condition in magento 2

Active an extension based on condition in magento 2

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Active an extension based on condition in magento 2

Activating an Extension Based on Conditions in Magento 2

If you’re running a Magento 2 store that offers multiple payment methods, you might find yourself needing to activate certain extensions based on specific conditions. In this article, we’ll explore how to dynamically enable or disable the custom payment gateway extension depending on whether your products have the ‘otherthancod’ attribute active.

Understanding the Scenario

You’ve installed a custom payment gateway extension alongside cash on delivery as available payment methods on your Magento 2 store. To control which payment options display in the checkout, you want to introduce an attribute-based condition: if the product has its ‘otherthancod’ attribute set to “active”, then show only the custom payment gateway; otherwise, offer both payment gateways.

Where to Add the Condition Check

You’re correct that the Config class in your custom payment extension’s config.php file plays a role in activating the payment method. However, adding an if condition before this class might not be the optimal approach.

A better strategy is to leverage Magento 2’s event-driven architecture by hooking into the payment_method_is_active_v1 event. This event allows you to override the default behavior of checking if a payment method should be active or not.

Implementing the Condition Check

To implement your condition check, create an observer that listens for the payment_method_is_active_v1 event and then modifies the result based on your product attribute value.

Step 1: Create an Observer File

Create an observer file at app/code/Vendor/PaymentExtension/etc/frontend/events.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/events.xsd">
    <event name="payment_method_is_active_v1">
        <observer name="vendor_paymentextension_check_condition" instance="Vendor\PaymentExtension\Observer\CheckCondition" />
    </event>
</config>

Step 2: Create the Observer Class

Create the observer class app/code/Vendor/PaymentExtension/Observer/CheckCondition.php:

namespace Vendor\PaymentExtension\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Model\Quote\Item;

class CheckCondition implements ObserverInterface
{
    protected $checkoutSession;
    protected $items;

    public function __construct(
        CheckoutSession $checkoutSession,
        Item $item
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->items = $item;
    }

    public function execute(EventObserver $observer)
    {
        $cart = $this->checkoutSession->getQuote();
        $items = $cart->getItems();

        // Implement your condition check logic here
        // ...

        if ($flag == $count) {
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', true);
        } else {
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false);
        }
    }
}

Step 3: Replace the Condition Check Logic

Replace the existing condition check logic with your custom attribute-based condition. This will determine whether to set is_available to true or false based on the presence of active ‘otherthancod’ attributes in the cart items.

Conclusion

By following this approach, you can easily control which payment methods display during checkout without creating a separate extension for each condition. Hooking into Magento 2’s built-in events like payment_method_is_active_v1 provides a flexible and maintainable way to customize the behavior of your payment gateways.

Remember that proper observer registration is crucial for this method to work correctly. Ensure that you register your observer in the correct event file (frontend/events.xml) and use the appropriate instance name when creating the observer class.

For more insights on Magento 2 development best practices, visit Laravel Company’s Magento Development Services page.

Magento 2 Checkout Catalog Extensions

Our network