Skip to content

Instantly share code, notes, and snippets.

@vanbo
Created November 2, 2017 15:52
Show Gist options
  • Save vanbo/cfed5d30b88ef7e1dd09f3d87730ded1 to your computer and use it in GitHub Desktop.
Save vanbo/cfed5d30b88ef7e1dd09f3d87730ded1 to your computer and use it in GitHub Desktop.
WooCommerce: Filter a product shipping weight
// 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;
}
@paoloyumul15
Copy link

Where do I put this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment