Last active
November 11, 2020 17:26
-
-
Save pablo-sg-pacheco/b859fbee1c4f44d1121fb3c56d7efee7 to your computer and use it in GitHub Desktop.
Frete grátis por região (faixa de cep) - Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Testa uma faixa de cep | |
function checkCpfRange($valor, $intervalos) { | |
$v = (int) preg_replace("/\D+/", "", $valor); | |
foreach ($intervalos as $range): | |
list($min, $max) = $range; | |
if ($v >= $min && $v <= $max) | |
return true; | |
endforeach; | |
return false; | |
} | |
//Habilita o frete gratis caso o CEP esteja dentro das faixas permitidas | |
add_filter( 'woocommerce_package_rates', 'custom_woocommerce_correios_shipping_methods', 10, 2 ); | |
function custom_woocommerce_correios_shipping_methods($rates, $package) { | |
$intervalos = array( | |
array('30000000', '39999999'), //MG | |
array('20000000', '28999999'), //RJ | |
array('01000000', '19999999'), //SP | |
array('29000000', '29999999'), //ES | |
); | |
//Verify if any coupon is applied | |
if(is_array(WC()->cart->applied_coupons) && count(WC()->cart->applied_coupons)>0){ | |
return $rates; | |
} | |
if (!checkCpfRange(WC()->customer->postcode, $intervalos)) { | |
unset($rates['free_shipping']); | |
} | |
$states = array('SP', 'RJ', 'ES', 'MG'); | |
if (isset($rates['free_shipping']) && !in_array(WC()->customer->shipping_state, $states)) { | |
unset($rates['free_shipping']); | |
} | |
if (isset(WC()->cart->subtotal) && WC()->cart->subtotal < 200) { | |
unset($rates['free_shipping']); | |
} | |
return $rates; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment