【问题标题】:Shortcode that display all product attributes set for a WooCommerce product显示为 WooCommerce 产品设置的所有产品属性的简码
【发布时间】:2021-10-13 23:00:03
【问题描述】:

我一直在寻找一种方法来做到这一点,但不幸的是我找不到任何东西。

我试图在单个产品的自定义位置显示所有产品的属性和值,用管道分隔(因此我正在考虑创建一个短代码,这样我就可以将它放在我想要的任何地方) .输出将是这样的:

品牌:雷诺 |型号:12 |年份:1973 年

Woocommerce 模板上的代码product-attributes.php 列出了当前产品在单品页面的属性,但是会列出一些我不想要的样式我不要。

我想用该代码创建一个简码,即:

<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
    
            <?php echo wp_kses_post( $product_attribute['label'] ); ?>: <?php echo wp_kses_post( $product_attribute['value'] ); ?> | 
    
    <?php endforeach; ?>

如何使用它创建短代码?我知道短代码的通用代码,但我不知道如何将上述代码实际集成到其中:


function custom_attributes_product_page() { 
 
// integrate the required code

// Output needs to be return
return 
} 
// register shortcode
add_shortcode('custom-attributes', 'custom_attributes_product_page'); 

如果这个简码能像我上面所说的那样列出由一列分隔的属性及其值,那就太好了(怎么做?)

非常感谢任何帮助。

【问题讨论】:

    标签: php function woocommerce shortcode taxonomy-terms


    【解决方案1】:

    尝试以下短代码,它将显示为产品设置的所有产品属性及其值,同时处理自定义属性:

    function get_product_attributes_shortcode($atts ) {
        // Extract shortcode attributes
        extract( shortcode_atts( array(
            'id'    => get_the_ID(),
        ), $atts, 'display-attributes' ) );
    
        global $product;
    
        if ( ! is_a($product, 'WC_Product') ) {
            $product = wc_get_product( $id );
        }
    
        if ( is_a($product, 'WC_Product') ) {
            $html = []; // Initializing
    
            foreach ( $product->get_attributes() as $attribute => $values ) {
                $attribute_name = wc_attribute_label($values->get_name());
                $attribute_data = $values->get_data();
                $is_taxonomy    = $attribute_data['is_taxonomy'];
    
                $option_values    = array(); // Initializing
    
                // For taxonomy product attribute values
                if( $is_taxonomy ) {
                    $terms = $values->get_terms(); // Get attribute WP_Terms
    
                    // Loop through attribute WP_Term(s)
                    foreach ( $terms as $term ) {
                        $term_link       = get_term_link( $term, $attribute );
                        $option_values[] = '<a href="'.$term_link.'">'.$term->name.'</a>';
                    }
                }
                // For "custom" product attributes values
                else {
                    // Loop through attribute option values
                    foreach ( $values->get_options() as $term_name ) {
                        $option_values[] = $term_name;
                    }
                }
    
                $html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
            }
    
            return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
        }
    }
    add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
    

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

    用法: [display-attributes] 或带有定义的产品 ID [display-attributes id="254"]

    您将获得如下显示:BRAND: RENAULT | 型号:12 | 年份:1973

    如果您不想要链接的术语,请替换:

                        $term_link       = get_term_link( $term, $attribute );
                        $option_values[] = '<a href="'.$term_link.'">'.$term->name.'</a>';
    

    通过这个:

                        $option_values[] = $term->name;
    

    【讨论】:

    • 非常感谢!你真的很棒,很友善,能提供如此完整的答案
    • @whitelord 这既简单又快捷,因为我主要在其他地方使用它,我只是更改了所需的输出。 This recent thread 有点复杂,例如:)
    • 您需要在 foreach 循环中添加一个计数器:在 foreach 循环之前您将其定义为 $count = 0;... 然后在 foreach 循环的开头添加 $count++;... 然后在结尾您添加的 foreach 循环 echo ( $count % 4 ) == 0 ? '&lt;br&gt;' : '';,这将打破每 4 个属性的行。你可以vote 2 or 3 max :)
    猜你喜欢
    • 2021-01-12
    • 2021-01-30
    • 1970-01-01
    • 2020-10-12
    • 2018-04-23
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多