Skip to content
Advertisement

Best way to create a table with each possible combination of product options and variations

I need to implement in PostgreSQL a table for each combination of option and variation.

This is my existing tables:

enter image description here

Imagine this scenario:

Product: T-Shirt

Options: Size, Color

Variations: Size:Small,Medium,Large | Color:White,Black,Yellow

I need to have a row for each combination to control quantity and price seperately.

So i need a row with Size:Small,Color:White, one with Size:Small,Color:Black etc…

It isn’t necessary to execute all in SQL, i manipulate the data in program logic in Go.

I thought about making a table with:

id |price | quantity | option1_id | option2_id | option3_id

and compare the values that i get from my form and get the appropriate price and qty.However i do not know how to guarantee that the options on my form are displayed on the correct order as to not put the option id on my query.

Is there better option?Perhaps saving the combination in JSON on one column and loading in a array in JS and manipulating it there?

To explain it a bit better(hopefully):

I display on my site some select inputs with the options and variations that i have inserted,like this.enter image description here Where each option on the dropdown menu has a value of variation_id and the name of the menu is the option_id,ex. 120:1400 where 120 is the option_id and 1400 is the variant attached to it and currently selected.So when user adds product to cart it sends this JSON to server[{"option_id":3,"variant_id":13},{"option_id":433,"variant_id":1085}]

I know how to make a table with each possible combination, but i don’t know how to link the variants selected to the one row on my table.

I want to implement the same way that Woocommerce does it.

This is the Attributes(Options)

enter image description here

And those are the Variations that are generated

enter image description here

As you can see there’s an entry for each possible configuration.

Currently i have set up the Name and Values of the third image. product_option has the name of the option and there are multiple product_variation rows pointed to the option_id, each with a variation name like Small,Medium etc…

I don’t know how to create a new table like the 4th image.

Advertisement

Answer

I have found a way.It’s called Cartesian Product.

Here is how it’s done.

This is a function that i found online,from this repo https://github.com/schwarmco/go-cartesian-product

func Iter(params ...[]interface{}) chan []interface{} {
    // create channel
    c := make(chan []interface{})
    // create waitgroup
    var wg sync.WaitGroup
    // call iterator
    wg.Add(1)
    iterate(&wg, c, []interface{}{}, params...)
    // call channel-closing go-func
    go func() { wg.Wait(); close(c) }()
    // return channel
    return c
}

// private, recursive Iteration-Function
func iterate(wg *sync.WaitGroup, channel chan []interface{}, result []interface{}, params ...[]interface{}) {
    // dec WaitGroup when finished
    defer wg.Done()
    // no more params left?
    if len(params) == 0 {
        // send result to channel
        channel <- result
        return
    }
    // shift first param
    p, params := params[0], params[1:]
    // iterate over it
    for i := 0; i < len(p); i++ {
        // inc WaitGroup
        wg.Add(1)
        // create copy of result
        resultCopy := append([]interface{}{}, result...)
        // call self with remaining params
        go iterate(wg, channel, append(resultCopy, p[i]), params...)
    }
}

Here’s how you can use it

a := []interface{}{"Small", "Medium", "Large"}

b := []interface{}{"White", "Black", "Yellow"}

var d [][]interface{}
d = append(d,a)
d = append(d,b)
c := Iter(d...)

for product := range c {
    fmt.Println(product)
}

I then store each product as a JSON string to the database

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement