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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.