Display All Products Tagged By Current Product’s Tags
I am trying to achieve a functionality where certain products on my WooCommerce store have a specific (just single) tag – multiple products of similar type can have same tag.
What I am trying to achieve is when a product is viewed, all other products with similar tag are displayed at the selected location. So far, I have managed to show all products with tags on the single product page but, I am not able to filter to show the products with the tags that match the current product’s tags.
[Example]:
I am on a product Okley 3008 Black with tag: Okley 3008
2 other products have the same tag i.e. Okley 3008
So, a total of 3 products showed being showed but, all products are currently showing on the product page (Below add to cart in drop down (currently broken) and thumbnails)
So, what I want help is when I am viewing Okley 3008, all products with Okley 3008 tag should be showed instead of displaying all products with different tags.
Here is my code:
function woo_products_by_tags_shortcode( $atts, $content = null ) {
global $post;
global $product;
$product_attributes = $product->get_attributes();
// Get attribuets
extract(shortcode_atts(array(
"tags" => ''
), $product_attributes));
ob_start();
// Define Query Arguments
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_tag' => $tags
);
// Create the new query
$loop = new WP_Query( $args );
// Get products number
$product_count = $loop->post_count;
// If results
if( $product_count > 0 ) :
echo '<select onchange="javascript:location.href = this.value;">';
// Start the loop
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
global $post;
//echo "<p>" . $thePostID = $post->post_title. " </p>";
echo "<option class='product_title'><a href=".get_permalink($post->ID).">". $thePostID = $post->post_title. "</a></option>";
/*if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else
echo '<div class="matched_product_image"><img src="'.$woocommerce->plugin_url().'/assets/images/placeholder.png" alt="" width="'.$woocommerce->get_image_size('shop_catalog_image_width').'px" height="'.$woocommerce->get_image_size('shop_catalog_image_height').'px" /></div>';*/
endwhile;
echo '</select><!--/.products-->';
else :
_e('No product matching your criteria.');
endif; // endif $product_count > 0
if( $product_count > 0 ) :
echo '<div class="matched_product_main">';
// Start the loop
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
global $post;
if (has_post_thumbnail( $loop->post->ID ))
echo '<div class="matched_product_image"><a href="'.get_permalink($post->ID).'">'.get_the_post_thumbnail($loop->post->ID).'</a></div>';
else
echo '<div class="matched_product_image"><img src="'.$woocommerce->plugin_url().'/assets/images/placeholder.png" alt="" /></div>';
endwhile;
echo '</div>';
else :
_e('No product matching your criteria.');
endif; // endif $product_count > 0
return ob_get_clean();
}
add_shortcode("woo_products_by_tags", "woo_products_by_tags_shortcode");
I am using the following code in functions.php to display products
add_action( 'woocommerce_single_product_summary', 'add_custom_related_product_by_tags', 15 );
function add_custom_related_product_by_tags() {
global $product;
$product_attributes = $product->get_attributes();
echo do_shortcode('[woo_products_by_tags tags="'.$product_attributes.'"]');
}
Leave an answer