【问题标题】:Display the variations weight on a variable product page in Woocommerce?在 Woocommerce 的可变产品页面上显示变体重量?
【发布时间】:2019-02-17 14:31:15
【问题描述】:
当用户点击我的产品变体下拉列表中的产品变体时,我还想知道每个变体的重量。我该怎么做?我一直在尝试这样做,但它不起作用:
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;
}
【问题讨论】:
标签:
php
wordpress
woocommerce
attributes
variations
【解决方案1】:
以下代码将附加到变体格式的价格、变体格式的重量:
// 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 文件中。经过测试并且可以工作。
【解决方案2】:
我的解决方案基于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;
}