/**
* WooCommerce customisation
* Filter the list of available payment gateways based on a custom
* field (in the example, "billing_customer_type").
*
* This customisation is compatible with the Aelia Currency Switcher
* (https://aelia.co/shop/currency-switcher-woocommerce/) and the other
* plugins developed by Aelia (https://aelia.co/shop/product-category/woocommerce/).
*
* @author Aelia <[email protected]>
* @link https://aelia.co
*/
/**
* Filters the availabel payment gateways based on the value of the
* "billing_customer_type" field.
*
* @param array gateways The array of gateways passed by WooCommerce.
* @return array The filtered array of gateways.
* @author Aelia <[email protected]>
* @link https://aelia.co
*/
add_filter('woocommerce_available_payment_gateways', function ($gateways) {
if(!empty($_POST['post_data'])) {
parse_str($_POST['post_data'], $post_data);
$customer_type = strtolower(!empty($post_data['billing_customer_type']) ? $post_data['billing_customer_type'] : 'company');
switch($customer_type) {
case 'company':
unset($gateways['paypal']);
break;
case 'private':
unset($gateways['cod']);
break;
}
}
return $gateways;
}, 30);
/**
* Adds a script to ensure that, when the "customer type" field changes,
* the list of available payment gateways is updated.
* @author Aelia <[email protected]>
* @link https://aelia.co
*/
add_action('wp_footer', function() {
// The script is only needed on the checkout page
if(!is_checkout()) {
return;
}
?>
<script type="text/javascript">
// When the "billing_customer_type" field changes,
// refresh the checkout data. This will update the
// list of payment gateways
jQuery(document).ready(function($) {
$('#billing_customer_type').on('change', function() {
$('#billing_country').change();
});
});
</script>
<?php
});
Comments