我有一个使用 Woocommcerce 构建的 WordPress 网站。我有一个具有四个属性的可变产品。我在这个产品中设置了一个默认变体,它显示在产品页面上。现在我只想在“additional_info”选项卡中显示默认设置属性术语。例如:我有四个术语(红色、黑色、白色、绿色)的颜色属性。我将默认设置为红色。我想在附加信息选项卡中只显示属性颜色的这个“红色”术语。
我用这段代码对价格做了类似的事情:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$available_variations = $product->get_available_variations();
$selectedPrice = '';
$dump = '';
foreach ( $available_variations as $variation )
{
// $dump = $dump . '<pre>' . var_export($variation['attributes'], true) . '</pre>';
$isDefVariation=false;
foreach($product->get_default_attributes() as $key=>$val){
// $dump = $dump . '<pre>' . var_export($key, true) . '</pre>';
// $dump = $dump . '<pre>' . var_export($val, true) . '</pre>';
if($variation['attributes']['attribute_'.$key]==$val){
$isDefVariation=true;
}
}
if($isDefVariation){
$price = $variation['display_price'];
}
}
$selectedPrice = wc_price($price);
// $dump = $dump . '<pre>' . var_export($available_variations, true) . '</pre>';
return $selectedPrice . $dump;
}
有什么提示吗?
答案 0 :(得分:0)
此代码粘贴在您的活动主题functions.php 文件中。并将您的属性名称替换为 'pa_color' 属性。 隐藏您自定义的属性。
<?php function action_woocommerce_product_additional_information( $wc_display_product_attributes ) {
$defult_arr_name = $wc_display_product_attributes->get_default_attributes();
?>
<table class="woocommerce-product-attributes">
<tbody>
<?php if($defult_arr_name['pa_color']) { ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_display_price">
<th class="woocommerce-product-attributes-item__label">Color</th>
<td class="woocommerce-product-attributes-item__value"><p><?php echo $defult_arr_name['pa_color']; ?></p></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
};
add_action( 'woocommerce_product_additional_information', 'action_woocommerce_product_additional_information', 10, 2 );