Hide payment method if specific variation is in Checkout Page | Woocommerce

I'm using this code on my WordPress website to hide the payment gateway when a particular Product variation is selected. However, as soon as I insert the code, WordPress displays a Critical error from the backend. Kindly help me with how to use this code on my website?

Product attribute: Personalization Required Variables: Yes| No

I want to achieve if someone selects a 'Yes' variation then COD options get disabled on the checkout page. Alternatively, if they select 'No' then only the cod option is available.

Product Page URL: https://savvyandgroovy.com/product/prashant/

This is what I am currently using:

add_filter(‘woocommerce_available_payment_gateways’, ‘conditional_payment_gateways’, 10, 1);
function conditional_payment_gateways($available_gateways)
{
  $in_cart=false;
  foreach(WC()->cart->get_cart_contents() as $key=>$values)
  {
    // See if there is an attribute called ‘pa_size’ in the cart
    // Replace with whatever attribute you want
    if(array_key_exists(‘personalization-required’, (array)$values[‘data’]->get_attributes()))
    {
      foreach($values[‘data’]->get_attributes() as $attribute=>$variation) ;
      // Replace ‘small’ with your value.
      if($variation==‘Yes’) $in_cart=true; //edited
    }
  }
  if($in_cart)
    unset($available_gateways[‘cod’]); // unset ‘cod’

  return $available_gateways;
}

Solutions

Your code is working properly but it is giving error because of quotes which you are using. I have updated it as bellow:

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
    $in_cart = false;
    
    if ( !empty(WC()->cart) ) {
        foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        // See if there is an attribute called ‘pa_size’ in the cart
        // Replace with whatever attribute you want
        //echo '<pre>';
        //print_r($values['data']->get_attributes());
        //echo '</pre>';
        if (array_key_exists('personalization-required', (array) $values['data']->get_attributes() ) ) {
            foreach ($values['data']->get_attributes() as $attribute => $variation);
                // Replace ‘small’ with your value.
                // if ($variation == 'No') $in_cart = true; //edited
                if ($variation == 'Yes') {
                    $in_cart = true; //edited
                    // unset($available_gateways['cod']); // unset ‘cod’
                }
            }
        }
    
        if ( $in_cart )
            unset($available_gateways['cod']); // unset ‘cod’
    }

    return $available_gateways;
}

I want to achieve if someone selects a 'NO' variation then cod options get disabled on the checkout page Alternatively, if they select 'YES' then only the cod option is available.

This will not work reverse in your code because you have written if ($variation == ‘Yes’) $in_cart = true; //edited so you need to update it also. So that also I have updated in above code.

The code above is not quite full since the OP wants to hide payment gateways in both scenarios so i just tweak the 1st answer a bit.

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
    
    if( is_admin() ) 
        return $available_gateways;
    $hide_cod = $hide_rest = false;
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        if($cart_item["variation"]["attribute_pa_personalization-required"] === 'no') {
            $hide_rest = true;
            
        }

        if($cart_item["variation"]["attribute_pa_personalization-required"] === 'yes') {
            $hide_cod = true;
        }
    }
    
    if($hide_cod) {
        unset($available_gateways['cod']);
    } 
    if($hide_rest) {
        unset($available_gateways['bacs']);
        unset($available_gateways['cheque']);
        unset($available_gateways['ppcp-gateway']);
        //etc
    }

    return $available_gateways;
}

Keep in mind that if you add both variations in cart youll be left with no payment method.

Similar questions

Order status still “Pending Payment” after successful payment (Custom payment gateway)
I was develop a custom payment gateway for woocommerce, everything is good but after payment success the order still "pending payment" In the __construct() function I check for payment provider respond and here is the complete_transaction() function But the order status still "pending payment". now I need to change the order status to "Completed" a...
Disable Payment Gateway For Specific Shipping Method On Checkout Only
I ran into an issue in WooCommerce and I'm wondering if anyone else has experienced it too. I sell certain products that are too fragile to be shipped by UPS/DHL/FedEx. So I have to ship these products via pallet. To solve my problem I created a "request a quote" shipping method that allows my customers to select BACS as the payment metho...
WooCommerce add custom email content based on payment method and shipping method
I'm trying to add different content to woocommerce completed order email notifications based on combinations of payment methods and shipping method. My code so far: The payment part works (I get the right "something1" to "something3" content) but if I add the && shipping condition, I get "something3" with every payment method. Any idea what...
Add a fee based on shipping method and payment method in Woocommerce
I need to apply an additional fee when a customer can place an order with free shipping, but wants to select COD payment. So, Free Shipping + COD payment => fee. I tried unsuccessfully the following piece of code. Where am I wrong?
How To Prevent Woocommerce from selecting default payment method on Checkout page?
On Checkout page payment methods are presented and the first one is selected by default and automatically. I need to prevent the selection so no payment method is initially selected by WC. I tried 2 things so far: jQuery from Chrome console: jQuery( '.payment_methods input.input-radio' ).prop('checked', false); result: Remove the code from payment-...
Woocommerce Payment Method Detection in Checkout Page
I am using woocommerce plugin and braintree extension of woocommerce for payment. I have enabled both card and paypal payment of woocommerce braintree to checkout. I am trying to figure out how to know which payment gateway the user selects before user actually checkouts and pays. Any hooks under woocommerce or braintree to find either credit card ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.