【问题标题】:Display WooCommerce Variation Data in Table on Product Page (Size, Weight etc.)在产品页面的表格中显示 WooCommerce 变化数据(尺寸、重量等)
【发布时间】:2020-09-30 17:17:49
【问题描述】:

我已经制作了一个表格,我在可变产品页面上显示产品数据。这个想法非常简单,但是 - 我遇到了一个我无法弄清楚的错误。

我得到“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>';
    }
}

【问题讨论】:

    标签: php wordpress woocommerce html-table product


    【解决方案1】:

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

    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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2019-04-08
      • 2019-02-17
      • 2018-03-01
      • 2015-08-08
      • 1970-01-01
      • 2019-02-11
      相关资源
      最近更新 更多