Keeping a dropdown attribute’s options up to date in the Magento 2 admin means a lot of clicking: deleting the values you no longer sell and adding new ones, one row at a time. This script does both in one go. You give it the list of values the attribute should end up with, and it makes the Manage Options table match that list.

This is an update to my previous script which was used to add custom product attribute values using the backend –
Add values to product attribute

Read the previous^ part to get an introduction to how it works. You will have to use the browser console (ctrl + shift + j in Google Chrome, Cmd + Option + J on a Mac).

First of all, initialize the array mimim with the final set of values you want to see in the backend. The script then automatically removes the extra options first (values which are not in mimim but are present in the backend), then it adds the remaining values from mimim (which are in mimim but not in the backend). Values that are in both are left alone.

Steps

  1. In the admin, go to Stores > Attributes > Product and open the attribute. It has to be a Dropdown or Multiple Select attribute, because the script works on the Manage Options table on the Properties tab.
  2. Wait until the whole options list has loaded. On an attribute with hundreds of options, Magento fills the table in batches.
  3. Open the console, edit the mimim array, paste the script and press Enter. An alert tells you how many options were removed.
  4. Check the table, then click Save Attribute. Nothing changes in the database until you save.
$jq=new jQuery.noConflict();
var mimim=["Abalone",
"Titanium Druzy",
"Tourmaline",
"Turquoise",
"Yellow Zircon",
"Light Blue Druzy"];
var trans=[];var o=0;
$jq('#manage-options-panel tbody tr td:nth-child(3) input').each(
  function(a,b){
	//if(a>10)return false;
	//alert(b.value);
	trans.push(b.value);
	if($jq.inArray(b.value,mimim)==-1){ o++;$jq('#delete_button_'+b.name.slice(14,-4)).click(); trans.pop();}
});
alert(o+" items removed");
$jq.each(mimim,function(a,b){
//console.log($jq('#manage-options-panel tbody tr td:nth-child(3) input').val());
if($jq.inArray(b,trans)==-1){
$jq('#add_new_option_button').click();
$jq('#manage-options-panel tbody tr:last-child td:nth-child(3) input').val(b);
}

});

The same script is also in this gist:

How it works

  • $jq=new jQuery.noConflict(); stores jQuery in $jq, so the script doesn’t depend on what $ means on the admin page. The new isn’t needed, but it does no harm.
  • mimim is the final list of values. trans collects the existing values that will stay, and o counts the deleted ones.
  • The first loop goes through the Admin value of every option row. td:nth-child(3) is the third column of the table, after the drag handle and the Is Default column. Each value is added to trans. If it isn’t in mimim ($jq.inArray(...)==-1), the script clicks that row’s Delete button and takes the value off trans again.
  • Finding the Delete button. Each Admin input is named like option[value][123][0], where 123 is the option’s ID. b.name.slice(14,-4) cuts off the first 14 characters (option[value][) and the last 4 (][0]), which leaves the ID, so the button is #delete_button_123.
  • alert(o+" items removed") shows how many options were marked for deletion.
  • The second loop goes through mimim. For every value that isn’t in trans, it clicks Add Option (#add_new_option_button) and types the value into the Admin field of the new row, which is always the last row (tr:last-child).
  • The commented-out lines (//if(a>10)..., //alert(...), //console.log(...)) are leftovers from testing and don’t run.

Things to watch out for

  • Anything not in mimim gets deleted, including options that products are using. Those products lose that value once you save. If the number in the alert is higher than you expected, reload the page without saving.
  • The comparison is exact. Capitals and spaces count, so Blue and blue (or Blue with a trailing space) are different values. A typo in mimim deletes the existing option and adds a new one with the typo.
  • Only the Admin value is filled in. New options have empty store view labels, so every store view shows the Admin value.
  • Swatch attributes won’t work. Visual and text swatch attributes use a different options table, so the selectors don’t match anything.
  • Written for Magento 2.1 in 2017. If the admin markup is different in your version, the selectors won’t match and the script won’t do anything. Check the table with the browser’s inspector and adjust them.