Comments (3)
hey guy! so hard and not good for performance,
you should try to get all the orders and get all the products.
then you can create a function to map this information to make the report.
from shopifysharp.
yes so actualy this is what i do
public async Task OnGetAsync()
{
TotalSalesVolume = 0;
var uniqueOrderIdsForVendor = new HashSet<long>();
var today = DateTime.Today;
var threeMonthsAgo = new DateTime(today.Year, today.Month, 1).AddMonths(-2);
MonthlySalesStats = Enumerable.Range(0, 3).Select(i => today.AddMonths(-i)).ToDictionary(d => d.ToString("yyyy-MM"), d => 0m);
HttpContext.Session.SetString("VendorName", "VendorName"); // its just a test it will go in the login logic
var vendorName = HttpContext.Session.GetString("VendorName");
var productIds = await GetProductIdsByVendorAsync(vendorName);
var orders = await orderService.ListAsync(new OrderListFilter { CreatedAtMin = threeMonthsAgo, Status = "any" });
foreach (var order in orders.Items)
{
var orderDate = DateTime.Parse(order.CreatedAt.ToString());
var orderMonth = orderDate.ToString("yyyy-MM");
if (MonthlySalesStats.ContainsKey(orderMonth))
{
foreach (var lineItem in order.LineItems)
{
if (!lineItem.ProductId.HasValue || !productIds.Contains(lineItem.ProductId.Value)) continue;
uniqueOrderIdsForVendor.Add(order.Id.Value);
var lineItemTotal = (decimal)(lineItem.Price * lineItem.Quantity);
TotalSalesVolume += lineItemTotal;
MonthlySalesStats[orderMonth] += lineItemTotal;
var productSaleInfo = TopSellingProducts.FirstOrDefault(x => x.ProductId == lineItem.ProductId.Value);
if (productSaleInfo == null)
{
productSaleInfo = new ProductSaleInfo
{
ProductId = lineItem.ProductId.Value,
ProductTitle = lineItem.Title,
QuantitySold = (int)lineItem.Quantity,
TotalSales = lineItemTotal
};
TopSellingProducts.Add(productSaleInfo);
}
else
{
productSaleInfo.QuantitySold += (int)lineItem.Quantity;
productSaleInfo.TotalSales += lineItemTotal;
}
}
}
}
TotalOrders = uniqueOrderIdsForVendor.Count;
TopSellingProducts = TopSellingProducts.OrderByDescending(p => p.TotalSales).Take(10).ToList();
}
and it works i just thinks that if you have a millions order it will be really hard on the server to map everythings
but thanks i wil close this cause it works now and i'm gonna
from shopifysharp.
Hey! I agree it's not great for performance, especially if you have a shop with millions of orders.
I think what I would do is put this into a background service first, so that you're not blocking any sort of UI or frontend javascript while you query this data. In Asp.Net 5+, you can use the IHostedService interface to create a service that runs in the background on startup.
For the actual querying though, I can't find any better method than what's been suggested already. Shopify doesn't seem to support anything to look up orders by the product that was purchased as far as I'm aware. I'll think on this and do some experiments! I'm interested in figuring this out.
from shopifysharp.
Related Issues (20)
- Add product into Exising Channels HOT 4
- Unable to add multiple images to product using ShopifySharp HOT 7
- Product Service ListAsync - Not Paging HOT 2
- Deprecated property delivery_category is being used HOT 2
- API get checkout by token not working HOT 6
- Internal application stopped working HOT 2
- Order not coming in api HOT 2
- Adding Multiple Tracking Numbers with Fulfillment.TrackingNumbers HOT 3
- Add read_returns and write_returns to access scopes enums HOT 2
- Customer is not returned by graphql mutation HOT 2
- LeakyBucket does not release the Semaphore if a request fails or the task is canceled
- Missing Alt attribute in ArticleImage class
- HttpResponseMessage Response in RequestResult marked as Obsolete HOT 5
- Add support for IAsyncEnumerable on every service that supports ListAsync
- Add support for validating the timestamp with a default timeout period in `ShopifyRequestValidationUtility`
- IsValidShopDomainAsync should check if shopDomain is a valid uri before building the shop domain uri HOT 2
- Support for Meta Objects? HOT 6
- RefundLineItem SubTotalTaxSet - copypaste issue? HOT 1
- Feature: Generate methods based on .graphql files HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shopifysharp.