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

时间:2018-08-20 18:33:12

标签: php wordpress woocommerce hook-woocommerce custom-taxonomy

我发现following code可以在产品详细信息页面上显示所有自定义属性(我需要具有特定的条形设计)。该代码的工作原理很吸引人,我有适当的CSS可以显示自定义属性的水平条。

  

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

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 ) . ': </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;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 20);

1 个答案:

答案 0 :(得分:1)

在以下代码中,您将首先在数组中定义所需的产品属性块,这些块将显示在单个产品页面中:

hover = HoverTool(tooltips=[('value',   '@value'), ('date', '@date{%F %H:%M}')],
                  formatters={'date': 'datetime'})

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

相关问题