Php WooCommerce根据wc\u customer\u Buyed\u product功能获取订单中的产品数量
我正在使用wc_customer_bunded_产品功能来检查当前用户是否购买了某个产品 但是,现在我需要检查客户购买了多少产品 例如,如果我在订单中购买了数量为7的产品,我需要一种方法来获取该产品的订单和订单中的数量 我怎样才能做到这一点Php WooCommerce根据wc\u customer\u Buyed\u product功能获取订单中的产品数量,php,wordpress,woocommerce,product,orders,Php,Wordpress,Woocommerce,Product,Orders,我正在使用wc_customer_bunded_产品功能来检查当前用户是否购买了某个产品 但是,现在我需要检查客户购买了多少产品 例如,如果我在订单中购买了数量为7的产品,我需要一种方法来获取该产品的订单和订单中的数量 我怎样才能做到这一点 如果有人有答案,我们将不胜感激 在这里,您拥有构建自己功能的所有材料。下面是一个示例,该示例使用wc_customer_bulled_product函数,根据当前客户的产品ID获取订单中的产品数量: 参考资料: 在这里,您拥有构建自己功能的所有材料。下面是一
如果有人有答案,我们将不胜感激 在这里,您拥有构建自己功能的所有材料。下面是一个示例,该示例使用wc_customer_bulled_product函数,根据当前客户的产品ID获取订单中的产品数量: 参考资料:
在这里,您拥有构建自己功能的所有材料。下面是一个示例,该示例使用wc_customer_bulled_product函数,根据当前客户的产品ID获取订单中的产品数量: 参考资料:
function checking_product_bought( $_product_id ){
global $woocommerce, $posts;
// Get the current customer info (as an object)
$customer = wp_get_current_user();
$customer_id = $customer->ID; // customer ID
$customer_email = $customer->email; // customer email
// Get all orders for this customer_id
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $customer_id,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
if ( $customer_orders ){
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order_id = $order->id; // get the order ID )or may be "order->ID")
// getting all products items for each order
$items = $order->get_items();
foreach ($items as $item)
{
$product_id = $item['product_id']; // product id
$product_qty = $item['qty']; // product quantity
if(wc_customer_bought_product( $customer_email, $customer_id, $_product_id))
{
echo '<div>for order number: ' . $order_id . ' ,there is ' . $product_qty . ' for this product.';
}
}
}
}
}