PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to get product attribute link on WooCommerce single product page

 May 09, 2022     php, product, taxonomy-terms, woocommerce, wordpress     No comments   

Issue

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?


Solution

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

}



Answered By - Reginaldo Higa
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing