⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

1-n on the fly



kellen

kellen
  • profile picture
  • Member

Posted 12 September 2013 - 07:27 AM

Hi,

 

Here goes my requirement - I would like to add n price-types to a product. 

 

Say I have a product, Product A

Product A can have different price types based on quantity

qty      - price

375ml - 20$

500ml - 30$

750ml - 50$

Quantity and Price will be different for each product.

 

I want to achieve the above scenario using grocery crud. Please help me on this.

 

Thanks in advance.


davidoster

davidoster
  • profile picture
  • Member

Posted 12 September 2013 - 08:33 AM

Hello [member=kellen] and welcome to the forums.

What you describe above doesn't actually tell us how you want to use this.

 

The most straight forward way would be to have a table products with fields id, name,quantity, price

e.g.

100,A,375ml,20$

101,A,500ml,30$

102,A,750ml,50$

103,B,125ml,13$ etc

 

Then if we assume that you want to somehow use these, let's say for sales then by using the set_relation  and with a "{name} {quantity} {price}" the user can select the one he needs.

 

Caution though, this is not the most optimal way of doing it. For such a configuration you should have two tables

products and product_details where the qty and price go to the product details.

If you choose to have 2 tables products and product_details then it will need a bit more effort to get it right since you will have to fetch information from two different tables.


kellen

kellen
  • profile picture
  • Member

Posted 12 September 2013 - 09:07 AM

Hi davidoster,

 

Sorry for the short description. Let me explain in detail. I can't use single table approach since a product can have multiple quantities. So based on database normalization I have used 2 tables approach. Please find the table structure details.

  1. Product (fields: id, name, description)
  2. ProductDetails (fields: id, product_id, price, quantity)

I think this is the right database design and been used in common. But I can't find any documentation how to setup this in grocery-crud. I am stuck on this. Please help. 

 

Thanks in advance


davidoster

davidoster
  • profile picture
  • Member

Posted 12 September 2013 - 09:42 AM

This is not supported by default on Grocery CRUD as far as I am aware.

In such situations, at least from my point of view it is better to use a custom model.

Check here for some sample code. Not exactly related but I hope it will give you an idea.


briggers

briggers
  • profile picture
  • Member

Posted 12 September 2013 - 10:04 AM

You could try this.

 

Add a custom action which takes you to a "price/quantity" crud where you can display just the price/quantities for the selected product from table ProductDetails and add/edit/delete etc. those there.

In your product grid use a callback_column function to get the price/quantity from the ProductDetails table and display them as a string. You would set that field to be excluded from the add and edit pages.