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

推荐答案

try 执行以下操作,仅当表列有值时才显示该表列:

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;
    
    // Get available variations
    $variations = $product->get_available_variations('object');       

    if( count($variations) > 0 ){
        // Set the table columns heading in an array
        $table_head = array(
            'image'         => __( 'Image', 'woocommerce' ),
            'sku'           => __( 'SKU', 'woocommerce' ),
            'colour'        => __( 'Colour', 'woocommerce' ),
            'technology'    => __( 'Technology', 'woocommerce' ),
            'posneg'        => __( 'Positive/Negative', 'woocommerce' ),
        );

        $table_columns = array_keys($table_head);
        $table_data = $variation_ids = array(); // Initializing

        // Set the table columns values in an array sorted by column
        foreach ( $variations as $variation ) {
            $variation_id    = $variation->get_id();
            $variation_ids[] = $variation_id;

            if ( $image = $variation->get_image('snippets') ) {
                $table_data['image'][$variation_id] = $image;
            }
            if ( $sku = $variation->get_sku() ) {
                $table_data['sku'][$variation_id] = $sku;
            }
            if ( $colour = $variation->get_attribute('pa_colour_range') ) {
                $table_data['colour'][$variation_id] = $colour;
            }
            if ( $technology = $variation->get_attribute('pa_technology') ) {
                $table_data['technology'][$variation_id] = $technology;
            }
            if ( $posneg = $variation->get_attribute('pa_posneg') ) {
                $table_data['posneg'][$variation_id] = $posneg;
            }
        }

        // Output start
        $output = '<table>
            <thead>
                <tr>';

                // Loop through table head columns
                foreach ( $table_head as $column => $column_label ) {
                    if( isset($table_data[$column]) ) {
                        $output .= '<th>'. $column_label .'</th>';
                    }
                }
                $output .= '</tr>
            </thead>
            <tbody>';

        // Loop through variations Ids
        foreach( $variation_ids as $variation_id ){
            $output .= '<tr>';

            // Loop through table columns
            foreach ( $table_columns as $column ) {
                if( isset($table_data[$column]) ) {
                    $output .= '<td>'. $table_data[$column][$variation_id] .'</td>';
                }
            }
            $output .= '</tr>';
        }
        $output .= '
            </tbody>
        </table>';

        echo $output;
    }
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

Php相关问答推荐

p a/a/p的x路径 Select ,但不是p somethext a链接/a或某些文本/p>

make dd()如何正确工作?¨

PHP:PHP手册-Manual

如何删除Foreach语句中的重复值?

使用MySQLi使用连接字符串设置字符集

删除了数组中的值,而不是多个数组

这是在可召回的背景下吗?

获取作者';在WordPress中使用PHP

如何使用PHP从TXM(XML)标记中读取特定属性

将自定义字段添加到管理产品 在 WooCommerce 3.2+ 中批量编辑

.htaccess 重写规则问题

如果 Select 的县不是store 所在的县,如何隐藏本地自提?

有没有办法获取触发流操作的页面的 URI?

根据支付网关选项更改 WooCommerce Checkout 上的立即付款按钮文本

将 WooCommerce 0 销售价格替换为自定义文本,并保留常规价格删除线

如何在自定义消息中包含 WooCommerce 选定的产品变体详细信息

为什么 AJAX 请求没有通过 is_ajax_request() codeigniter 的条件?

如果 static:: 被解析为B,为什么 PHP 会执行A的方法

我正在使用 Baryryvdh/laravel-snappy 从 HTML 导出 PDF 它正在工作但缩小了 pdf 文件

hasOne/belongsToMany 或多对多