在产品页面的 Table 中显示 WooCommerce 变体数据(尺寸、重量等)

Display WooCommerce Variation Data in Table on Product Page (Size, Weight etc.)

我做了一个 table,我在可变产品页面上显示了产品数据。这个想法非常直接,但是 - 我遇到了一个我无法弄清楚的错误。

我得到“Undefined variable: variation_id”,尽管我有它作为参数但我不知道如何解决它。

代码:

add_action( 'woocommerce_single_product_summary', 'variable_product_data_as_table', 5 );
function variable_product_data_as_table(){

global $product;

    if (! $product->is_type('variable')) return;

    $variation = wc_get_product($variation_id);
    $available_variations = $product->get_available_variations();
    if( count($available_variations) > 0 ) {

        $table_display = '<hr><table class="variable-data">
    <thead>
            <tr>
                <th style="text-align: center; font-weight: bold;">'. __( 'Length', 'woocommerce' ) .'</th>
                <th style="text-align: center; font-weight: bold;">'. __( 'Width', 'woocommerce' ) .'</th>
                <th style="text-align: center; font-weight: bold;">'. __( 'Height', 'woocommerce' ) .'</th>
                <th style="text-align: center; font-weight: bold;">'. __( 'Stock', 'woocommerce' ) .'</th>
                <th style="text-align: center; font-weight: bold;">'. __( 'Price', 'woocommerce' ) .'</th>
                <th style="color:red; text-align: center;" font-weight: bold;>'. __( 'Sale', 'woocommerce' ) .'</th>
            </tr>
        </thead>
    <tbody>';

    foreach( $available_variations as $variation ){

        $product_variation = wc_get_product($variation['variation_id']);

        $sale_price = $product_variation->get_sale_price();

        if( empty( $sale_price ) ) $sale_price = __( '', 'woocommerce' );

    $table_display .= '
        <tr>
            <td style="text-align: center;">'. $product_variation->get_length() .'</td>
            <td style="text-align: center;">'. $product_variation->get_width() .'</td>
            <td style="text-align: center;">'. $product_variation->get_height() .'</td>
            <td style="text-align: center;">'. $product_variation->get_stock_quantity() .'</td>
            <td style="text-align: center;">'. $product_variation->get_regular_price() .'</td>
            <td style="color: darkgreen; text-align: center; font-weight: bold; padding-left: 5px;">'. $sale_price .'</td>
        </tr>';
    }
    $table_display .= '
        </tbody>
    </table>';

    echo $table_display;
    echo '<hr>';
    }
}

通知实际上已经解决了这个问题,所以试试这个

function variable_product_data_as_table(){
    global $product;

    if (! $product->is_type('variable')) return;

    $available_variations = $product->get_available_variations();

    if( count($available_variations) > 0 ) {
        $table_display = '<hr><table class="variable-data">
        <thead>
                <tr>
                    <th style="text-align: center; font-weight: bold;">'. __( 'Length', 'woocommerce' ) .'</th>
                    <th style="text-align: center; font-weight: bold;">'. __( 'Width', 'woocommerce' ) .'</th>
                    <th style="text-align: center; font-weight: bold;">'. __( 'Height', 'woocommerce' ) .'</th>
                    <th style="text-align: center; font-weight: bold;">'. __( 'Stock', 'woocommerce' ) .'</th>
                    <th style="text-align: center; font-weight: bold;">'. __( 'Price', 'woocommerce' ) .'</th>
                    <th style="color:red; text-align: center;" font-weight: bold;>'. __( 'Sale', 'woocommerce' ) .'</th>
                </tr>
            </thead>
        <tbody>';

        foreach( $available_variations as $variation ) {

            $product_variation = wc_get_product( $variation['variation_id'] );

            $sale_price = $product_variation->get_sale_price();

            if( empty( $sale_price ) ) $sale_price = __( '', 'woocommerce' );

            $table_display .= '
            <tr>
                <td style="text-align: center;">'. $product_variation->get_length() .'</td>
                <td style="text-align: center;">'. $product_variation->get_width() .'</td>
                <td style="text-align: center;">'. $product_variation->get_height() .'</td>
                <td style="text-align: center;">'. $product_variation->get_stock_quantity() .'</td>
                <td style="text-align: center;">'. $product_variation->get_regular_price() .'</td>
                <td style="color: darkgreen; text-align: center; font-weight: bold; padding-left: 5px;">'. $sale_price .'</td>
            </tr>';
        }

        $table_display .= '
        </tbody>
        </table>';

        echo $table_display;
        echo '<hr>';
    }
}
add_action( 'woocommerce_single_product_summary', 'variable_product_data_as_table', 5, 0 );