在 Woocommerce 中,我添加了一个挂钩函数,该函数显示分配给 Woocommerce 存档页面上可变产品的变体的产品属性:
add_action( 'woocommerce_after_shop_loop_item', 'stock_variations_loop' );
function stock_variations_loop(){
global $product;
if ( $product->get_type() == 'variable' ) {
foreach ($product->get_available_variations() as $key) {
$attr_string = '';
foreach ( $key['attributes'] as $attr_name => $attr_value) {
$attr_string[] = $attr_value;
}
if ( $key['max_qty'] > 0 ) {
echo '<div class="sizeVariantCat">' . implode(', ', $attr_string).'</div>';
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但有些数据很混乱……我只想显示变体值的“尺寸”产品属性,而不是所有具有“尺寸”属性的产品。