akoggy
akoggy

Reputation: 1

Add "Show all products" to all product categories that have child categories on default Woocommerce listing widget

I need to add some custom feature to the default Woocommerce product category listing widget. The widget is working fine, but I need to add a "Show all products" list element to all categories that have child category list element. The "Show all products" list element is needed to show all products of the parent category.

Example:

Upvotes: 0

Views: 65

Answers (1)

akoggy
akoggy

Reputation: 1

here is an solution what I just created and it woks for me:

<script>
jQuery(document).ready(function($) {
  $('.wc-block-product-categories-list-item').each(function() {
    var $parentItem = $(this);
    var $subCategories = $parentItem.find('.wc-block-product-categories-list');

    if ($subCategories.length > 0) {
      var parentLink = $parentItem.find('a').attr('href');
      var $showAllLink = $('<li class="wc-block-product-categories-list-item show-all-link"><a href="' + parentLink + '">Show All Products</a></li>');

      $showAllLink.on('click', function(e) {
        e.preventDefault();
        window.location.href = parentLink;
      });

      $subCategories.prepend($showAllLink);
    }
  });
});
</script>

Upvotes: 0

Related Questions