How to disable some payments while shipment is checked Wordpress Woocommerce - php

I have a question how to disable payment option called: "przelewy24" while the "przesyłka pobraniowa" and "płatność przy odbiorze" is checked. (both are the shipment). My code looks like, but it doesn't work:
/**
* Filter payment gateways
*/
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if ( in_array( 'flat_rate:3', $chosen_shipping_rates ) ) :
unset( $gateways['cod'] );
elseif ( in_array( 'flat_rate:5', $chosen_shipping_rates ) ) :
unset( $gateways['bacs'] );
elseif ( in_array( 'flat_rate:5', $chosen_shipping_rates ) ) :
unset( $gateways['przelewy24'] );
elseif ( in_array( 'flat_rate:4', $chosen_shipping_rates ) ) :
unset( $gateways['cod'] );
elseif ( in_array( 'local_pickup:6', $chosen_shipping_rates ) ) :
unset( $gateways['cod'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );

Related

Filter for payment gateways (WooCommerce) - in_array() expects parameter 2 to be array

I've got an
PHP Warning: in_array() expects parameter 2 to be array, null given
function my_custom_available_payment_gateways( $gateways )
{
$chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if ( in_array( 'flexible_shipping_single:7', $chosen_shipping_rates ) ) :
unset( $gateways['bacs'] );
unset( $gateways['ppcp-gateway'] );
unset( $gateways['przelewy24'] );
elseif ( in_array( 'flexible_shipping_single:8', $chosen_shipping_rates ) ) :
unset( $gateways['bacs'] );
unset( $gateways['ppcp-gateway'] );
unset( $gateways['przelewy24'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );
How to change this?
Although chosen_shipping_methods is a native WC session variable, it is probably not being set yet (at the time of that error being shown).
Set your $chosen_shipping_rates variable like this:
$chosen_shipping_rates = WC()->session->chosen_shipping_methods ?? null;
And then wrap your if...endif code to this condition:
if( $chosen_shipping_rates ) {
...
}

Disabling payment methods based on custom shipping methods in WooCommerce

In WooCommerce shop I have 2 delivery method based on Parcel locker. The first is payable in advance, while the second is cash on delivery.
The idea is:
easypack_parcel_machines -> only Pay in advance
easypack_parcel_machines_cod -> only Cash on delivery
My code below. After applying in both cases, I only have Pay in advance. What's wrong?
add_filter( 'woocommerce_available_payment_gateways', 'gateway_disable_shipping_meth' );
function gateway_disable_shipping_meth( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['bacs'] ) && 0 === strpos( $chosen_shipping, 'easypack_parcel_machines' ) ) {
unset( $available_gateways['cod'] );
}
elseif ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'easypack_parcel_machines_cod' ) ) {
unset( $available_gateways['bacs'] );
}
}
return $available_gateways;
}
As both shipping methods start with the same slug, you should simply need to invert them in your if / elseif statement as follows (also there are some other mistake):
add_filter( 'woocommerce_available_payment_gateways', 'gateway_disable_shipping_meth' );
function gateway_disable_shipping_meth( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
if ( isset( $available_gateways['bacs'] ) && 0 === strpos( $chosen_shipping, 'easypack_parcel_machines_cod' ) ) {
unset( $available_gateways['bacs'] );
}
elseif ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'easypack_parcel_machines' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
or also this way too:
add_filter( 'woocommerce_available_payment_gateways', 'gateway_disable_shipping_meth' );
function gateway_disable_shipping_meth( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
if ( 0 === strpos( $chosen_shipping, 'easypack_parcel_machines' ) ) {
if ( false !== strpos( $chosen_shipping, 'cod' ) && isset( $available_gateways['bacs'] ) ) {
unset( $available_gateways['bacs'] );
} elseif ( isset( $available_gateways['cod'] ) ) {
unset( $available_gateways['cod'] );
}
}
}
return $available_gateways;
}
It should work.

How to Disable Multiple Payment Gateways For Specific Shipping Method [duplicate]

