在 Woocommerce 的可变产品页面上显示变体权重?

Display the variations weight on a variable product page in Woocommerce?

当用户点击我的产品变体下拉列表中的产品变体时,我还想知道每个变体的权重。我该怎么做?我一直在尝试这样做,但它不起作用:

add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
function wb_change_product_html( $price ) {
    global $product;
    $weight = $product->get_weight();
    $price_html = '<span class="amount">' . $price . $weight . '</span>';
    return $price_html;
}

以下代码将附加到变体格式化价格,变体格式化重量:

// Append the formatted variation weight to the variation formatted price
add_filter('woocommerce_available_variation', 'display_variation_weight', 10, 3 );
function display_variation_weight( $variation_data, $product, $variation ) {
    $variation_data['price_html'] .= '<span class="weight">' . $variation_data['weight_html'] . '</span>';

    return $variation_data;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

我的解决方案基于 LoicTheAztec 答案。适用于两种类型的产品:简单和可变。测试和工作。

add_filter('woocommerce_get_price_html', 'display_product_weight', 10, 3 );
    function display_product_weight( $price, $product ) {
        $price .= '<span class="weight">' . $product->get_weight() . get_option('woocommerce_weight_unit'); '</span>';
    
        return $price;
    }