Anyone know how to do this please?
Feb 12, 2016
21
Level 51
Issue with custom e-commerce & select dropdowns
Hi all,
I have a small problem and not sure how to fix it.
I have a custom e-commerce website, selling clothing which has different sizes & colors. The problem I'm having now if the client enters a product with one color and 2 or 3 different sizes it shows up like this:

The database looks like:

As you can see that's probably what is expected, but I need it do be Yellow one then I use Vue to populate the sizes dropdown once a color is selected.
This is the block of code in question:
<div class="col s12 m6">
@if(! $product->options->isEmpty() && ! $product->options->isEmpty())
<select name="colour" class="browser-default colors" data-index="{!! $product->id !!}" v-model="colour" v-on="change:selectedColour" id="optionID">
<option value="" disabled selected>Choose a Colour</option>
@foreach($product->options as $option)
<option v-model="option_id" value="{!! $option->product_id !!}-{!! $option->colour !!}" id="optionId" data-optionId="{!! $option->id !!}">{!! $option->colour !!} @if ($option->stock > 0) - In Stock @endif</option>
@endforeach
</select>
@else
<select class="browser-default colors">
<option value="Out of Stock">(Out of Stock)</option>
</select>
@endif
<label></label>
</div>
This is the VueJS
new Vue({
el: '#cart',
data: {
colour: '',
sizes: '',
selectedSize: ''
},
methods: {
fetchSizes: function() {
this.$http.get('/related-sizes?info=' + this.colour, function(sizes) {
this.$set('sizes', sizes);
});
},
selectedColour: function() {
this.fetchSizes();
}
}
});
Which uses the following method on a controller
public function selectedColour(Request $request)
{
$info = explode('-', $request->input('info'));
$cat = Options::where('product_id', $info[0])
->where('colour', $info[1])
->having('stock', '>', 0)
->orderBy('size', 'asc')
->get(['id', 'size', 'colour', 'stock']);
return Response::json($cat, 200);
}
So question is how can I get it to just show Yellow once in this example but yet show the 2-3 different sizes available?
Please or to participate in this conversation.