繁体   English   中英

将 WooCommerce 订单中按产品类别订购的产品数量相加

[英]Sum the quantities of products ordered by product categories from a WooCommerce order

我真的坚持这一点,我非常感谢任何帮助。

目标是计算 Woocommerce 订单上每个类别中的商品数量,以便每个部分都可以以类别名称和产品数量为标题。 例如:

汉堡 x 5

下面将是该订单上所有汉堡的列表。

现在我以为我可以使用以下代码:

$categories = [];
foreach ($order->get_items() as $item) {
  $product_id = $item['product_id'];
  $meta = $item['item_meta'];
  $meta = array_filter($meta, function ($key) {
                    return !in_array($key, Order::getHiddenKeys());
  }, ARRAY_FILTER_USE_KEY);
  $terms = get_the_terms($product_id, 'product_cat');
  $cat_id = $terms[0]->term_id;
  $categories[$cat_id][] = $item;
  $cat_name = $terms[0]->name;
}

foreach($categories as $category => $items){
?>
  <tr>
    <td colspan="3">
      <h4>
        <?php 
        if( $term = get_term_by( 'id', $category, 'product_cat' ) ){
          echo $term->name.' ('.$term->count.')';
          echo " &times; ";
          echo count($items);
        }
        ?>
      </h4>
    </td>
  </tr>
  <?php

但我意识到这只是计算该类别中已订购的产品数量,而不是数量 因此,如果其中一个有多个相同的产品,则不会计算在内。 例如,如果类别包含以下三个项目:

芝士汉堡 x 2
汉堡 x 1

将返回两个而不是三个。

我一直在查看类别和术语 arrays 但似乎找不到任何可行的方法。 我确定我错过了一些东西,但我现在只是不知所措。

谁能指出我正确的方向?

(另外,如果它有任何用处,这是上一个问题Filtering the Woocommerce $order items By Category (Term)的间接后续)

您可以通过两种方式做到这一点:

  1. 根据从产品中获得的产品类别将订购的产品数量相加(因此必须只有1)
  2. 设置您想要获得购买产品数量计数的特定产品类别列表

解决方案#1

您可以使用get_the_terms() function 来获取产品类别。

function参数必须是要统计的订单ID。

请注意,只会检查get_the_terms() function 返回的第一个产品类别。 如果一个产品有多个,你可能会得到意想不到的结果。

// gets an array with the quantities of the ordered products grouped by product categories
function gets_the_quantities_of_products_ordered_grouped_by_product_categories( $order_id ) {

    // initializes the array that will contain the product categories with the ordered quantities of each
    $count_by_cat = array();

    // get the order object
    $order = wc_get_order( $order_id );

    // for each product ordered
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        // get product categories
        $terms = get_the_terms( $product->get_id(), 'product_cat' );
        foreach ( $terms as $term ) {
            $cat_name = $term->name;
            // if the product category is already present in the array, add the quantities
            if ( array_key_exists( $cat_name, $count_by_cat ) ) {
                $count_by_cat[$cat_name] += $item->get_quantity();
            // otherwise it adds the category and quantity to the array
            } else {
                $count_by_cat[$cat_name] = $item->get_quantity();
            }
            break;
        }
    }

    return $count_by_cat;
}

解决方案#2

在这种情况下,您必须使用要获取订购产品数量的产品类别名称列表来初始化一个数组

使用has_term() function 您可以检查产品是否属于特定产品类别。

function参数必须是要统计的订单ID。

// gets an array with the quantities of the ordered products grouped by product categories
function gets_the_quantities_of_products_ordered_grouped_by_product_categories( $order_id ) {

    // initializes the array that will contain the product categories with the ordered quantities of each
    $count_by_cat = array();

    // set the product categories you want to get counts for
    $categories = array(
        'Clothes',
        'Shoes',
        'Pants',
        'Jackets',
        'Hats'
    );

    // get the order object
    $order = wc_get_order( $order_id );

    // for each product ordered
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        // for each product category
        foreach ( $categories as $cat_name ) {
            // check if the product belongs to one of the product categories
            if ( has_term( $cat_name, 'product_cat', $product->get_id() ) ) {
                // if the product category is already present in the array, add the quantities
                if ( array_key_exists( $cat_name, $count_by_cat ) ) {
                    $count_by_cat[$cat_name] += $item->get_quantity();
                // otherwise it adds the category and quantity to the array
                } else {
                    $count_by_cat[$cat_name] = $item->get_quantity();
                }
                break;
            }
        }
    }

    return $count_by_cat;
}

这两个函数都将返回一个类似于此的数组

$count_by_cat = array(
    'Shoes' => 4,
    'Pants' => 2,
    'Hats'  => 11
)

因此,您可以打印类似于以下内容的 HTML 代码

// print the counts for each product category
foreach ( $count_by_cat as $category_name => $count ) {
    echo "<p><strong>" . $category_name . ":</strong> " . $count . "</p>";
}

两种代码都已经过测试并且可以工作。 将它们添加到您的活动主题的功能中。php。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM