基于WooCommerce variable products: Display some variations values in an HTML table个代码答案,我有这个表显示正确,但我只希望属性列显示是否存在,我如何修改它来做到这一点?
例如,如果有任何变体将"技术"作为选项,则显示"技术"列,如果不隐藏它.
add_action( 'woocommerce_after_single_product_summary', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
global $product;
if( ! $product->is_type('variable')) return;
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
$output = '<table>
<thead>
<tr>
<th>'. __( 'Image', 'woocommerce' ) .'</th>
<th>'. __( 'SKU', 'woocommerce' ) .'</th>
<th>'. __( 'Colour', 'woocommerce' ) .'</th>
<th>'. __( 'Technology', 'woocommerce' ) .'</th>
<th>'. __( 'Positive/Negative', 'woocommerce' ) .'</th>
</tr>
</thead>
<tbody>';
foreach( $available_variations as $variation ){
// Get an instance of the WC_Product_Variation object
$product_variation = wc_get_product($variation['variation_id']);
$output .= '
<tr>
<td>'. $product_variation->get_image('snippets') .'</td>
<td>'. $product_variation->get_sku() .'</td>
<td>'. $product_variation->get_attribute('pa_colour_range') .'</td>
<td>'. $product_variation->get_attribute('pa_technology') .'</td>
<td>'. $product_variation->get_attribute('pa_posneg') .'</td>
</tr>';
}
$output .= '
</tbody>
</table>';
echo $output;
}
}