Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce:获取产品的所有订单_Php_Sql_Wordpress_Woocommerce_Orders - Fatal编程技术网

Php Woocommerce:获取产品的所有订单

Php Woocommerce:获取产品的所有订单,php,sql,wordpress,woocommerce,orders,Php,Sql,Wordpress,Woocommerce,Orders,在WooCommerceAPI中,我看到一个方法wc_get_orders 在上面提到的参数中,我没有看到一个参数可以提供一个产品ID或对象来限制仅针对该特定产品的订单结果 有没有办法只过滤特定产品的订单 我需要使用此方法,因为某些参数是必需的已编辑 此函数不存在,但可以构建。因此,下面的函数将返回给定产品ID的所有订单ID的数组,并进行正确的SQL查询: /** * Get All orders IDs for a given product ID. * * @param integ

在WooCommerceAPI中,我看到一个方法wc_get_orders

在上面提到的参数中,我没有看到一个参数可以提供一个产品ID或对象来限制仅针对该特定产品的订单结果

有没有办法只过滤特定产品的订单

我需要使用此方法,因为某些参数是必需的

已编辑 此函数不存在,但可以构建。因此,下面的函数将返回给定产品ID的所有订单ID的数组,并进行正确的SQL查询:

/**
 * Get All orders IDs for a given product ID.
 *
 * @param  integer  $product_id (required)
 * @param  array    $order_status (optional) Default is 'wc-completed'
 *
 * @return array
 */
function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ){
    global $wpdb;

    $results = $wpdb->get_col("
        SELECT order_items.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        AND order_item_meta.meta_value = '$product_id'
    ");

    return $results;
}
用法1(对于给定的产品ID
37
和默认完成订单状态):

用法2(对于给定的产品ID
37
一些定义的订单状态):

代码位于活动子主题(或主题)的function.php文件或任何插件文件中


这段代码经过测试并运行正常。

所以我想获取特定于产品和个人的订单。我使用@LoicTheActec提供的解决方案和wc_get_方法来获得我想要的结果

以下是功能:

    /**
 * Get All orders IDs for a given product ID.
 *
 * @param  integer  $product_id (required)
 * @param  array    $order_status (optional) Default is 'wc-completed'
 *
 * @return array
 */
public function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ) {
    global $wpdb;

    $results = $wpdb->get_col("
        SELECT order_items.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        AND order_item_meta.meta_value = '$product_id'
    ");

    return $results;
}
过滤掉我想要的结果。由于该方法不提供include参数,我必须获取所有订单,然后删除给定产品id的订单,并从wc_get_订单中排除所有剩余订单:

    /**
 * Check if current user has more than 3 orders
 *
 * @return void
 * @author 
 **/
public function woocommerce_check_order_count($product_id, $customer_email)
{   


    $statuses = array(  'wc-processing' );

    $orders_ids_for_product = $this->get_orders_ids_by_product_id( $product_id, $statuses );


    // Get All Orders
    $allorders = wc_get_orders( array(
        'status'      => 'processing',
        'type'        => 'shop_order',
        'email'       => '',
        'limit'       => -1,
        'return'      => 'ids',
    ) );

    // Remove the $orders_ids_for_product array from the $allorders array
    $orderstoexclude = array_diff($allorders, $orders_ids_for_product);

    $orders = wc_get_orders( array(
        'status'      => 'processing',
        'type'        => 'shop_order',
        'customer'    => $customer_email,
        'email'       => '',
        'limit'       => 3,
        'exclude'     => $orderstoexclude,
        'return'      => 'ids',
    ) );

    return $orders;

}

这是一个真正的WC_顺序对象Hello LoicTheAztec,如果我有多个元键值,如何修改sql。例如颜色>红色?谢谢@somtam您需要再添加一个:
LEFT JOIN{$wpdb->prefix}woocommerce\u order\u item meta as order\u item\u meta2 ON order\u item.order\u item\u id=order\u item\u meta2.order\u item\u id
…然后您将能够使用新的
order\u item\u meta2.meta\u key
或/和
order\u item\u meta 2.meta\u value
…非常有效,谢谢!还有一件事。通常使用get_col()来转义sql,比如$results=$wpdb->get_col($wpdb->prepare($sql,$id));。我必须这样做还是让你自己逃跑?谢谢。@somtam prepare()方法在某些情况下允许更高的安全性来避免SQL注入。在这个问题上,我不需要它。这取决于你是否真的需要它。
    /**
 * Get All orders IDs for a given product ID.
 *
 * @param  integer  $product_id (required)
 * @param  array    $order_status (optional) Default is 'wc-completed'
 *
 * @return array
 */
public function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ) {
    global $wpdb;

    $results = $wpdb->get_col("
        SELECT order_items.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        AND order_item_meta.meta_value = '$product_id'
    ");

    return $results;
}
    /**
 * Check if current user has more than 3 orders
 *
 * @return void
 * @author 
 **/
public function woocommerce_check_order_count($product_id, $customer_email)
{   


    $statuses = array(  'wc-processing' );

    $orders_ids_for_product = $this->get_orders_ids_by_product_id( $product_id, $statuses );


    // Get All Orders
    $allorders = wc_get_orders( array(
        'status'      => 'processing',
        'type'        => 'shop_order',
        'email'       => '',
        'limit'       => -1,
        'return'      => 'ids',
    ) );

    // Remove the $orders_ids_for_product array from the $allorders array
    $orderstoexclude = array_diff($allorders, $orders_ids_for_product);

    $orders = wc_get_orders( array(
        'status'      => 'processing',
        'type'        => 'shop_order',
        'customer'    => $customer_email,
        'email'       => '',
        'limit'       => 3,
        'exclude'     => $orderstoexclude,
        'return'      => 'ids',
    ) );

    return $orders;

}