Created
October 3, 2019 21:28
-
-
Save paaljoachim/e520bc25c2608eb06ae92c3eefd0a0e1 to your computer and use it in GitHub Desktop.
WooCommerce cart: Change shipping rate based on contents count and another example based on total purchased.
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
// Change shipping based on quantity purchased. | |
// https://easywebdesigntutorials.com/adjust-shipping-price-when-quantity-changes/ | |
// https://businessbloomer.com/woocommerce-setup-tiered-shipping-rates-order-amount/ | |
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 ); | |
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { | |
$threshold1 = 2; | |
$threshold2 = 3; | |
if ( WC()->cart->get_cart_contents_count() < $threshold1 ) { | |
unset( $rates['flat_rate:6'], $rates['flat_rate:8'] ); | |
} elseif ( WC()->cart->get_cart_contents_count() < $threshold2 ){ | |
unset( $rates['flat_rate:1'], $rates['flat_rate:8'] ); | |
} else { | |
unset( $rates['flat_rate:1'], $rates['flat_rate:6'] ); | |
} | |
return $rates; | |
} | |
/* Change shipping based on total cost purchased. */ | |
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 ); | |
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { | |
$threshold1 = 2000; | |
$threshold2 = 4000; | |
if ( WC()->cart-> cart_contents_total < $threshold1 ) { | |
unset( $rates['flat_rate:9'], $rates['flat_rate:10'] ); | |
} elseif ( WC()->cart-> cart_contents_total < $threshold2 ) { | |
unset( $rates['flat_rate:8'], $rates['flat_rate:10'] ); | |
} else { | |
unset( $rates['flat_rate:8'], $rates['flat_rate:9'] ); | |
} | |
return $rates; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment