Created
October 26, 2015 09:26
-
-
Save WPprodigy/ea0cf7bcdf829534d206 to your computer and use it in GitHub Desktop.
Example code for manipulating shipping rates in WooCommerce
This file contains 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 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