VPS business hosting starting at $29.95/24/7 premium technical support

How to manage Magento 2 product attribute values options using console

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

Read the previous^ part to get introduction about how it works. you will have to use browser console (ctrl + shift +j in Google chrome)

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

$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);
}

});

Author: Harshvardhan Malpani

PHP Developer based in New Delhi, India. Working as a freelance web developer providing server deployment, website development and maintenance services.

One thought on “How to manage Magento 2 product attribute values options using console”

Leave a Reply

Your email address will not be published. Required fields are marked *