简体   繁体   English

在 Woocommerce 的单个产品页面上显示特定的自定义产品属性

[英]Display specific custom product attributes on single product pages in Woocommerce

I found the following code<\/a> to display all custom attributes on a product detail page (with a specific bar-style design that I need).我发现以下代码<\/a>可以在产品详细信息页面上显示所有自定义属性(具有我需要的特定条形设计)。 The code works like a charm and I have the proper CSS to display horizontal bars of my custom attributes.代码就像一个魅力,我有适当的 CSS 来显示我的自定义属性的水平条。

Problem I have is that I only want to display specific named attributes and don't know how to change the loop to do that...我遇到的问题是我只想显示特定的命名属性并且不知道如何更改循环来做到这一点......

<\/blockquote>

 function isa_woocommerce_all_pa(){ global $product; $attributes = $product->get_attributes(); if ( ! $attributes ) { return; } $out = '<ul class="taste-attributes">'; foreach ( $attributes as $attribute ) { \/\/ skip variations if ( $attribute->get_variation() ) { continue; } $name = $attribute->get_name(); if ( $attribute->is_taxonomy() ) { $terms = wp_get_post_terms( $product->get_id(), $name, 'all' ); \/\/ get the taxonomy $tax = $terms[0]->taxonomy; \/\/ get the tax object $tax_object = get_taxonomy($tax); \/\/ get tax label if ( isset ( $tax_object->labels->singular_name ) ) { $tax_label = $tax_object->labels->singular_name; } elseif ( isset( $tax_object->label ) ) { $tax_label = $tax_object->label; \/\/ Trim label prefix since WC 3.0 if ( 0 === strpos( $tax_label, 'Product ' ) ) { $tax_label = substr( $tax_label, 8 ); } } $out .= '<li class="' . esc_attr( $name ) . '">'; $out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': 

'; $tax_terms = array(); foreach ( $terms as $term ) { $single_term = esc_html( $term->name ); \/\/ Insert extra code here if you want to show terms as links. array_push( $tax_terms, $single_term ); } $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '<\/span><progress value="' . implode(', ', $tax_terms) . '" max="10"><div class="progress-bar"><span style="width:' . implode(', ', $tax_terms) . '0%">' . implode(', ', $tax_terms) . '<\/span><\/progress><\/li>'; } else { $value_string = implode( ', ', $attribute->get_options() ); $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">'; $out .= '<p class="attribute-label">' . $name . ':

'; $out .= '<progress value="' . esc_html( $value_string ) . '" max="10"><\/progress><\/li>'; } } $out .= '<\/ul>'; echo $out; } add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 20);<\/code><\/pre>"

In the following code you will define first the desired product attributes slugs in an array, that will get displayed in single product pages:在以下代码中,您将首先在数组中定义所需的产品属性 slug,它将显示在单个产品页面中:

add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
    // HERE define the desired product attributes to be displayed
    $defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');

    global $product;
    $attributes = $product->get_attributes();

    if ( ! $attributes ) {
        return;
    }

    $out = '<ul class="taste-attributes">';

    foreach ( $attributes as $attribute ) {

        // Get the product attribute slug from the taxonomy
        $attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );

        // skip all non desired product attributes
        if ( ! in_array($attribute_slug, $defined_attributes) ) {
            continue;
        }

        // skip variations
        if ( $attribute->get_variation() ) {
            continue;
        }

        $name = $attribute->get_name();

        if ( $attribute->is_taxonomy() ) {

            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            // get the taxonomy
            $tax = $terms[0]->taxonomy;
            // get the tax object
            $tax_object = get_taxonomy($tax);
            // get tax label
            if ( isset ( $tax_object->labels->singular_name ) ) {
                $tax_label = $tax_object->labels->singular_name;
            } elseif ( isset( $tax_object->label ) ) {
                $tax_label = $tax_object->label;
                // Trim label prefix since WC 3.0
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                   $tax_label = substr( $tax_label, 8 );
                }                
            }

            $out .= '<li class="' . esc_attr( $name ) . '">';
            $out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
            $tax_terms = array();

            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                // Insert extra code here if you want to show terms as links.
                array_push( $tax_terms, $single_term );
            }

            $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
            '" max="10"><div class="progress-bar"><span style="width:'
            . implode(', ', $tax_terms) . '0%">'
            . implode(', ', $tax_terms) . '</span></div></progress></li>';

        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<p class="attribute-label">' . $name . ': </p> ';
            $out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
        }
    }

    $out .= '</ul>';

    echo $out;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

i want to display some custom attributes on the single product page.我想在单个产品页面上显示一些自定义属性。 This is the first code i found that works.这是我发现的第一个有效的代码。 But i don't want bars to show up but just the text values of the atttributes.但我不希望显示条,而只是显示属性的文本值。 Could you help me to change the bars to text?你能帮我把栏改成文字吗? Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 Woocommerce 档案页面上显示特定的产品属性 - Display specific product attributes on Woocommerce archives pages 在自定义单个帖子中显示 Woocommerce 产品属性 - Display Woocommerce product attributes in a custom single post 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 获取特定产品属性并将其显示在Woocommerce单一产品页面中 - Get specific product attribute and display it in Woocommerce Single Product Pages 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 在 WooCommerce 单一产品页面上为特定产品添加自定义内容 - Add custom content for a specific product on WooCommerce single product pages 显示 WooCommerce 自定义产品属性和单个产品的所有条款 - Display WooCommerce Custom Product Attributes and all terms on single products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM