How to Hide Specific Payment Method in Woocommerce based on Delivery Option Picked

In my WooCommerce store I have 2 shipping methods. Both are DPD but, 1- you pay upfront 2- you pay cash on delivery

So I got it like:

  • DPD (cash on delivery)
  • DPD (pay upfront)

The problem is that when I pick 2 option I still have COD available to pick as a payment method, how can I delete that? I have always been using this code and it worked but now it doesn't. '''function my_custom_available_payment_gateways( $gateways ) { $ship = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();

  if(in_array('local_pickup:1', $ship)) {
    //Odbior osobisty
} elseif(in_array('flat_rate:1', $ship)) {
} elseif(in_array('flat_rate:2', $ship)) {

Pls help

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum