-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I find out what orders belong to a user? #10
Comments
use Corcel\WooCommerce\Model\Order;
Order::hasMeta('_customer_user', 1)->get(); // 1 - customer ID
use Corcel\WooCommerce\Model\Order;
// get orders and eager load items with product to optimize queries count
$orders = Order::with('items.product')->hasMeta('_customer_user', 1)->get();
$items = $orders->pluck('items')->flatten(1); // all items from orders
$products = $items->pluck('product'); // all products from all orders (non-unique) Unfortunately relation from product to orders is not yet implemented, so your third question cannot be solved right now. PRs are welcome. |
Thanks! This is already very helpful. :)
…On Mon, Sep 28, 2020 at 4:20 PM Krzysztof Grabania ***@***.***> wrote:
Given a user ID, how do I find out what orders belong to them:
use Corcel\WooCommerce\Model\Order;
Order::hasMeta('_customer_user', 1)->get(); // 1 - customer ID
Given a user ID, how do I find out which products they have purchased
use Corcel\WooCommerce\Model\Order;
// get orders and eager load items with product to optimize queries count$orders = Order::with('items.product')->hasMeta('_customer_user', 1)->get();$items = $orders->pluck('items')->flatten(1); // all items from orders$products = $items->pluck('product'); // all products from all orders (non-unique)
Unfortunately relation from product to orders is not yet implemented, so
your third question cannot be solved right now. PRs are welcome.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#10 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEN6HTIDYSSE2IC7FOCRW3SIDV2NANCNFSM4R4RS57A>
.
|
With the last commit from master you can do it for example in this way: $product = Product::find(1);
$orders = $product->items->pluck('order');
$orders = $orders->filter(function ($order) {
return $order->hasMeta('_customer_id', 1);
});
$wasBought = $orders->isNotEmpty(); I will close this issue. Any work with documentation will be continued in this issue: #11 |
It would be great to have some basic documentation for this plugin. I'm looking for answers to the following questions:
The text was updated successfully, but these errors were encountered: