【问题标题】:Get global order items quantity sum by category in WooCommerce在 WooCommerce 中按类别获取全球订单商品数量总和
【发布时间】:2021-07-24 10:31:29
【问题描述】:

我想问是否有人知道如何在 WooCommerce 中统一产品。我有订单,我只需要 INT 形式的最后一个值和变量,以便我可以继续使用它。

这是我的代码:

// set the product categories you want to get counts for
$categories = array(
    'pro',
    'medium',
    'basic',
);

if ( count( $subscriptions ) > 0 ) {
    // Loop through customer subscriptions
    foreach ( $subscriptions as $subscription ) {
        // Get the initial WC_Order object instance from the subscription
        $order = wc_get_order( $subscription->get_parent_id() );

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $product = $item->get_product(); // Get the product object instance
            // Target only subscriptions products type
            // print_r($product->get_name());
            if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
                $quantity = $item->get_quantity(); // Get the quantity
                echo '<p>Quantity: ' . $quantity . '</p>';
            }
            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;
                }
            }
            foreach ( $count_by_cat as $category_name => $count ) {
                echo "<p><strong>" . $category_name . ":</strong> " . $count . "</p>";
            }
        }
    }
}

在下面的截图中,这是我想要得到的:

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-subscriptions product-quantity


    【解决方案1】:

    要按类别获取全球订单商品总数,请使用以下重新访问的代码:

    $count_by_cat = array(); // Initializing
    
    // Below set the product categories you want to get counts for
    $categories = array( 'pro', 'medium', 'basic' );
    
    if ( count( $subscriptions ) > 0 ) {
        // Loop through customer subscriptions
        foreach ( $subscriptions as $subscription ) {
            // Get the initial WC_Order object instance from the subscription
            $order = wc_get_order( $subscription->get_parent_id() );
    
            // Loop through order items
            foreach ( $order->get_items() as $item ) {
                $product = $item->get_product(); // Get the product object instance
                
                // Target only subscriptions products type
                if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
                    foreach ( $categories as $category ) {
                        // check if the product belongs to one of the product categories
                        if ( has_term( $category, 'product_cat', $item->get_product_id() ) ) {
                            // if the product category is already present in the array, add the quantities
                            if ( array_key_exists( $category, $count_by_cat ) ) {
                                $count_by_cat[$category] += $item->get_quantity();
                            } 
                            // otherwise it adds the category and quantity to the array
                            else {
                                $count_by_cat[$category] = $item->get_quantity();
                            }
                        }
                    }
                }
            }
        }
        if ( ! empty($count_by_cat) ) {
            foreach ( $count_by_cat as $category => $quantity ) {
                echo '<p><strong>' . $category . '</strong>: ' . $quantity . '</p>';
            }
        }
    }
    

    它现在应该给出预期的输出。

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 2021-04-02
      • 2019-01-26
      • 2018-01-24
      • 2019-02-23
      • 2021-01-07
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多