在woocommerce中使用特定产品属性获取购买的产品总数

2skhul33  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(373)

我正在使用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,颜色和大小的函数。正如我所说,它是有效的,但它是一个资源Pig。
任何提高效率的想法都将不胜感激!

yfjy0ee7

yfjy0ee71#

这可以通过使用以下函数在一个简单的打火机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');

gcmastyq

gcmastyq2#

要计算具有特定属性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));

相关问题