The following code in my functions.php file and does indeed change the weight for all products but I would like to isolate it to a specific product.
add_filter('woocommerce_product_get_weight', 'rs_product_get_weight', 10, 1);
function rs_product_get_weight($weight) {
$weight = 45.67;
return $weight;
}
Is there any way to determine the product ID in my filter function?
I am afraid to say it doesn’t work this way…
if you look at woocommerce get_weight function
Maybe you are referencing to and old version of woocommerce…
So, for example, if you want to change dinamically a cart product weight, you have to hook woocommerce_before_calculate_totals filter
and add this function
and so on…
It’s a little strange, but product weight seems to come from the
get_weight()
method which has 2 filters inside of it. The one you are referencing and alsowoocommerce_product_weight
which does indeed have the product ID also passed along.Therefore you should be able to filter the weight with: