【问题标题】:WooCommerce variable products: Display some variations values in an HTML tableWooCommerce 可变产品:在 HTML 表格中显示一些变体值
【发布时间】:2018-03-02 16:38:20
【问题描述】:

在 Woocommerce 中,我想创建一个函数,它为 变量产品的每个 variation 输出一个简单的 HTML 表,其中包含 heightwidthregular pricesale price >。

例如,假设变量 product 带有三个不同尺寸的变体,我需要让我的函数输出这个 HTML:

<table>
<thead>
    <tr>
        <th>Height</th>
        <th>Width</th>
        <th>Regular price</th>
        <th>Sale price</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>180cm</td>
        <td>100cm</td>
        <td>224€</td>
        <td>176€</td>
    </tr>
    <tr>
        <td>210cm</td>
        <td>125cm</td>
        <td>248€</td>
        <td>200€</td>
    </tr>
    <tr>
        <td>240cm</td>
        <td>145cm</td>
        <td>288€</td>
        <td>226€</td>
    </tr>
</tbody>

我不确定如何为此构建一个函数,因此我可以将其添加到 content-single-product.php 内的 woocommerce_after_single_product 操作中。

如何做到这一点?

非常感谢任何帮助。

【问题讨论】:

    标签: php html wordpress woocommerce variations


    【解决方案1】:

    更新 (2018-03-27 - 仅限于可变产品,避免错误)

    这是在 woocommerce_after_single_product 动作挂钩中实现挂钩的正确方法:

    add_action( 'woocommerce_after_single_product', 'custom_table_after_single_product' );
    function custom_table_after_single_product(){
        global $product;
    
       // Only for variable products
       if( ! $product->is_type('variable')) return; 
    
        $available_variations = $product->get_available_variations();
    
        if( count($available_variations) > 0 ){
    
            $output = '<table>
                <thead>
                    <tr>
                        <th>'. __( 'Height', 'woocommerce' ) .'</th>
                        <th>'. __( 'Width', 'woocommerce' ) .'</th>
                        <th>'. __( 'Regular price', 'woocommerce' ) .'</th>
                        <th>'. __( 'Sale price', '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']);
    
                $sale_price = $product_variation->get_sale_price();
                if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );
    
                $output .= '
                <tr>
                    <td>'. $product_variation->get_height() .'</td>
                    <td>'. $product_variation->get_width() .'</td>
                    <td>'. $product_variation->get_regular_price() .'</td>
                    <td>'. $sale_price .'</td>
                </tr>';
            }
            $output .= '
                </tbody>
            </table>';
    
            echo $output;
        }
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    所有代码都在 Woocommerce 3+ 上进行了测试并且可以正常工作。你可以view additional hooks here

    【讨论】:

    • 仅供参考,这会导致查看非可变产品时出现白屏。我还没有时间调试原因,但我想我会告诉你的(不确定你是否能够复制)。
    • 最新的。 ;)
    • 是否考虑过让它同时适用于可变产品和简单产品?例如,如果它是一个简单的,它只会显示 1 行?
    • 好吧,我对上面这个确切的请求没有用处。但是对于不同类型的数据:gyazo.com/08cc02cd7d13b2de2df368bc83386138 我想我可以为每个变体显示此信息。但如果产品是简单的产品,它仍然会以相同的结构显示此信息。因此,每组都有一个块:价格、尺寸、销售价格、库存
    • @LiocTheAztec,确定:stackoverflow.com/questions/49564967/…
    猜你喜欢
    • 2017-11-24
    • 2015-07-25
    • 2020-02-24
    • 2018-11-13
    • 2019-02-17
    • 1970-01-01
    • 2015-10-10
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多