繁体   English   中英

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

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

我发现以下代码<\/a>可以在产品详细信息页面上显示所有自定义属性(具有我需要的特定条形设计)。 代码就像一个魅力,我有适当的 CSS 来显示我的自定义属性的水平条。

我遇到的问题是我只想显示特定的命名属性并且不知道如何更改循环来做到这一点......

在以下代码中,您将首先在数组中定义所需的产品属性 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;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。 测试和工作。

我想在单个产品页面上显示一些自定义属性。 这是我发现的第一个有效的代码。 但我不希望显示条,而只是显示属性的文本值。 你能帮我把栏改成文字吗? 谢谢!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM