There are four levels categories in my site, e.g.
Category A -> Category B-> Category C -> Category D
and the products belong to Category D. I want to fetch the cheapest product, what should I do? Or is there any function I can use to fetch the min price of the product by a product id?
do you need the exact code or just a guideline how to go about this?
you could list the products in that specific category and then run a check (in a loop) which product is cheapest by comparing their prices.
set cheapest is price first product
set cheapest_id is id first product
for each product in category
new_price is get price next product
if new_price is lower than cheapest
cheapest is new_price
cheapest_id is product_id
end for each
you can perform any action after that using the cheapest_id to identify the product
Thank you for the comments, actually, I could not figure out the product list in the catalog page (In other words, I can not display the whole structure from top category to base prodcut). I used these codes to fetch categories:
if(shopp('category','hascategories')):
while(shopp('category','subcategories')):
......(in my example, there should be another cycle to fetch the sub categories in this place, but I have no idea to figure it out with this plugin's codes)
endwhile;
endif;
I want to use it on the catalog page or the home of the shop page, not already in a sub/sub/sub category. Here is an example for my categories:
first level: Sports
second levle: Basketball
third level: T-shirt
four levle: Anniversary
product: 5th Champion T-shit ($25.5)
So, when someone come to the home of shop (e.g. http://mydomain.com/shop/), he will see a mixed list, such as following:
Top category: Sports
Second category: Basketball
Third category: T-shirt
START AT: $25.5 (here I want to get the min price of the all fourth categories)
It is the catalog page, so actually, I will list all the top categories (maybe sports, computers......), in every top category, list all the second categories, and in every second category, list all the third category, then under the third category, there is the text: "START AT: $XX.XX", it is the cheapest product which is belong to the current third category.
Hop you can understand this example. Thank you very much for your comments.
It migh be easier to write a function (add it to functions.php) instead of determining which catergory to use.
it could be something like (just describing, no real coding yet)
shoppMyFunction(category-id)
set lowprice = 0
get category
if category hasproducts
while category hasproducts
if pricetagproduct < lowprice
lowprice = pricetagproduct
endif
endwhile
endif
return lowprice
in your catalog.php
it would look like shoppMyFunction(3) for example
Yes, I thought about the same method as you metioned above, but unfortunately, I have no idea to figure out the real price of a product, because there are some thipping rates & other discount calculation, if I only fetch the price from database, I don't think it will be correct.
Because of the reasons above, I want to use the plugin's template codes to go through the whole structure, and get the real price with the code <?php $each_base_price = shopp('product','price','return=true');?>, then I can get the cheapest.
uhm, you are still fetching the price from database. So the price may still not be right. But yes it can be done going through the structure, hierarchy.
How to fetch the min price for a category
Profile
There are four levels categories in my site, e.g.
Category A -> Category B-> Category C -> Category D
and the products belong to Category D. I want to fetch the cheapest product, what should I do? Or is there any function I can use to fetch the min price of the product by a product id?
Profile
do you need the exact code or just a guideline how to go about this?
you could list the products in that specific category and then run a check (in a loop) which product is cheapest by comparing their prices.
set cheapest is price first product
set cheapest_id is id first product
for each product in category
new_price is get price next product
if new_price is lower than cheapest
cheapest is new_price
cheapest_id is product_id
end for each
you can perform any action after that using the cheapest_id to identify the product
Profile
Thank you for the comments, actually, I could not figure out the product list in the catalog page (In other words, I can not display the whole structure from top category to base prodcut). I used these codes to fetch categories:
if(shopp('category','hascategories')):
while(shopp('category','subcategories')):
......(in my example, there should be another cycle to fetch the sub categories in this place, but I have no idea to figure it out with this plugin's codes)
endwhile;
endif;
Profile
Can you explain how and where you want to use it?
I thought you wanted to know the cheapest product of a category once ppl where already in a sub/sub/sub category by browsing or something.
Can you shed a little more light on the category structure you are using, by giving some example categories?
Profile
I want to use it on the catalog page or the home of the shop page, not already in a sub/sub/sub category. Here is an example for my categories:
first level: Sports
second levle: Basketball
third level: T-shirt
four levle: Anniversary
product: 5th Champion T-shit ($25.5)
So, when someone come to the home of shop (e.g. http://mydomain.com/shop/), he will see a mixed list, such as following:
Top category: Sports
Second category: Basketball
Third category: T-shirt
START AT: $25.5 (here I want to get the min price of the all fourth categories)
It is the catalog page, so actually, I will list all the top categories (maybe sports, computers......), in every top category, list all the second categories, and in every second category, list all the third category, then under the third category, there is the text: "START AT: $XX.XX", it is the cheapest product which is belong to the current third category.
Hop you can understand this example. Thank you very much for your comments.
Profile
It migh be easier to write a function (add it to functions.php) instead of determining which catergory to use.
it could be something like (just describing, no real coding yet)
shoppMyFunction(category-id)
set lowprice = 0
get category
if category hasproducts
while category hasproducts
if pricetagproduct < lowprice
lowprice = pricetagproduct
endif
endwhile
endif
return lowprice
in your catalog.php
it would look like shoppMyFunction(3) for example
Profile
Hello "Just me",
Yes, I thought about the same method as you metioned above, but unfortunately, I have no idea to figure out the real price of a product, because there are some thipping rates & other discount calculation, if I only fetch the price from database, I don't think it will be correct.
Because of the reasons above, I want to use the plugin's template codes to go through the whole structure, and get the real price with the code <?php $each_base_price = shopp('product','price','return=true');?>, then I can get the cheapest.
Profile
uhm, you are still fetching the price from database. So the price may still not be right. But yes it can be done going through the structure, hierarchy.
Reply
You must log in to post.