skip to Main Content

I am using WooCommerce and I want to get the current product attribute URL to be used on the single product page, replacing the code <category_url>/?filter_preco= by the URL.

Here is my code:

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);
            }
        }
    }
}

Any idea?

2

Answers


  1. Chosen as BEST ANSWER

    I got the solution by using this (I am getting a error when the product has no category)

    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);
            }
        }
    }
    

    }


  2. There are some mistakes in your code… Use this replacement code that uses get_term_link() WordPress function to get the term link as follows:

    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;
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should better work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search