【问题标题】:Woocommerce get_term() count and get_terms() count differentWoocommerce get_term() 计数和 get_terms() 计数不同
【发布时间】:2015-06-28 18:04:03
【问题描述】:

我在 woocommerce 的帖子中使用 php,并希望根据其库存水平显示/隐藏类别中的产品。当前使用 get_term() 并且产品计数包括“缺货”产品。使用 get_terms() 并且同一类别的产品计数仅计算“库存”产品。如何编写 if else 条件语句以使用特定产品类别的 get_terms 计数?

这是我到目前为止的代码(“如果”在此类别中的产品“缺货”时为真。希望“如果”仅在此类别中的产品“有货”时为真"):

<?php
$term = get_term(373, 'product_cat' );
$category = $term->name;
$theCount = $term->count;
if ( ( $category = 'Outlet Other' ) && ( $theCount > 0 ) ){
    echo '[product_category category="outlet-other" per_page="100" orderby="menu_order" order="asc"]';} 
else {
    echo 'There are currently no products in this category.  Please check back soon!';}
?>

这是我在页面上用于测试目的的其他代码,它使用 get_terms() 显示此类别的产品计数等于“库存”数量:

<?php
$terms = get_terms( 'product_cat' );
foreach( $terms as $term ){
    echo 'Product Category: ' . $term->name . ' - Count: ' . $term->count . "\r\n";}
?>

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
    <?php
    $term = get_term(373, 'product_cat' );
    $category = $term->name;
    $termsother = get_terms( 'product_cat' );
        foreach( $termsother as $termsnew ) {
            if (($termsnew->count > 0) && ($termsnew->name == $category)) {
              echo '[product_category category="' . $termsnew->slug . '" per_page="100" orderby="menu_order" order="asc"]';
            }
            elseif (($termsnew->count == 0) && ($termsnew->name == $category)) {
              echo 'There are currently no products in this category. Please check back soon!';
            }
            else {
              //echo 'If and ElseIf are false!';
            }
        }
    ?>
    

    【讨论】:

      【解决方案2】:

      在 if 语句之前,使用您的测试代码检查该类别中是否有库存产品,并在您的 if 语句中添加附加条件。请参阅下面的代码。

      <?php
      $term = get_term(373, 'product_cat' );
      $category = $term->name;
      $theCount = $term->count;
      $terms = get_terms( 'product_cat' );
      $category_has_in_stock_items = false;
          foreach( $terms as $term ) {
          if ($term->count > 0) {
              category_has_in_stock_items = true;
              break;
          }
      }
      if (($category = 'Outlet Other') && ($theCount > 0) && $category_has_in_stock_items){
          echo '[product_category category="outlet-other" per_page="100" orderby="menu_order" order="asc"]';} 
      else {
          echo 'There are currently no products in this category.  Please check back soon!';}
      ?>
      

      【讨论】:

      • 嗨,brulecap,感谢您的回复(确实注意到 category_has_in_stock_items 缺少 $ ......没什么大不了的)。我没有运气就应用了附加逻辑。仍在测试并对逻辑进行细微修改,并将报告我的发现。谢谢!
      • 不客气。那些讨厌的美元总是让我着迷。希望你早点发现它并没有造成太多的挫败感。在再次查看我的代码建议时,我注意到 $term 在 foreach 循环中被重新分配。您可以考虑在此处使用单独的变量...
      • 感谢您的帮助。 $term 在 foreach 循环中被重新分配。我只是将变量更改为 $termnew 并添加了 ifelse 条件。
      【解决方案3】:

      我不知道它是否完全是指提问者提出的问题,但我在 WP REST API 中遇到了与 get_terms 类似的问题。调试一个小时后,我发现 WooCommerce 仅在 is_admin() || is_ajax() 时更改条款计数:

      https://github.com/woocommerce/woocommerce/blob/51912c659db971dcc1bf4a9a31a33777904ec627/includes/wc-term-functions.php#L530-L533

      解决方案:在您的 get_terms 函数调用之前添加一个 __return_true 过滤器以模拟 WP REST API 中的 AJAX 请求:

      add_filter('wp_doing_ajax', '__return_true');
      $terms = get_terms([
          // [...]
          'pad_counts' => 1 // This is necessery if you want to correct parent categories count, too
      ]);
      remove_filter('wp_doing_ajax', '__return_true');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 2021-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多