to be honest, I don’t have any knowledge with coding that’s why I use blackbox ai to make it. I’m trying to have a vehicle fitment filter in the homepage in shopify where the ktypes is the identifier. However, the vehicle data javascript is not pulling up.
here’s the code of the html/css
<div
id="vehicle-fitment-filter"
style="display:flex; gap:10px; overflow-x:auto; padding:15px; background:#f7f7f7; border-radius:6px; border:1px solid #ddd; margin-bottom:30px; align-items:center; flex-wrap: nowrap;"
>
<select
id="filter-make"
enabled
style="padding:8px 12px; border-radius:4px; border:1px solid #ccc; min-width:150px;"
>
<option value="">Select Make</option>
</select>
<select
id="filter-model"
disabled
style="padding:8px 12px; border-radius:4px; border:1px solid #ccc; min-width:150px;"
>
<option value="">Select Model</option>
</select>
<select
id="filter-type"
disabled
style="padding:8px 12px; border-radius:4px; border:1px solid #ccc; min-width:150px;"
>
<option value="">Select Type</option>
</select>
<select
id="filter-year"
disabled
style="padding:8px 12px; border-radius:4px; border:1px solid #ccc; min-width:150px;"
>
<option value="">Select Year</option>
</select>
<button
id="filter-submit"
disabled
style="padding:9px 20px; background:#007bff; border:none; color:#fff; border-radius:4px; cursor:pointer;"
>
Search
</button>
</div>
<!-- Load the vehicle data JS asset -->
<script src="{{ 'vehicle-data.js' | asset_url }}" id="vehicle-data-script"></script>
<script>
const selectMake = document.getElementById('filter-make');
const selectModel = document.getElementById('filter-model');
const selectType = document.getElementById('filter-type');
const selectYear = document.getElementById('filter-year');
const btnSubmit = document.getElementById('filter-submit');
// The functions to populate dropdowns (same as previous code)...
function populateMakes() {
selectMake.innerHTML = '<option value="">Select Make</option>';
vehicleData.forEach(v => {
const option = document.createElement('option');
option.value = v.make;
option.textContent = v.make;
selectMake.appendChild(option);
});
selectMake.disabled = false;
btnSubmit.disabled = false;
}
function populateModels(selectedMake) {
selectModel.innerHTML = '<option value="">Select Model</option>';
selectType.innerHTML = '<option value="">Select Type</option>';
selectYear.innerHTML = '<option value="">Select Year</option>';
selectModel.disabled = true;
selectType.disabled = true;
selectYear.disabled = true;
if (!selectedMake) return;
const make = vehicleData.find(v => v.make === selectedMake);
if (!make) return;
make.models.forEach(m => {
const option = document.createElement('option');
option.value = m.model;
option.textContent = m.model;
selectModel.appendChild(option);
});
selectModel.disabled = false;
}
function populateTypes(make, model) {
selectType.innerHTML = '<option value="">Select Type</option>';
selectYear.innerHTML = '<option value="">Select Year</option>';
selectType.disabled = true;
selectYear.disabled = true;
if (!make || !model) return;
const makeEntry = vehicleData.find(v => v.make === make);
if (!makeEntry) return;
const modelEntry = makeEntry.models.find(m => m.model === model);
if (!modelEntry) return;
modelEntry.types.forEach(t => {
const option = document.createElement('option');
option.value = t.type;
option.textContent = t.type;
selectType.appendChild(option);
});
selectType.disabled = false;
}
function populateYears(make, model, type) {
selectYear.innerHTML = '<option value="">Select Year</option>';
selectYear.disabled = true;
if (!make || !model || !type) return;
const makeEntry = vehicleData.find(v => v.make === make);
if (!makeEntry) return;
const modelEntry = makeEntry.models.find(m => m.model === model);
if (!modelEntry) return;
const typeEntry = modelEntry.types.find(t => t.type === type);
if (!typeEntry) return;
typeEntry.years.forEach(y => {
const option = document.createElement('option');
option.value = y.ktype;
option.textContent = y.year;
selectYear.appendChild(option);
});
selectYear.disabled = false;
}
// Event Listeners
selectMake.addEventListener('change', (e) => {
populateModels(e.target.value);
selectType.innerHTML = '<option value="">Select Type</option>';
selectYear.innerHTML = '<option value="">Select Year</option>';
selectType.disabled = true;
selectYear.disabled = true;
});
selectModel.addEventListener('change', (e) => {
populateTypes(selectMake.value, e.target.value);
selectYear.innerHTML = '<option value="">Select Year</option>';
selectYear.disabled = true;
});
selectType.addEventListener('change', (e) => {
populateYears(selectMake.value, selectModel.value, e.target.value);
});
btnSubmit.addEventListener('click', () => {
const ktype = selectYear.value;
if (!ktype) {
alert('Please select a valid Year.');
return;
}
const url = new URL(window.location.origin + '/pages/vehicle-fitment-results');
url.searchParams.set('ktype', ktype);
window.location.href = url.toString();
});
document.getElementById('vehicle-data-script').onload = () => {
if (typeof vehicleData === 'undefined') {
alert('Vehicle data failed to load. Please check vehicle-data.js');
return;
}
populateMakes();
};
</script>
{% schema %}
{
"name": "Vehicle Fitment Filter",
"settings": [],
"presets": [{ "name": "Vehicle Fitment Filter" }]
}
{% endschema %}
as for the javascript for the vehicle fitment here’s the code (just for example)
const vehicleData = [
{
make: "AC",
models: [
{
model: "Ace",
types: [
{
type: "4.6",
years: [
{year: 1998, ktype: "126007" },
{year: 1999, ktype: "126007" },
{year: 2000, ktype: "126007" },
{year: 2001, ktype: "126007" },
{year: 2002, ktype: "126007" },
{year: 2003, ktype: "126007" },
{year: 2004, ktype: "126007" },
{year: 2005, ktype: "126007" },
{year: 2006, ktype: "126007" },
{year: 2007, ktype: "126007" },
{year: 2008, ktype: "126007" },
{year: 2009, ktype: "126007" },
{year: 2010, ktype: "126007" },
{year: 2011, ktype: "126007" },
{year: 2012, ktype: "126007" },
{year: 2013, ktype: "126007" },
{year: 2014, ktype: "126007" },
{year: 2015, ktype: "126007" },
{year: 2016, ktype: "126007" },
{year: 2017, ktype: "126007" },
{year: 2018, ktype: "126007" },
{year: 2019, ktype: "126007" },
{year: 2020, ktype: "126007" },
{year: 2021, ktype: "126007" },
{year: 2022, ktype: "126007" },
{year: 2023, ktype: "126007" },
{year: 2024, ktype: "126007" }
]
}
]
}
]
}
];
and this is the code for the result page
{% assign ktype_filter = request.params.ktype %}
<h1>Vehicle Fitment Search Results</h1>
{% if ktype_filter == blank %}
<p>Please select a vehicle on the homepage.</p>
{% else %}
<p>
Showing products compatible with vehicle code: <strong>{{ ktype_filter }}</strong>
</p>
{% assign matched_products = collections.all.products
| where: 'metafields.Vehicle_KType.compatibility.ktype', ktype_filter
%}
{% if matched_products == empty %}
<p>No compatible products found.</p>
{% else %}
<div style="display:flex; flex-wrap:wrap; gap:20px;">
{% for product in matched_products %}
<div style="width:calc(25% - 20px); border:1px solid #ccc; border-radius:5px; padding:10px; box-sizing:border-box;">
<a href="{{ product.url }}">
{% if product.featured_image %}
{% endif %}
<h2 style="font-size:16px; margin:10px 0;">{{ product.title }}</h2>
</a>
<p>{{ product.price | money }}</p>
</div>
{% endfor %}
</div>
{% endif %}
{% endif %}
I hope guys you could help me with this.