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
I got the solution by using this (I am getting a error when the product has no category)
}
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:Code goes in functions.php file of the active child theme (or active theme). It should better work.