2016-12-12 1 views
-1

Comment désactiver BACS mode de paiement pour local delivery méthode d'expédition?Désactivation de la méthode de paiement BACS pour la méthode d'expédition de livraison locale

J'ai inclus le code ci-dessous pour mon functions.php fichier, mais il ne fonctionne pas.
Peut-être que quelqu'un pourrait m'aider avec ça.

function my_custom_available_payment_gateways($gateways) { 
    $chosen_shipping_rates = WC()->session->get('chosen_shipping_methods'); 
    // When 'local delivery' has been chosen as shipping rate 
    if (in_array('local_delivery', $chosen_shipping_rates)) : 
     // Remove bank transfer payment gateway 
     unset($gateways['bacs']); 
    endif; 
    return $gateways; 
} 
add_filter('woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways'); 

Répondre

2

Vous n'êtes pas loin. Pour que votre code fonctionne, vous devez manipuler les données dans le tableau chosen shipping methods pour obtenir uniquement les limaces dans une boucle foreach.

Voici le code:

add_filter('woocommerce_available_payment_gateways', 'unset_bacs_for_local_delivery'); 

function unset_bacs_for_local_delivery($gateways) { 
    // Initialising variables 
    $chosen_shipping_rates = array(); 

    // Iterating and manipulating the "chosen shipping methods" to get the SLUG 
    foreach(WC()->session->get('chosen_shipping_methods') as $shipping_method) : 
     $shipping_arr = explode(':', $shipping_method); 
     $chosen_shipping_rates[] = $shipping_arr[0]; 
    endforeach; 

    //When 'local delivery' has been chosen as shipping method 
    if (in_array('local_delivery', $chosen_shipping_rates)) : 
     // Remove bank transfer payment gateway 
     unset($gateways['bacs']); 
    endif; 

    return $gateways; 
} 

Ce code est testé et est entièrement fonctionnel.

Le code va dans le fichier function.php de votre thème (ou thème) enfant actif. Ou encore dans tous les fichiers php plugin.