Skip to content

Instantly share code, notes, and snippets.

@luiseduardobraschi
Created May 25, 2018 09:50
Show Gist options
  • Save luiseduardobraschi/af09e10a2d91edb5b68f5ca6f03a4951 to your computer and use it in GitHub Desktop.
Save luiseduardobraschi/af09e10a2d91edb5b68f5ca6f03a4951 to your computer and use it in GitHub Desktop.
Show either Correios or flat rate shipping method based on cart total.
<?php
add_action('init', function(){
function hide_correios_shipping_methods($rates, $package) {
$correios_methods_list = array(
'correios-pac',
'correios-sedex',
'correios-sedex10-envelope',
'correios-sedex10-pacote',
'correios-sedex12',
'correios-sedex-hoje',
'correios-esedex',
'correios-carta-registrada',
'correios-impresso-normal',
'correios-impresso-urgente',
'correios-mercadoria-expressa',
'correios-mercadoria-economica',
'correios-leve-internacional',
);
$max_cart_total_allowed = 100;
if( WC()->cart->get_cart_contents_total() > $max_cart_total_allowed) {
$rates = array_filter($rates, function($rate) {
return 'flat_rate' == $rate->get_method_id(); // deixa só a taxa fixa
});
} else {
$rates = array_filter($rates, function($rate) use ($correios_methods_list) {
return in_array($rate->get_method_id(), $correios_methods_list); // deixa só os métodos dos Correios
});
}
return $rates;
}
add_filter('woocommerce_package_rates', 'hide_correios_shipping_methods', 10, 2);
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment