Hide specific payment method based on total weight in Woocommerce

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Hide specific payment method based on total weight in Woocommerce



In woocommerce I would like to hide a "Cash on delivery" payment method for a specific total weight.



For example if cart total weight is up to 15 kilos, "Cash on delivery" payment method on checkout.



Any help is appreciated.




1 Answer
1



The following simple code will hide "COD" payment method if cart total weight is up to 15 Kg.

I assume that the weight unit set in Woocommerce is Kg (kilos).


15 Kg



The code:


add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 11, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways )

$total_weight = WC()->cart->get_cart_contents_weight();

if( $total_weight >= 15 )
unset($available_gateways['cod']); // unset 'cod'

return $available_gateways;



Code goes in function.php file of your active child theme (or active theme). Tested and work.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard