Change value custom product Tag


#1

Any idea to display all product and just certain or one custom product tag list editor at entity screen for change value from Yes to No
i have idea to make item can’t order when custom product tag, name “Aktif” equal No.
if i give permit product tag list editor to user level like manager, i am scare they will change all custom product tag.


#2

There is no Action for updating a Product Tag and there is no API command for it either.

So there is no mechanism to set or change a Product Custom Tag value other than using the Product Tag Editor or directly modifying the DB (ie. running SQL Script via JScript).


#3

there is sample script for do that please


#4

No sample to do that. Will take time to develop since the Tags are stored in JSON format. I can put something together but it won’t happen until next week - no time right now.


#5

Thank You, i still try test it, but i get problem with json format to manage it.

ok


#6

Here is a start… EDIT: fully working!

function productTagUpdate(productName,tagName,tagValue) {
  // for testing
  //productName = 'Barena';
  //tagName = 'productType';
  //tagValue = 'Drink';
  
  var productId = 0;
  
  var SQL = "SELECT [Id] FROM [MenuItems] WHERE [Name] = '" + productName + "'";
  
  var sqlResult = sql.Exec(SQL);

  if (typeof sqlResult[0] === 'undefined') {
    dlg.ShowMessage('Could not find Product: ' + productName);
    return 1;
  }
  
  productId = sqlResult[0];
  
  SQL = "SELECT [CustomTags] FROM [MenuItems] WHERE [Id] = " + productId;
  sqlResult = sql.Exec(SQL);

  if (typeof sqlResult[0] === 'undefined') {
    dlg.ShowMessage('Could not find Product with Id: ' + productId);
    return 1;
  }

  // convert [CustomTags] JSON String to an Arry of Objects
  var jsonObj = JSON.parse(sqlResult[0]);
  
  var foundTag = false;
  
  var op = "productName:" + productName + "\r";
  
  // iterate the Array of Objects looking for the tagName
  for (var t=0; t<jsonObj.length; t++) {
  
    // if we find the tagName, replace the tagValue
    if (jsonObj[t].TN == tagName) {
      dlg.ShowMessage("Found Tag '" + tagName + "' for Product '" + productName + "'.\rChanging value from '" + jsonObj[t].TV + "' to '" + tagValue + "'");
      jsonObj[t].TV = tagValue;
      foundTag = true;
    }
    
    op += jsonObj[t].TN;
    op += ":";
    op += jsonObj[t].TV;
    op += "\r";
  }
  
  var newJSON = "";
  
  if (foundTag) {
    // convert the Array of Objects back into a JSON String
    newJSON = JSON.stringify(jsonObj);
    // do SQL UPDATE
    SQL = "UPDATE [MenuItems] SET [CustomTags] ='" + newJSON + "'  WHERE [Id] = " + productId;
    sqlResult = sql.Exec(SQL);
    dlg.ShowMessage("Executed SQL:\r"+SQL);
    return 0;
  } else {
    dlg.ShowMessage("FAILED to find Tag '" + tagName + "' for Product '" + productName + "'");
    return 1;
  }
  
  return op;
}


#7

Should we request it as a feature in future releases? I found that it could very helpful.


#8

@QMcKay are you…

  1. A time travelling ninja
  2. Always keen to solve a problem
  3. All round awesome and providing amazing support and insight to all of us

#9

I don’t sleep much and… Yes. Yes I am. :stuck_out_tongue_winking_eye:


#10

Hi @QMcKay Excellent script, I’m trying to use it with dlg.EditValue(“product name;;;ON”,""); so can be automatized for every product can you please enlight me


#11

This part of the Script:

// if we find the tagName, replace the tagValue
    if (jsonObj[t].TN == tagName) {
      dlg.ShowMessage("Found Tag '" + tagName + "' for Product '" + productName + "'.\rChanging value from '" + jsonObj[t].TV + "' to '" + tagValue + "'");
      jsonObj[t].TV = tagValue;
      foundTag = true;
    }

This line from above:

      jsonObj[t].TV = tagValue;

Add a line before it:

      tagValue = dlg.EditValue("product name;;;ON","");
      jsonObj[t].TV = tagValue;