這有點奇怪,但產品重量似乎來自get_weight()
方法,其中有2個過濾器。您正在參考的產品ID也是woocommerce_product_weight
,它確實也有產品ID。
/**
* Returns the product's weight.
* @todo refactor filters in this class to naming woocommerce_product_METHOD
* @return string
*/
public function get_weight() {
return apply_filters('woocommerce_product_weight', apply_filters('woocommerce_product_get_weight', $this->weight ? $this->weight : ''), $this);
}
因此,你應該能夠過濾重量:
add_filter('woocommerce_product_weight', 'rs_product_get_weight', 10, 2);
function rs_product_get_weight($weight, $product) {
if($product->id == 999){
$weight = 45.67;
}
return $weight;
}
遺憾的是沒有用那個鉤子(imo)。你想重寫插入的權重並將其保存到數據庫,或者你只需要在模板中顯示一個不同的值(不用觸摸數據庫)? – d79