显示为 WooCommerce 产品设置的所有产品属性的简码

问题描述

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

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

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

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

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

For example,at least in my version of Firefox,this Android documentation link
doesn't take me directly to the method [with percent encoding](https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29). 
But this one with a [reference-style link][2] does.

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

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