Skip to content

Instantly share code, notes, and snippets.

@goliver79
Last active April 1, 2020 20:13
Show Gist options
  • Save goliver79/7adab9de94709b165b7d660e76e544db to your computer and use it in GitHub Desktop.
Save goliver79/7adab9de94709b165b7d660e76e544db to your computer and use it in GitHub Desktop.
Woocommerce disable free shipping in "big products"
<?php
if ( !class_exists( 'wcShippingBigProducts' ) ) {
class wcShippingBigProducts {
// disable free sipping if "big products" in cart
// "big products" has shipping class 'productos-grandes'
function activate() {
add_filter( 'woocommerce_package_rates', array( $this, 'shippingBigProducts' ), 10, 2 );
}
function shippingBigProducts( $rates, $package ) {
$has_big_products = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = wc_get_product($cart_item['product_id']);
if( $product->get_shipping_class() == 'productos-grandes'){
$has_big_products = true;
break;
}
}
if($has_big_products){
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
if('free_shipping' !== $rate->method_id){
$new_rates[$rate_id] = $rate;
}
}
return $new_rates;
}
return $rates;
}
}
$shipping_big_products = new wcShippingBigProducts();
$shipping_big_products->activate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment