【问题标题】:Shortcode displaying product attributes list in WooCommerce single products在 WooCommerce 单品中显示产品属性列表的简码
【发布时间】:2021-01-30 12:50:32
【问题描述】:

所以我一直在努力寻找有关这方面的任何信息,但看不到任何明显的方法。我想在产品描述中有一个表格,列出我设置的某些属性。

例如,在描述中,我将在标题 A 后跟一些文本和一个具有属性 1、2 和 3 的表格,然后在标题 B 下一些文本和属性 4、5 和 6。

我认为最简单的方法是创建一个短代码,所以我尝试过并且明显错过了一些重要的东西,因为当我使用它时它根本没有显示任何东西。

add_action( 'woocommerce_single_product_summary', 'show_attribute_group_a', 45 );

add_shortcode( 'show_attributes_a', 'show_attribute_group_a' );
function show_attribute_group_a() {
    global $product;
    $attribute1 = $product->get_attribute('attribute1');
    $attribute2 = $product->get_attribute('attribute2');
    ?>
    <table class="">
        <tr class="">
            <th class="">Attribute 1</th>
            <td class=""><?php echo $attribute1; ?></td>
        </tr>
        <tr class="">
            <th class="">Attribute 2</th>
            <td class=""><?php echo $attribute2; ?></td>
        </tr>
    </table>
    <?php
}

那么我在哪里尝试拉出我使用过的属性的 slug 是正确的吗?但是,即使那部分是错误的,我仍然希望看到一张空桌子。显然存在根本性错误,我看不出是什么。

【问题讨论】:

    标签: php html wordpress woocommerce shortcode


    【解决方案1】:

    在构建简码时,输出永远不会回显,而是返回。请尝试以下方法:

    // The shortcode
    add_shortcode( 'show_attributes_a', 'attribute_group_a_shortcode' );
    function attribute_group_a_shortcode() {
        global $product;
    
        $attribute1  = $product->get_attribute('attribute1');
        $attribute2  = $product->get_attribute('attribute2');
        $output_html = ''; // Initializing
    
        if ( ! empty($attribute1) || ! empty($attribute2) ) {
            $output_html .= '<table class="">';
    
            if ( ! empty($attribute1) ) {
                $output_html .= '<tr class="">
                    <th class="">' . __("Attribute 1", "woocommerce") . '</th>
                    <td class="">' . $attribute1 . '</td>
                </tr>';
            }
    
            if ( ! empty($attribute2) ) {
                $output_html .= '<tr class="">
                    <th class="">' . __("Attribute 2", "woocommerce") . '</th>
                    <td class="">' . $attribute2 . '</td>
                </tr>';
            }
    
            $output_html .= '</table>';
        }
        return $output_html; // Always use "return" in a shortcode, never use "echo"
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    现在您可以在文本编辑器中使用 shorcode [show_attributes_a] 进行产品描述或简短描述,在单个产品页面上显示产品属性的自定义表。

    或者您可以使用以下代码在产品选项卡之前显示产品属性的自定义表:

    // Display the shortcode output in Single product pages
    add_action( 'woocommerce_single_product_summary', 'display_table_attribute_group_a', 45 );
    function display_table_attribute_group_a() {
        echo do_shortcode('[show_attributes_a]');
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 非常完美,谢谢!显然,由于我的声誉,我不能赞成你的回答,但还是要感谢你。
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2020-01-25
    • 2022-01-02
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多