D
D
Dmitry2017-05-03 17:23:18
PHP
Dmitry, 2017-05-03 17:23:18

Why is the number of products displayed incorrectly in Woocommerce?

Hello,
in the product catalog (wordpress / woocommerce) I display all product categories. For each category in brackets should go the number of products in this category. When outputting, it shows zero products, but when you click on the link, there are a lot of products. Where is the mistake?
Thanks

// вывод всех категорий после @hooked woocommerce_result_count - 20 и @hooked woocommerce_catalog_ordering - 30
// в archive-product.php строка 67
add_action('woocommerce_before_shop_loop', 'dnk_get_all_categories', 40);
function dnk_get_all_categories () {
  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 1;      // 1 for yes, 0 for no
  $pad_counts   = 1;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;

  $args = array(
           'taxonomy'     => $taxonomy,
           'orderby'      => $orderby,
           'show_count'   => $show_count,
           'pad_counts'   => $pad_counts,
           'hierarchical' => $hierarchical,
           'title_li'     => $title,
           'hide_empty'   => $empty
  );

  $all_categories = get_categories( $args );

  // echo '<pre>';
  // var_dump($all_categories);
  // echo '</pre>';
  echo '<div class="dnk_all_cat_list">';
  foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
      $category_id = $cat->term_id;       
      echo '<a class="dnk_cat_link" href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .' (' . $cat->category_count . ')</a>';
    }       
  }
  echo '</div>';
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WordPress WooCommerce, 2017-05-03
@ddimonn8080

Ptm that WooCommerce has a number of products stored elsewhere. It seems to be in some kind of meta. We need to study the code.
You display the number of posts associated with the term. It is often at odds with the number of products available.
Conventionally, there can be 10 posts in a term, this is the WordPress logic. But according to the logic of WooCommerce, some products may not be available for order, some have no stock, some are out of stock, etc. - all these products are hidden and it is considered that they are not in the category. But at the same time, the post inside the term remains in place.
If you read the source, it becomes clear where he stores this data. You can see how a typical category tree is built. Everything is right there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question