【问题标题】:Get total product purchased count with specific product attributes in Woocommerce在 Woocommerce 中获取具有特定产品属性的产品购买总数
【发布时间】:2018-06-26 04:51:16
【问题描述】:

我正在使用 WooCommerce 创建一个自定义报告,它正在运行,但它使用了大量资源,而且它需要更大,如果我增加它的大小,它会超时。我确信有更好的方法来做到这一点。

我正在运行多个查询以查看已订购了多少特定颜色和尺寸的产品。目前,这些是单独的查询,效率不高。这是我的代码:

这是获取订购数量的函数:

function get_order_by_product_id($product_id, $product_color, $product_size) {
    $args = array ( 
            'limit' => '-1',
            'status' => 'processing',
    );
    $orders = wc_get_orders( $args );
    if($orders) :
        $total = 0;
        foreach( $orders as $order) :
            $order_id = $order->get_id();
            $single_order = wc_get_order( $order_id ); 

            foreach ($single_order->get_items() as $item_id => $item_data) :
                $product = $item_data->get_product();
                $productID = $single_order->get_item_meta($item_id, '_product_id', true);
                $product_name = $product->get_name();
                $color = $single_order->get_item_meta($item_id, 'pa_colors', true);
                $size = $single_order->get_item_meta($item_id, 'pa_sizes', true);
                $item_quantity = $item_data->get_quantity();

                if($productID == $product_id && $color == $product_color && $size == $product_size ) :
                    $total += $item_quantity;
                endif;

            endforeach;
        endforeach;
        echo $total;
    endif;
} 

这是对函数的调用:

get_order_by_product_id(47652, 'black', 'xs');

目前,为了完成这项工作,我为每个产品 ID、颜色和尺寸一遍又一遍地调用该函数。正如我所说,它可以工作,但需要消耗大量资源。

任何提高效率的想法都将不胜感激!

【问题讨论】:

  • 您可能会编写一个 SQL 查询来获取您需要的信息,我建议您进入 MySQL 客户端(HeidiSQL、MySQL Workbench)并尝试一下。没有看到您的数据库结构,这里没有足够的信息来帮助您。
  • 您的意思是不使用内置的 WooCommerce 查询,而是执行 SQL 查询?
  • 是的,您正在寻求优化多个复杂查询,唯一的方法是绕过抽象并直接进行优化查询,利用数据库来完成它的设计目的。由于循环,您的代码不仅速度慢,而且要求数据库一遍又一遍地返回数据,增加网络负载、MySQL 解析器负载等。

标签: php mysql sql wordpress woocommerce


【解决方案1】:

这可以通过使用以下函数的简单 SQL 查询来完成:

/*
 * @param (int)    $product_id 
 * @param (string) $colors     slug term for "pa_colors" product attribute
 * @param (string) $sizes      slug term for "pa_sizes"  product attribute
 * @param (string) $status     Optional: Order status ("processing" by default)
**/
function get_ordered_product_count( $product_id, $colors, $sizes, $status='processing' ){
    return $wpdb->get_var( "
        SELECT SUM(woim.meta_value)
        FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim ON woi.order_item_id = woim.order_item_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim1 ON woi.order_item_id = woim1.order_item_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim3 ON woi.order_item_id = woim3.order_item_id
        WHERE p.post_type LIKE 'shop_order'
        AND p.post_status LIKE 'wc-$status'
        AND woim.meta_key LIKE '_qty'
        AND woim1.meta_key LIKE 'pa_colors'
        AND woim1.meta_value LIKE '$colors'
        AND woim2.meta_key LIKE 'pa_sizes'
        AND woim2.meta_value LIKE '$sizes'
        AND woim3.meta_key LIKE '_product_id'
        AND woim3.meta_value = $product_id
    " );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

用法示例:

  • “处理中”订单状态:get_ordered_product_count(47652, 'black', 'xs');
  • 具体订单状态:get_ordered_product_count(47652, 'black', 'xs', 'completed');

【讨论】:

  • Loic 你一如既往地经历过!我不得不进行一些更改:在开头添加了function,并且第一行中有一个右括号。
  • @RiotAct 哦,是的,当然……我只是忘记了。谢谢:)
【解决方案2】:

要计算具有特定属性 eq 的所有(已发布)产品,颜色为“蓝色”或“黑色”,您可以使用:

$colors = 'black, blue';  
$args = array( 
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'meta_query'            => array(
                array(
                    'key'           => '_visibility',
                    'value'         => array('catalog', 'visible'),
                    'compare'       => 'IN'
                )
            ),
            'tax_query'             => array(
                array(
                    'taxonomy'      => 'pa_color',
                    'terms'         => explode(",",$colors),
                    'field'         => 'slug',
                    'operator'      => 'IN'
                )                   
            )
        );

echo count (wc_get_products($args));

【讨论】:

  • 是的,但这并没有得到订购的产品,只是一般的产品。
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 2020-11-02
  • 2020-01-12
  • 1970-01-01
  • 2019-05-01
  • 2018-09-17
  • 1970-01-01
  • 2021-03-17
相关资源
最近更新 更多