php 获取用户在Woocmmmerce中购买的物品总数

3lxsmp7m  于 5个月前  发布在  PHP
关注(0)|答案(4)|浏览(51)

我试图找出一个函数,得到当前用户购买的项目总数(不是总金额,但项目)在所有的订单。到目前为止,我发现这(这不工作)-但再次此功能应该得到总金额,而不是项目。一直试图编辑它的工作,但没有成功到目前为止。

public function get_customer_total_order() {
$customer_orders = get_posts( array(
    'numberposts' => - 1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => array( 'shop_order' ),
    'post_status' => array( 'wc-completed' )
) );

$total = 0;
foreach ( $customer_orders as $customer_order ) {
    $order = wc_get_order( $customer_order );
    $total += $order->get_total();
}

return $total;
}

字符串
有什么想法吗?

x8diyxa7

x8diyxa71#

**已更新 (考虑项目数量)

下面这个非常轻量级的函数将获得客户购买的物品总数:

function get_user_total_purchased_items( $user_id = 0 ){
    global $wpdb;

    $customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;

    return (int) $wpdb->get_var( "
        SELECT SUM(woim.meta_value)
        FROM {$wpdb->prefix}woocommerce_order_items AS woi
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
        INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
        WHERE woi.order_item_type = 'line_item'
        AND p.post_type LIKE 'shop_order'
        AND p.post_status IN ('wc-completed')
        AND pm.meta_key LIKE '_customer_user'
        AND pm.meta_value LIKE '$customer_id'
        AND woim.meta_key LIKE '_qty'
    " );
}

字符串
代码放在你的活动子主题(或活动主题)的function.php文件中。测试和工作。

用法示例

1)显示当前用户购买的项目总数:

<?php echo '<p>Total purchased items: ' . get_user_total_purchased_items() . '</p>'; ?>


2)显示给定用户ID的已购买项目总数:

// Here the user ID is 105
<?php echo '<p>Total purchased items: ' . get_user_total_purchased_items(105) . '</p>'; ?>

fruv7luv

fruv7luv2#

function get_user_total_purchased_items_by_email( $email ){
            global $wpdb;
            if (empty($email)) {
                return;
            }

            return (int) $wpdb->get_var( "
                SELECT SUM(woim.meta_value)
                FROM {$wpdb->prefix}woocommerce_order_items AS woi
                INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
                INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
                INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
                WHERE woi.order_item_type = 'line_item'
                AND p.post_type LIKE 'shop_order'
                AND p.post_status IN ('wc-completed')
                AND pm.meta_key LIKE '_customer_user'
                AND pm.meta_value LIKE '$email'
                AND woim.meta_key LIKE '_qty'
            " );
        }

字符串
代码放在你的活动子主题(或活动主题)的function.php文件中。测试和工作。
使用示例

<?php echo '<p>Total purchased items: ' . get_user_total_purchased_items_by_email('[email protected]') . '</p>'; ?>

xytpbqjk

xytpbqjk3#

下面是自定义代码

$user = wp_get_current_user();
 $user_id = $user->ID;
  $customer_orders = get_posts( array(
    'meta_key' => '_customer_user',
    'meta_value' => $user_id,
    'post_type' => 'shop_order',
    'post_status' => array_keys( wc_get_order_statuses() ),
    'numberposts' => -1,
    'orderby' => 'date',
    'order' => 'ASC',
  ) );
      
  
  foreach($customer_orders as $order){
    $order_id = $order->ID;
    $order = wc_get_order( $order_id );
    $total_num_item += $order->get_item_count();
  }

字符串

whlutmcx

whlutmcx4#

试试下面的代码

global $wpdb;
$customer_orders = get_posts( array(
    'numberposts' => - 1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => array( 'shop_order' ),
    'post_status' => array( 'wc-completed' )
) );
$customer_orders = json_decode(json_encode($customer_orders),true);

$total_product_count = 0;
foreach ( $customer_orders as $customer_order_data ) {


        $Order_ID=$customer_order_data['ID'];
        $Select_Order_Details = $wpdb->get_results( "SELECT  COUNT(order_item_id) AS total_product_count FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_type='line_item' and order_id = $Order_ID");
        $Select_Order_Details = json_decode(json_encode($Select_Order_Details),true);

        foreach($Select_Order_Details as $Product_Count)
        {
                $total_product_count=$total_product_count+$Product_Count['total_product_count'];
        }

}
echo ($total_product_count); exit;

字符串

相关问题