Last active
July 1, 2020 20:48
-
-
Save juniorthiesen/de02e8c1eedccb1d80091ec1a661dc97 to your computer and use it in GitHub Desktop.
entrega gratuita por classe de entrega. - remove se tiver algum sem classe
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
function my_wc_free_shipping_by_shipping_class( $rates, $package ) { | |
$shipping_class = 'entrega-gratuita'; // Slug da sua classe de entrega. | |
$allow_free_shipping = true; | |
// Verifica se todos os produtos precisam ser entregues e se possuem a class de entrega selecionada. | |
foreach ( $package['contents'] as $value ) { | |
$product = $value['data']; | |
if ( $product->needs_shipping() && $shipping_class !== $product->get_shipping_class() ) { | |
$allow_free_shipping = false; | |
break; | |
} | |
} | |
// Remove a entrega gratuita se algum produto não possuir a classe de entrega selecionada. | |
if ( ! $allow_free_shipping ) { | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 'free_shipping' === $rate->method_id ) { | |
unset( $rates[ $rate_id ] ); | |
break; | |
} | |
} | |
} | |
return $rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'my_wc_free_shipping_by_shipping_class', 100, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Se eu quiser adicionar mais um slug de classe de entrega, como faço?
Tenho mais 2 slugs:
gratis-kit-cachaca-df-go
frete-gratis-luxo-sp-rj
Consegue me ajudar?