This question already has answers here:
Show hide payment methods based on selected shipping method in Woocommerce
(2 answers)
Hide payment methods based on selected shipping method in WooCommerce [duplicate]
(1 answer)
Closed 2 years ago.
I created this code on WooCommerce to disable multiple payment gateways (cardgatecreditcard, cardgategiropay, cardgateideal and cardgatesofortbanking) for two different shipping methods (request_shipping_quote and flat_rate). But how do I simplify it?
// Disable Payment Gateway For Specific Shipping Method
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cardgatecreditcard'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
unset( $available_gateways['cardgatecreditcard'] );
}
if ( isset( $available_gateways['cardgategiropay'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
unset( $available_gateways['cardgategiropay'] );
}
if ( isset( $available_gateways['cardgateideal'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
unset( $available_gateways['cardgateideal'] );
}
if ( isset( $available_gateways['cardgatesofortbanking'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
unset( $available_gateways['cardgatesofortbanking'] );
}
if ( isset( $available_gateways['cardgatecreditcard'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
unset( $available_gateways['cardgatecreditcard'] );
}
if ( isset( $available_gateways['cardgategiropay'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
unset( $available_gateways['cardgategiropay'] );
}
if ( isset( $available_gateways['cardgateideal'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
unset( $available_gateways['cardgateideal'] );
}
if ( isset( $available_gateways['cardgatesofortbanking'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
unset( $available_gateways['cardgatesofortbanking'] );
}
}
return $available_gateways;
}
Try the following:
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
function filter_woocommerce_available_payment_gateways( $available_gateways ) {
$gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
$shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
$disable_gateways = false;
// Check if we need to disable gateways
foreach ( $shipping_methods as $shipping_method ) {
if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
}
// If so, disable the gateways
if ( $disable_gateways ) {
foreach ( $available_gateways as $id => $gateway ) {
if ( in_array( $id, $gateways_to_disable ) ) {
unset( $available_gateways[$id] );
}
}
}
return $available_gateways;
}

How could I fix this problem: in_array () expects parameter 2 to be array, null given in file... on line

I have a problem with this code.
I get warning: in_array() expects parameter 2 to be array, null given in file... on line...
/**
* Filter payment gateways
*/
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( ‘chosen_shipping_methods’ ) : array();
if ( in_array( ‘flexible_shipping_6_1’, $chosen_shipping_rates ) ) :
unset( $gateways[‘cod’] );
elseif ( in_array( ‘flexible_shipping_6_4’, $chosen_shipping_rates ) ) :
unset( $gateways[‘bacs’] );
unset( $gateways[‘paypal’] );
endif;
return $gateways;
}
add_filter( ‘woocommerce_available_payment_gateways’, ‘my_custom_available_payment_gateways’ );
It will be nice if somebody can help me :-)
Judging from your code, it could be that the $chosen_shipping_rates array hasn't been initialized and is resolving to NULL.
For example, if you were to do in_array('some string', $uninstantiated_array)
This will return the warning you are receiving.
My guess is that this is happening because WC()->session is set, but not WC()->session->get( ‘chosen_shipping_methods’ ) and thus you are getting a NULL array.
Try to see if adding a variable $chosen_shipping_methods = WC()->session->get( ‘chosen_shipping_methods’ ) and then using ( isset( $chosen_shipping_methods ) ) ? $chosen_shipping_methods : array(); to see if this solves your problem.
according to your code and supposing that $chosen_shipping_rates is sometimes not an array you can avoid the issue by making sure to execute the responsible code only if the variable is an array:
/**
* Filter payment gateways
*/
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( ‘chosen_shipping_methods’ ) : array();
if(isset($chosen_shipping_rates) && is_array($chosen_shipping_rates )){
if ( in_array( ‘flexible_shipping_6_1’, $chosen_shipping_rates ) ) :
unset( $gateways[‘cod’] );
elseif ( in_array( ‘flexible_shipping_6_4’, $chosen_shipping_rates ) ) :
unset( $gateways[‘bacs’] );
unset( $gateways[‘paypal’] );
endif;
}
return $gateways;
}
add_filter( ‘woocommerce_available_payment_gateways’, ‘my_custom_available_payment_gateways’ );

error post contact form 7 when use the woocommerce filter 'woocommerce_available_payment_gateways'

I use this code snippet to below in order to disable Payment Gateway For Specific Shipping Method.
Snippet found here : https://businessbloomer.com/woocommerce-disable-payment-gateway-for-specific-shipping-method/
function bbloomer_gateway_disable_shipping_30( $available_gateways ) {
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
if ( isset( $available_gateways['paypal'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['paypal'] );
}
if ( isset( $available_gateways['stripe'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['stripe'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_30' );
This code triggers a 500 error with contact form 7
https://moebella24.ninapresotto.com/wp-json/contact-form-7/v1/contact-forms/57004/feedback 500

Categories