如何将 WooCommerce 产品属性术语名称显示为链接术语?

问题描述 投票:0回答:1

我想在单个产品选项卡中显示产品属性,但作为具有此属性的产品的链接。

我发现了这样的代码:

function cw_woo_attribute(){
    global $product;
    $attributes = $product->get_attributes();
    if ( ! $attributes ) {
        return;
    }
    $display_result = '';
    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
        $name = $attribute->get_name();
        if ( $attribute->is_taxonomy() ) {
            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            $cwtax = $terms[0]->taxonomy;
            $cw_object_taxonomy = get_taxonomy($cwtax);
            if ( isset ($cw_object_taxonomy->labels->singular_name) ) {
                $tax_label = $cw_object_taxonomy->labels->singular_name;
            } elseif ( isset( $cw_object_taxonomy->label ) ) {
                $tax_label = $cw_object_taxonomy->label;
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                    $tax_label = substr( $tax_label, 8 );
                }
            }
            $display_result .= $tax_label . ': ';
            $tax_terms = array();
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                array_push( $tax_terms, $single_term );
            }
            $display_result .= implode(', ', $tax_terms) .  '<br />';
        } else {
            $display_result .= $name . ': ';
            $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
        }
    }
    echo $display_result;
}
add_action('woocommerce_single_product_summary', 'cw_woo_attribute', 25);

它有效!

但是如何修改此代码,以便各个属性的名称成为指向分配了该属性的所有产品的链接?

我之前使用过 Isabel Castillo 的“WooCommerce 显示属性”,但它开始与某些内容发生冲突,并在“添加到购物车”按钮上显示错误。

php wordpress woocommerce product taxonomy-terms
1个回答
0
投票

请注意,您使用的代码可以高度简化。

使用 WordPress

get_term_link()
将允许您在可能的情况下显示链接的术语名称。

尝试以下操作:

add_action( 'woocommerce_single_product_summary', 'woocommerce_single_product_linked_attributes', 25 );
function woocommerce_single_product_linked_attributes(){
    global $product;

    $attributes  = $product->get_attributes();
    $html_output = '';

    if ( empty($attributes) ) {
        return;
    }

    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
        
        if ( $attribute->is_taxonomy() ) {
            $values    = array();
            $taxonomy  = $attribute->get_taxonomy();

            foreach ( $attribute->get_terms() as $term ) {
                if ( $term_link = get_term_link($term, $taxonomy) ) {
                    $values[] = '<a href="'. esc_url( $term_link ) . '">'. esc_html( $term->name ) . '</a>';
                } else {
                    $values[] = esc_html( $term->name );
                }
            }
            $html_output .=  wc_attribute_label($taxonomy) . ': ' . implode(', ', $values) . '<br>';
        } else {
            $html_output .= $attribute->get_name() . ': ' . esc_html( implode(', ', $attribute->get_options()) ) . '<br>';
        }
    }
    echo $html_output;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.