Skip to content

Instantly share code, notes, and snippets.

@WPprodigy
Created October 26, 2015 09:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WPprodigy/ea0cf7bcdf829534d206 to your computer and use it in GitHub Desktop.
Save WPprodigy/ea0cf7bcdf829534d206 to your computer and use it in GitHub Desktop.
Example code for manipulating shipping rates in WooCommerce
function wc_ninja_manipulate_shipping( $rates, $package ) {
// All available rates and their costs
//print_r($rates);
// All products in the cart and their costs
//print_r($package);
// Example of manipulating the cost of
// a shipping method based on the above variables.
if ( isset( $rates['flat_rate'] ) ) {
$flat_rate = $rates['flat_rate'];
$cart_products = $package['contents'];
// Loop through each product in the cart
foreach ( $cart_products as $product ) {
if ( $product['product_id'] =='70' ) {
// If product #70 is in the cart,
// add it's cost to the flat rate shipping method.
$products_cost = $product['line_total'];
$flat_rate->cost = $flat_rate->cost + $products_cost;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_manipulate_shipping', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment