Giter VIP home page Giter VIP logo

Comments (3)

concua1111 avatar concua1111 commented on June 28, 2024

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.

Loocist23 avatar Loocist23 commented on June 28, 2024

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.

nozzlegear avatar nozzlegear commented on June 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.