stats from sales to show on listing page

So, I want to include some stats and reviews from orders on listings pages. Here is my intended functionality: On the listing page, you will be able to see the total amount of orders of that product and the total amount of $ generated from orders. Also, I want buyers to be able to include a message when they complete the order. This message will show on the listing page (like a review). If someone could describe how to do this, I would be very grateful. Thanks

Posted about 5 years ago by Markus Lopez
Posted about 5 years ago by Alex Yang

Markus, here's an updated outline:

1. Add a 'total_revenue' column to your Listing database
2. Update the blank 'total_revenue' fields for your existing Listings by using the Rails Console
3. In your orders_controller.rb file, make sure that each new Order that's placed updates the 'total_revenue' field for that Listing
4. In your Listing Show Page, retrieve the 'total_revenue' field for that Listing with something like '@listing.total_revenue'

0
Posted about 5 years ago by Alex Yang

Markus, you've mentioned a couple different features here. First, to set up stats, you'll need to add some fields to your Listing database so you can track them. You'll do this by generating a migration. Then, you'll need to use these new fields and start tracking the desired stats. Whenever a new order is placed, in addition to creating the Order, you'll also want to update the Listing's stats (e.g. total sales, total revenue generated). Once these fields are being filled in properly (and you've made sure that no old Listings have blank fields), you'll be able to display it in your View Pages however you like.

Reviews will be more complicated to integrate into your site because you'll need a whole new resource entirely. The easiest way would be to generate a Review scaffold and then customize it by removing the pieces you don't need. The high-level flow of what you'll need to do is: set up your Review database correctly (and linking it with Listings and/or Orders), make sure that you can create/edit/destroy Reviews properly, and finally integrate them into the Listing View Pages on your site.

Hope this helps!

0
Posted about 5 years ago by Markus Lopez

would i be able to define some new variables in my orders controller? i.e.: # of orders, and $ revenue

0
Posted about 5 years ago by Alex Yang

Yes, all the definitions for variables go in your controller.

0
Posted about 5 years ago by Markus Lopez

ok, i was able to get the number of sales to show up fine. i'm stuck on total revenue tho. is there any way to add up the order.listing.price variable located in my "sales" file, JUST for that specific item? that's the one part that i can't get

0
Posted about 5 years ago by Alex Yang

It sounds like you're running the calculation every time you have to display total revenue, but this isn't a very efficient solution. A better solution would be to follow the outline I gave above, where you can calculate total once (when an order is placed) and store it in a column in your database. Then when you want to display it, you just have to retrieve the value from the database.

If you just want a quick-and-dirty solution, then you'd need to first find all the orders for that specific listing. For example, you could write:

@orders = Order.where(listing: @order.listing)
@revenue = 0
@orders.each do |order|
@revenue = @revenue + order.listing.price
end

This code finds all the orders for that specific listing (assuming that the current Order is already defined in the @order variable). Then it runs a loop, adding up the price of each Order's Listing to get the total revenue in the @revenue variable. Make sense?

0
Posted about 5 years ago by Markus Lopez

been still struggling since yesterday afternoon. pulling my hair out a bit. so i added a variable of "total" to orders. now i'm struggling on the next step of actually calculating the total revenue of orders. i know you wrote those lines of code that locate orders of that specific listing and calculate the total. but i'm not sure where to put this info and how to define it. thanks again for any help.

0
Posted about 5 years ago by Alex Yang

Do you mean you added a 'total' column to your Orders database? You would put the lines of code I wrote in the controller of whichever page you need to display it on. For example, if you want to display these stats on the Listing page, you would place the code under the 'show' action of the listings_controller.rb file.

You can set up this calculation to happen in either the model or in the controller, but it will be easiest to set it up in the controller.

0
Posted about 5 years ago by Markus Lopez

i was able to figure this one out. thanks

0
Posted about 5 years ago by Markus Lopez

yes to my orders database. is that the wrong place for it? i've added your code to my listings_controller, but I'm not sure how to call for it in my show page. i'm getting an undefined method for 'revenue' for what i've been trying.

0
Posted about 5 years ago by Markus Lopez

i think one thing i'm confused about is we talked about defining variables in the controller, but i saw other examples with defining variables in the model. which way is best?

0
Posted about 5 years ago by Markus Lopez

thanks for the outline. i'll give it a shot

0