پارسی

Conditional WooCommerce Payment Gateways

Conditional WooCommerce Payment Gateways

After a multilingual WordPress site, you must condition the WooCommerce payment gateway based on different languages. In this article, I want to explain exactly how to do this.

Some time ago (before we launched the Ertano site ) on the MihanWP, we used the WPML plugin to make the site multilingual. We used the WooCommerce Multilingual plugin to sell products in dollars in different languages with this plugin. But we had a fundamental problem! Our Bank payment portal was also displayed in English.

So, the user also had access to the payment portal in Rials in the English version of the site, and if the price of the product in the English version of the site was $50, he could buy the product for 50 Rials. What was the solution ?! We had to condition the payment gateway in different languages.

Finally, we solved this problem using a WordPress filter. Now, after the video on making a Bitcoin payment gateway in WordPress, as I promised in the video, I will talk about conditioning the WooCommerce payment gateway in this article. Are you ready? So let’s get started.

Conditional WooCommerce payment gateway

The first thing you need to do is enter the functions.php file of your site template or build a dedicated WordPress plugin. Now you should be a little familiar with PHP. Of course, it is done with a right and a wrong if you are not familiar with it. So do not worry too much. 😉

The function call we will create.

When we enter the functions.php file, you should find the <?php code at the beginning of the file. One line after that, in the second line of the WordPress function file, enter this code:

add_filter( 'woocommerce_available_payment_gateways', 'mihanwp_conditional_gateway' );

Using the above code, we added a new filter to WooCommerce. We informed that the available payment gateways must be activated or deactivated precisely according to our specific function, namely mihanwp_conditional_gateway.

Create the desired function

We need to specify the function and add our desired terms and conditions to the function. So add these codes as well:

function mihanwp_conditional_gateway( $available_gateways ) {

}

Now we need to add our favorite code inside the above function, between {and.

Our terms and conditions for displaying the payment gateway

For example, suppose you want to activate the desired payment gateway for users who have entered the site. So we have to put the following code in the above function:

$user = wp_get_current_user();

if ( isset( $available_gateways['mellat'] ) && !is_user_logged_in()) {
        unset( $available_gateways['mellat'] );
}
return $available_gateways;

Do not forget that you should use the name of your preferred payment gateway in the above code instead of mellat. The payment gateway name is available in your payment gateway plugin codes.

Display payment gateway based on WPML language

Now suppose we want to enable or disable ports in WPML for different languages. So we have to use the following code.

if(ICL_LANGUAGE_CODE == 'en'){
        unset( $available_gateways['mellat'] );
}
if(ICL_LANGUAGE_CODE == 'fa'){
        unset( $available_gateways['paypal'] );
}
return $available_gateways;

In the above code, I specified that if the site’s language was English, deactivate the Mellat payment gateway, and if the language of the site was Persian, deactivate the PayPal payment gateway.

Whatever we made

In general, to condition WooCommerce payment gateways based on different languages, we must use this code:

add_filter( 'woocommerce_available_payment_gateways', 'mihanwp_conditional_gateway' );

function mihanwp_conditional_gateway( $available_gateways ) {
if(ICL_LANGUAGE_CODE == 'en'){
        unset( $available_gateways['mellat'] );
}
if(ICL_LANGUAGE_CODE == 'fa'){
        unset( $available_gateways['paypal'] );
}
return $available_gateways;
}

Now you can replace your desired codes based on different conditions in the above function and activate or deactivate payment gateways according to your desired conditions. 🙂 For example, you can find the condition of the transport options and put it in the built-in function. In this case, your payment gateway will be displayed based on shipping methods.

Be happy.

What do you think?