Created
November 2, 2017 15:52
-
-
Save vanbo/cfed5d30b88ef7e1dd09f3d87730ded1 to your computer and use it in GitHub Desktop.
WooCommerce: Filter a product shipping weight
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
// Filters both variations and simple products with the same function | |
add_filter( 'woocommerce_product_variation_get_weight', 'filter_product_weight', 10, 2 ); | |
add_filter( 'woocommerce_product_get_weight', 'filter_product_weight', 10, 2 ); | |
/** | |
* @param float $weight | |
* @param WC_Product $product | |
* | |
* @return int | |
*/ | |
function filter_product_weight( $weight, $product ) { | |
// 1. Check if the product has a set weight already. May be you want to add to the weight | |
if ( 0 < $weight ) { | |
$weight += 100; | |
} | |
// 2. Assuming you did add a setting in your product edit | |
// screen so the admin can change the additional weight you will add, check the setting and add the weight | |
if ( 0 < $product->get_meta( 'additional_weight', true ) ) { | |
$weight += $product->get_meta( 'additional_weight', true ); | |
} | |
return $weight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where do I put this?