Skip to content

Instantly share code, notes, and snippets.

@saifulapm
Last active June 3, 2021 12:10
Show Gist options
  • Select an option

  • Save saifulapm/9682fb603f1670e7ea085f35f6cb7b71 to your computer and use it in GitHub Desktop.

Select an option

Save saifulapm/9682fb603f1670e7ea085f35f6cb7b71 to your computer and use it in GitHub Desktop.
var dataProducts = [];
var totalProducts = 0;
var dataLoaded = false;
var totalPage = 0;
var maxLimit = 5000;
function getCategory(type, total = 0) {
var brand = $("#brand option:selected").val();
var year = $("#years option:selected").val();
var make = $("#make option:selected").val();
var model = $("#model option:selected").val();
var subModel = $("#subModel option:selected").val();
var engine = $("#engine option:selected").val();
var categoryId = $("#category option:selected").val();
var partId = $("#parts option:selected").val();
if (type == 'next') {
if (total != 0) {
var No = total;
} else {
var No = dataProducts['0']['Properties']['endingRecord'];
}
} else {
var savedNo = localStorage.getItem(csrf_token);
if (savedNo) {
var No = savedNo;
} else {
var No = "";
}
}
var recordsPerPage = $('#optionTopPartsPerPage').val();
var skuName = $('#textSearch').val().trim();
var skuName = encodeURIComponent(skuName.toUpperCase());
var value = $('#value').val();
var now = new Date();
var n = now.getTime();
$.ajax({
type: 'post',
url: 'api/makeEndecaCall/ts=' + n + '',
async: true,
cache: false,
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
},
data: {
recordsPerPage: 1000,
year: year,
make: make,
model: model,
subModel: subModel,
engine: engine,
brand: brand,
No: No,
skuName: skuName,
categoryId: categoryId,
partId: partId,
type: "category"
},
statusCode: {
500: function() {
alert("Something Went Wrong");
}
},
success: function(data) {
totalProducts = data['0']['Properties']['totalNumberOfRecords'];
dataProducts = data;
if (total != 0) {
config.count = 0;
}
if (type == 'next') {
localStorage.setItem(csrf_token, dataProducts['0']['Properties']['endingRecord']);
}
dataLoaded = true;
},
});
}
var config = {
count: 0
};
var cart_id = $("#current_cart_id").val();
function emptyCart() {
$.ajax({
type: 'POST',
url: 'api/emptyCart',
data: {
cart_id: cart_id
},
success: function(data) {
console.log('Cart Cleared');
console.log('Current Runing:' + dataProducts['0']['Properties']['endingRecord']);
},
error: function() {
console.log('Error on Clear Cart');
}
});
}
function syncStore(callback) {
if (dataProducts[config.count] !== undefined && dataLoaded ) {
var item = dataProducts[config.count];
$.ajax({
type: 'GET',
url: "https://api.usautoparts.io/autoSuggest?q=" + item['sku'],
dataType: 'json',
cache: false,
success: function(json) {
var index = json.suggestion.findIndex(x => x.sku === item['sku']);
if (json.suggestion[index] != undefined) {
var index = json.suggestion.findIndex(x => x.sku === item['sku']);
var partName = json.suggestion[index].part_name;
var parturl = partName.replaceAll(" ", "-").toLowerCase();
var url = 'https://www.carparts.com/'+parturl+'/'+json.suggestion[index].brand_name+'/'+json.suggestion[index].url;
$.ajax({
type: 'GET',
url: url,
dataType: 'text',
cache: false,
success: function(content) {
var tempElement = document.createElement('div');
tempElement.innerHTML = content;
for (var i = 0; i < tempElement.getElementsByTagName('script').length; i++) {
eval(JSON.parse(JSON.stringify(tempElement.getElementsByTagName('script')[i].innerHTML)));
}
// console.log(window.__COMBINED_DATA__);
if(window.__COMBINED_DATA__ === undefined || window.__COMBINED_DATA__.productDetails === undefined || window.__COMBINED_DATA__.productDetails.brand === undefined) {
config.count = config.count + 1;
setTimeout(callback, 500);
} else {
var sku = window.__COMBINED_DATA__.productDetails.sku;
var brand = window.__COMBINED_DATA__.productDetails.brand;
if (sku !== undefined && brand !== undefined) {
var fitment_url = 'https://api.usautoparts.io/fitment?brand='+brand+'&offset=0&size=3000&sku='+sku;
// console.log(fitment_url);
$.ajax({
url: fitment_url,
dataType: 'json',
success: function( fitment_data ) {
emptyCart();
if (!item['Properties']['id']) {
item['Properties']['id'] = item['id'];
}
if (!item['Properties']['titleSku']) {
item['Properties']['titleSku'] = item['Properties']['title'];
}
if (!item['Properties']['P_brand']) {
item['Properties']['P_brand'] = item['brand'];
}
if (item['Properties']['parts']) {
item['Properties']['parts'] = "";
}
$.ajax({
type: 'POST',
url: 'addItemToCart',
data: {
properties: item['Properties'],
_token: csrf_token,
recordSpec: item['Record Spec'],
qty: 1,
description: item['description'],
cart_id: cart_id
},
success: function(cart_data) {
$.ajax({
type: 'POST',
url: 'checkout',
timeout: 120000,
data: {
cart_id: cart_id,
page: 1,
record_per_page: 10,
_token: csrf_token,
},
success: function(data) {
if (data['items'][0] != undefined) {
var total = parseFloat(data['total']) + parseFloat(data['total'] * 0.25);
var local_data = {
'action': 'syncNow',
'brand': window.__COMBINED_DATA__.productDetails.brand,
'title': window.__COMBINED_DATA__.productDetails.skuTitle,
'price': window.__COMBINED_DATA__.productDetails.pricing.listPrice,
'barcode': item['Properties']['oem_Number'],
'quantity': data['items'][0]['max_available_all'],
'mainprice': total.toFixed(2),
'sku': window.__COMBINED_DATA__.productDetails.sku,
'category1': window.__COMBINED_DATA__.productDetails.topLevelCategory,
'category2': window.__COMBINED_DATA__.productDetails.category,
'category3': window.__COMBINED_DATA__.productDetails.subCategory,
'productAttributes': window.__COMBINED_DATA__.productDetails.productAttributes,
'fitments': fitment_data
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/product",
dataType: 'json',
data: local_data,
success: function(data) {
console.log(data);
config.count = config.count + 1;
setTimeout(callback, 500);
},
error: function() {
var local_data = {
'action': 'addError',
'sku': window.__COMBINED_DATA__.productDetails.sku
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/errors",
dataType: 'json',
data: local_data
}).done(function(json) {
console.log('Somehting wrong');
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
} else {
setTimeout(callback, 500);
}
},
error: function() {
var local_data = {
'action': 'addError',
'sku': window.__COMBINED_DATA__.productDetails.sku
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/errors",
dataType: 'json',
data: local_data
}).done(function(json) {
console.log('Somehting wrong');
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
},
error: function( data ) {
var local_data = {
'action': 'syncNow',
'brand': window.__COMBINED_DATA__.productDetails.brand,
'title': window.__COMBINED_DATA__.productDetails.skuTitle,
'price': window.__COMBINED_DATA__.productDetails.pricing.listPrice,
'barcode': item['Properties']['oem_Number'],
'quantity': 0,
'mainprice': 0,
'sku': window.__COMBINED_DATA__.productDetails.sku,
'category1': window.__COMBINED_DATA__.productDetails.topLevelCategory,
'category2': window.__COMBINED_DATA__.productDetails.category,
'category3': window.__COMBINED_DATA__.productDetails.subCategory,
'productAttributes': window.__COMBINED_DATA__.productDetails.productAttributes,
'fitments': fitment_data
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/product",
dataType: 'json',
data: local_data,
success: function(data) {
console.log(data);
config.count = config.count + 1;
setTimeout(callback, 500);
},
error: function() {
var local_data = {
'action': 'addError',
'sku': window.__COMBINED_DATA__.productDetails.sku
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/errors",
dataType: 'json',
data: local_data
}).done(function(json) {
console.log('Somehting wrong');
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
}
});
},
error: function( data ) {
var local_data = {
'action': 'addError',
'sku': window.__COMBINED_DATA__.productDetails.sku
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/errors",
dataType: 'json',
data: local_data
}).done(function(json) {
console.log('Somehting wrong');
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
} else {
config.count = config.count + 1;
setTimeout(callback, 500);
}
}
},
error: function() {
var local_data = {
'action': 'addError',
'sku': item['sku']
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/greb_errors",
dataType: 'json',
data: local_data
}).done(function(json) {
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
} else {
var local_data = {
'action': 'addError',
'sku': item['sku']
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/not_errors",
dataType: 'json',
data: local_data
}).done(function(json) {
console.log('Not Found');
console.log('SKU: ' + item['sku']);
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
},
error: function() {
var local_data = {
'action': 'addError',
'sku': item['sku']
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/errors",
dataType: 'json',
data: local_data
}).done(function(json) {
console.log('Somehting wrong');
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
} else if(dataLoaded) {
if(totalProducts > dataProducts['0']['Properties']['endingRecord'] && dataProducts['0']['Properties']['endingRecord'] <= maxLimit) {
if(totalPage > 9) {
window.location.href = "https://www.perfectfitgroup.com/orderEntry";
} else {
dataLoaded = false;
totalPage = totalPage + 1;
getCategory('next');
config.count = 0;
setTimeout(callback, 500);
}
} else {
console.log("Completed " + maxLimit);
}
} else {
console.log('Waiting for new Data');
setTimeout(callback, 500);
}
}
// Change Brand Here
$('#brand').val('Replacement').trigger('change');
(function syncNow() {
syncStore(syncNow);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment