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

巧了我就是萌 提交于 2021-01-29 03:50:44

问题


When the user clicks on a product variant of a dropdown of my products' variants, I would like to also have the weight of each variant. How would I do this? I have been trying to do it like this, but it is not working:

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;
}

回答1:


The following code will append to the variation formatted price, the variation formatted weight:

// 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;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/52303817/display-the-variations-weight-on-a-variable-product-page-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!