如何在 WooCommerce 单个产品页面上获取产品属性链接

时间:2021-02-13 15:29:05

标签: php wordpress woocommerce product taxonomy-terms

我正在使用 WooCommerce,我想获取要在单个产品页面上使用的当前产品属性 URL,用 URL 替换代码 <category_url>/?filter_preco=

这是我的代码:

add_action( 'woocommerce_single_product_summary', 'cj_show_attribute_links', 4 );

function cj_show_attribute_links() {
    global $post, $product;
    $attribute_names = array( 'pa_preco' ); // Insert attribute names here

    foreach ( $attribute_names as $attribute_name ) {
        $taxonomy = get_taxonomy( $attribute_name );

        if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
            $terms = wp_get_post_terms( $post->ID, $attribute_name );
            $terms_array = array();

            if ( ! empty( $terms ) ) {
                foreach ( $terms as $term ) {
                   $archive_link =  $term->slug;
                   $base = '<category_url>?filter_preco=';
                   $full_line = '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="'. $base .'' . $archive_link . '">'. $term->name . '</a></h4><div class="is-divider small"></div>';
                   array_push( $terms_array, $full_line );
                }

                echo ' ' . implode( $terms_array);
            }
        }
    }
}

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您的代码中存在一些错误...使用此替换代码使用 get_term_link() WordPress 函数获取术语链接,如下所示:

add_action( 'woocommerce_single_product_summary', 'display_linked_product_attributes', 4 );
function display_linked_product_attributes() {
    global $post, $product;

    $taxonomy = array( 'pa_preco' ); // Here define product attribute(s) taxonomy(ies)

    foreach ( $attribute_names as $attribute_name ) {
        $terms  = wp_get_post_terms( get_the_ID(), $taxonomy );
        $output = '';
        
        foreach ( $terms as $term ) {
           $link    = get_term_link( $term, $taxonomy );
           $output .= '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="'. $link . '">'. $term->name . '</a></h4><div class="is-divider small"></div>';
        }
        echo ' ' . $output;
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该会更好地工作。

答案 1 :(得分:0)

我通过使用这个得到了解决方案(当产品没有类别时我收到错误)

function pagina_produto_embaixo_titulo() {
global $post;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here

foreach ( $attribute_names as $attribute_name ) {
    $taxonomy = get_taxonomy( $attribute_name );
    

    if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
        $terms = wp_get_post_terms( $post->ID, $attribute_name );
        $terms_array = array();
        
        

        if ( ! empty( $terms ) ) {
            foreach ( $terms as $term ) {
               $archive_link =  $term->slug;
               $terms = get_the_terms( $post->ID, 'product_cat' );
               $link = get_term_link( $terms[0]->term_id, 'product_cat' );
               $base = $link .'?filter_preco=';
               //erro nas 2 linhas acima, quando o produto nao tem categoria, e na pagina da loja, deixa de funcuonar algus coisas.
               $full_line = '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="https://www.cotacaodebrindes.com.br/o-que-e-ustar/" target="_blank">O que é µStar+?</a> | <a href="'. $base .'' . $archive_link . '">'. $term->name . '</a></h4><div>';
               array_push( $terms_array, $full_line );
            }

            echo ' ' . implode( $terms_array);
        }
    }
}

}

相关问题