Skip to content

Instantly share code, notes, and snippets.

@juniorthiesen
Last active July 1, 2020 20:48
Show Gist options
  • Save juniorthiesen/de02e8c1eedccb1d80091ec1a661dc97 to your computer and use it in GitHub Desktop.
Save juniorthiesen/de02e8c1eedccb1d80091ec1a661dc97 to your computer and use it in GitHub Desktop.
entrega gratuita por classe de entrega. - remove se tiver algum sem classe
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 );
@nettoferreira
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment