{"id":1233,"date":"2015-12-17T03:48:09","date_gmt":"2015-12-16T21:48:09","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1233"},"modified":"2021-10-23T13:55:34","modified_gmt":"2021-10-23T18:55:34","slug":"magento-manually-set-product-attribute-sort-order-in-the-database","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/magento-manually-set-product-attribute-sort-order-in-the-database\/","title":{"rendered":"Magento 1: Manually Set Product Attribute Sort Order In The Database"},"content":{"rendered":"<p>I have a large number of color attribute names in Magento. &nbsp;I mean a LARGE number &#8211; several thousand. &nbsp;For this reason loading the color attribute page in the Magento admin gets to be a bit cumbersome and hard to manage. &nbsp;In addition, there are a few color attributes that have <em>null<\/em> values for one reason or another and thus the page throws errors when I attempt to save any changes in the Magento Admin for the color attribute.<\/p>\n\n\n\n<p>That said, there are at times changes need to be made. &nbsp;While this may sound like a &#8220;sloppy developer&#8221; comment, rather than sort out the issues with the&nbsp;<em>null<\/em> attributes, this is fairly simple to accomplish directly in the database.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/12\/Magento-Attribute-Sorting-via-Database.jpg\" rel=\"attachment wp-att-236\" data-lasso-id=\"558\" data-rel=\"lightbox-gallery-PH3bR0nL\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/12\/Magento-Attribute-Sorting-via-Database-500x500.jpg\" alt=\"How to sort Magento product attributes in the database\" class=\"wp-image-1236\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/12\/Magento-Attribute-Sorting-via-Database-500x500.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/12\/Magento-Attribute-Sorting-via-Database-150x150.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/12\/Magento-Attribute-Sorting-via-Database.jpg 600w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption>Sort product attributes in Magento through the database.<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Magento EAV Tables<\/h2>\n\n\n\n<p>There are two tables that are needed to accomplish this task.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Table Name<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Table Contents<\/strong><\/td><\/tr><tr><td>eav_attribute_option<\/td><td>\n<p>The attributes and their sort order.<\/p>\n<\/td><td>\n<p><strong>option_id<\/strong>: The attribute ID<\/p>\n<p><strong>attribute_id<\/strong>: ID of the attribute (example: color, gender, size, etc.)<\/p>\n<p><strong>sort_order<\/strong>: The sort order value for this attribute<\/p>\n<\/td><\/tr><tr><td>eav_attribute_option_value<\/td><td>\n<p>The text to be displayed for each attribute per store.<\/p>\n<\/td><td>\n<p><strong>value_id<\/strong>: ID of the value<\/p>\n<p><strong>option_id<\/strong>: The option id &#8211; directly correlates to the&nbsp;<em>option_id<\/em> in the&nbsp;<em>eav_attribute_option<\/em> table<\/p>\n<p><strong>store_id<\/strong>: Store ID this attribute value is associated to<\/p>\n<p><strong>value<\/strong>: The text value that is tied to this attribute option<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As you can see, these&nbsp;<strong>sort_order<\/strong> that we need to modify is in the&nbsp;<em>eav_attribute_option<\/em> table, but it&#8217;s tied directly to the ID of the attribute. &nbsp;If you know your attribute ID then this is a simple fix &#8211; just work directly within that table. &nbsp;But if you don&#8217;t know the attribute ID&#8217;s you are attempting to modify this appears difficult at first, but can be solved with a simple join query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Find the Attribute ID<\/h2>\n\n\n\n<p>The one thing you do need to know is the&nbsp;<strong>attribute_id<\/strong> that you are working with. &nbsp;If you don&#8217;t know it, it can be found in the Magento Admin via the URL for the attribute.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>In the Magento Admin choose: Catalog -&gt; Attributes -&gt; Manage Attributes<\/li><li>Select the attribute you are working with<\/li><li>Look at the resulting URL &#8211; it should be something like:<br>https:\/\/www.yoursite.com\/index.php\/admin\/catalog_product_attribute\/edit\/attribute_id\/<strong>50<\/strong>\/<\/li><li>The number at the end of the URL, 50 in this example, is the attribute ID you are looking for.<\/li><\/ol>\n\n\n\n<p>Alternatively, this can be found on the frontend of the website.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Right click on the dropdown or radio button that correlates to the attribute you are working with and choose&nbsp;<strong>Inspect Element<\/strong>. &nbsp;(Note: This should work in all modern browsers, but each implements this slightly different.)<\/li><li>In the window that opens with the HTML code of the page, you&#8217;ll see the HTML input for that attribute. &nbsp;The attribute ID is part of the name and ID for the input fields. &nbsp;Here is an example of what the code might look like &#8211; notice the number 50, which is the attribute ID for this example.<\/li><li>\n<pre><code>&lt;select name=\"super_attribute[50]\" id=\"attribute50\" class=\"required-entry super-attribute-select\"&gt;\n &lt;option value=\"123\"&gt;Red&lt;\/option&gt;\n &lt;option value=\"124\"&gt;Blue&lt;\/option&gt;\n&lt;\/select&gt;<\/code><\/pre>\n<\/li><\/ol>\n\n\n\n<p><strong>NOTE<\/strong>: If you use this option you could actually skip over the join query section and go right to the&nbsp;<strong>Change the Sort Order<\/strong> section below. &nbsp;Look for the&nbsp;<strong>shortcut<\/strong> option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Join Query To Get Attribute With Text<\/h2>\n\n\n\n<p>With the attribute ID we can now run a query in the database to make this more accessible.<\/p>\n\n\n\n<p>I&#8217;m listing several queries to accomplish this depending on your needs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">All Attributes With Attribute ID<\/h3>\n\n\n\n<p>Notice the attribute id of 50 is at the end of the where clause.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT *\nFROM eav_attribute_option_value\nJOIN eav_attribute_option\nON eav_attribute_option_value.option_id=eav_attribute_option.option_id\nWHERE eav_attribute_option.attribute_id=50<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Specific Attributes Based on Text Value<\/h3>\n\n\n\n<p>To narrow down the results of this query a bit more, we can list what attribute values we want returned. &nbsp;Looking at the example above, there are only two colors &#8211; Red and Blue. &nbsp;See how they are added at the end of the query below separated by a comma in quotes? &nbsp;That means we will just get those two returned in our results.<\/p>\n\n\n\n<p>Also notice the attribute id of 50 is at the end of the where clause.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT *\nFROM eav_attribute_option_value\nJOIN eav_attribute_option\nON eav_attribute_option_value.option_id=eav_attribute_option.option_id\nWHERE eav_attribute_option.attribute_id=50\nAND eav_attribute_option_value.value IN (\"Red\", \"Blue\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Change The Sort Order<\/h2>\n\n\n\n<p>Sadly from the join query above you won&#8217;t be able to update the database records. &nbsp;So first thing&#8217;s first &#8211; save those results in some way. &nbsp;Take a screenshot or export them to a spreadsheet, open a new query window for the next step or whatever you &nbsp;need to do to be able to reference this result again quickly.<\/p>\n\n\n\n<p>Now we need to run another query that will allow us to view the attributes we are interested in updating in the&nbsp;<em>eav_attribute_option<\/em> table so that we can set the sort order.<\/p>\n\n\n\n<p>From the result of the above copy out the column of&nbsp;<strong>option_id<\/strong>&#8216;s and convert them to a comma separated string. &nbsp;Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>123, 124<\/code><\/pre>\n\n\n\n<p><strong>SHORTCUT<\/strong>: If you used the step above to view your input box HTML code, you&#8217;ll notice these are the same ID&#8217;s for each of the drop down or radio button items. &nbsp;For that reason, you could have really skipped over the join query step and gone right to this step.<\/p>\n\n\n\n<p>We&#8217;ll now run one more query to get only these options. &nbsp;This one is simpler.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM eav_attribute_option WHERE option_id IN (123, 124)<\/code><\/pre>\n\n\n\n<p>Notice that you put the string of&nbsp;<strong>option_id<\/strong>&#8216;s inside the parenthesis at the end of the query.<\/p>\n\n\n\n<p>When you run this query you get the results of just the options you were trying to focus in on. &nbsp;Now you can update the sort order in your database and you should be all set to go.<\/p>\n\n\n\n<p>It may be worth using numbers that aren&#8217;t right at the top of the numeric list. &nbsp;For example, if you use 1, 2, 3 and then later on need to make a change and put another item above this one you&#8217;ll have to change all of the values. &nbsp;If you instead use something like 100, 110, 120 this gives you some wiggle room. &nbsp;If another color needs to go into position #2 you can use 105. &nbsp;Then another that should top the list, try 90. &nbsp;This saves some time in the long run.<\/p>","protected":false},"excerpt":{"rendered":"<p>I have a large number of color attribute names in Magento. &nbsp;I mean a LARGE number &#8211; several [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1236,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[46,5],"tags":[],"class_list":["post-1233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/12\/Magento-Attribute-Sorting-via-Database.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-jT","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":4,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":2034,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1233\/revisions\/2034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1236"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}