Skip to content

Instantly share code, notes, and snippets.

@cawecoy
Last active July 22, 2023 05:01
Show Gist options
  • Save cawecoy/80326d95ba2bb9f0a3bfbbdd537b287b to your computer and use it in GitHub Desktop.
Save cawecoy/80326d95ba2bb9f0a3bfbbdd537b287b to your computer and use it in GitHub Desktop.
Generate product attributes correctly formated to be imported from Pangaea Digital Agency to WordPress Woocommerce
// replace variable value after =
var attr = [
{
"OptionList.0.ProductId": "62977ef1489dac21a4364b5c",
"OptionList.0.Name": "Kleur",
"OptionList.0.Type": "String",
"OptionList.0.Value": "Blauw",
"OptionList.1.ProductId": "62977ef1489dac21a4364b5c",
"OptionList.1.Name": "Maat",
"OptionList.1.Type": "String",
"OptionList.1.Value": "55cm",
"OptionList.2.ProductId": "62977ef1489dac21a4364b5c",
"OptionList.2.Name": "Materiaal",
"OptionList.2.Type": "String",
"OptionList.2.Value": "Latex rubber"
},
{
"OptionList.0.ProductId": "62977ef1489dac21a4364b42",
"OptionList.0.Name": "Kleur",
"OptionList.0.Type": "String",
"OptionList.0.Value": "Blauw",
"OptionList.1.ProductId": "62977ef1489dac21a4364b42",
"OptionList.1.Name": "Materiaal",
"OptionList.1.Type": "String",
"OptionList.1.Value": "Latex rubber",
"OptionList.2.ProductId": "62977ef1489dac21a4364b42",
"OptionList.2.Name": "Maat",
"OptionList.2.Type": "String",
"OptionList.2.Value": "65cm"
}
]
// replace variable value after =
var products = {
"62977ef1489dac21a4364b5c": {
"ParentId": "62977ef1489dac21a4364b57",
"isParent": null,
"meta:attribute_pa_Maat": "",
"attribute:pa_Maat": "",
"attribute_data:pa_Maat": "",
"attribute_default:pa_Maat": "",
"meta:attribute_pa_Kleur": "",
"attribute:pa_Kleur": "",
"attribute_data:pa_Kleur": "",
"attribute_default:pa_Kleur": "",
"meta:attribute_pa_Materiaal": "",
"attribute:pa_Materiaal": "",
"attribute_data:pa_Materiaal": "",
"attribute_default:pa_Materiaal": ""
},
"62977ef1489dac21a4364b42": {
"ParentId": "62977ef1489dac21a4364b3a",
"isParent": null,
"meta:attribute_pa_Maat": "",
"attribute:pa_Maat": "",
"attribute_data:pa_Maat": "",
"attribute_default:pa_Maat": "",
"meta:attribute_pa_Kleur": "",
"attribute:pa_Kleur": "",
"attribute_data:pa_Kleur": "",
"attribute_default:pa_Kleur": "",
"meta:attribute_pa_Materiaal": "",
"attribute:pa_Materiaal": "",
"attribute_data:pa_Materiaal": "",
"attribute_default:pa_Materiaal": ""
}
}
// meta:attribute_pa_color = red (filho)
// attribute:pa_color = red|blue
// attribute_data:pa_color = 0|1|1 = position|visible|variation
// attribute_default:pa_color = red
function capitalizeOnlyFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.toLowerCase().slice(1);
}
var optionListMaxQuantity = 100;
var names = new Set();
for(var i = 0; i < attr.length; i++){
for(var j = 0; j <= optionListMaxQuantity; j++){
//console.log(i+' - '+j)
if(attr[i]['OptionList.'+j+'.ProductId'] === undefined){
//j = optionListMaxQuantity;
}
else if(products[attr[i]['OptionList.'+j+'.ProductId']] === undefined){
console.log('product doesn`t exist: ' + attr[i]['OptionList.'+j+'.ProductId'] + ' | i = ' +i+' | j = '+j)
}
else{
var id = attr[i]['OptionList.'+j+'.ProductId']
var name = attr[i]['OptionList.'+j+'.Name']
var value = capitalizeOnlyFirstLetter(attr[i]['OptionList.'+j+'.Value'])
names.add(name);
//console.log(id)
if(products[id]['meta:attribute_pa_'+name] == '' || products[id]['meta:attribute_pa_'+name] === undefined){
products[id]['meta:attribute_pa_'+name] = value
}
else{
var at = products[id]['meta:attribute_pa_'+name].split('|')
var found = false
for(var k = 0; k < at.length; k++){
if(at[k] == value){
found = true
k = at.lenght
}
}
if(!found){
products[id]['meta:attribute_pa_'+name] += ('|'+value)
}
}
if(!products[id].isParent){
id = products[id].ParentId
if(products[id] !== undefined){
products[id]['attribute_data:pa_'+name] = '0|1|1'
}
}
else{
products[id]['attribute_default:pa_'+name] = value
if(products[id]['attribute_data:pa_'+name] == ''){
products[id]['attribute_data:pa_'+name] = '0|1|0'
}
}
if(products[id] === undefined){
console.log('# product doesn`t exist: ' + id)
}
else{
if(products[id]['attribute_default:pa_'+name] == '' || products[id]['attribute_default:pa_'+name] === undefined){
products[id]['attribute_default:pa_'+name] = products[id]['attribute:pa_'+name] = value
}
else{
var at = products[id]['attribute:pa_'+name].split('|')
var found = false
for(var k = 0; k < at.length; k++){
if(at[k] == value){
found = true
k = at.lenght
}
}
if(!found){
products[id]['attribute:pa_'+name] += ('|'+value)
}
}
}
}
}
}
console.log('end')
names = [...names];
// fix for plugin bug & check if there are buggy combination of attributes (WC variant can have only or all attribute possible values)
for(var id in products){
if(!products[id].isParent){
var parent_id = products[id].ParentId
if(products[parent_id] !== undefined){
// variations that includes all possibile attribute's values should be empty to works
for(var name in names){
if(name in products[id]){
if(products[id]["meta:attribute_pa_"+name].length == products[parent_id]["attribute:pa_"+name].length){
products[id]["meta:attribute_pa_"+name] = ''
}
}
}
var c = 0;
for(var name in names){
if(name in products[id]){
if(products[id]["meta:attribute_pa_"+name].length != products[parent_id]["attribute:pa_"+name].length){
if(products[id]["meta:attribute_pa_"+name].split('|').length > 1){
c++;
}
// console.log('# '+id+': child product possible attribute value qty differs from the possible qty on the parent')
// console.log(products[id]["meta:attribute_pa_"+name].length +' != '+ products[parent_id]["attribute:pa_"+name].length)
}
}
}
if(c > 1){
console.log('#'+id+' with more than one attribute qty != from parrent`s attribute possible qty')
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment