<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Solution Log &#124; Antonio David - NVNCBL &#187; script</title>
	<atom:link href="http://www.nvncbl.com/tagged/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nvncbl.com</link>
	<description>A place to log my programming solutions for future reference</description>
	<lastBuildDate>Sun, 28 Aug 2011 01:20:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Magento &#8211; Set Product Dropdown and Multiselect Values Programmatically</title>
		<link>http://www.nvncbl.com/2010/03/magento-set-product-dropdown-and-multiselect-values-programmatically/</link>
		<comments>http://www.nvncbl.com/2010/03/magento-set-product-dropdown-and-multiselect-values-programmatically/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 01:02:17 +0000</pubDate>
		<dc:creator>Antonio</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[addData]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[automated]]></category>
		<category><![CDATA[dropdown]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[multiple]]></category>
		<category><![CDATA[multiselect]]></category>
		<category><![CDATA[programmatically]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[values]]></category>

		<guid isPermaLink="false">http://www.nvncbl.com/?p=513</guid>
		<description><![CDATA[Here&#8217;s how to programmatically, meaning through code, set a product&#8217;s attribute value when the attribute is of type Dropdown or Multiselect.  For dropdown, we&#8217;ll be interested in setting only a single value.  And obviously for multiselect, we&#8217;ll be interested in setting multiple values.
With Text attributes such as Name and Description, you can do something like:

$product-&#62;setName&#40; [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to programmatically, meaning through code, set a product&#8217;s attribute value when the attribute is of type Dropdown or Multiselect.  For dropdown, we&#8217;ll be interested in setting only a single value.  And obviously for multiselect, we&#8217;ll be interested in setting multiple values.</p>
<p>With Text attributes such as Name and Description, you can do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'My Sweet Shirt'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDescription</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'This shirt will make you look good, thus impressing girls.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Unfortunately, this won&#8217;t work for attributes whose values are predefined.  Instead of cursing Magento, think of it as a data-integrity/validation measure.</p>
<p>Anyway, assuming you already have a $product object, our overall process will be to:</p>
<ol>
<li>Load an attribute object for the attribute you want to work on</li>
<li>Load the collection of that attribute&#8217;s values</li>
<li>Make your choices (different for Dropdown and Multiselect)</li>
<li>Save the product</li>
</ol>
<p><strong>1. Load an attribute object for the attribute you want to work on</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$attribute</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'eav/entity_attribute'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$attribute</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadByCode</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'color'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>2. Load the collection of that attribute&#8217;s values</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$valuesCollection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getResourceModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'eav/entity_attribute_option_collection'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttributeFilter</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$attribute</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStoreFilter</span><span style="color: #009900;">&#40;</span> Mage_Core_Model_App<span style="color: #339933;">::</span><span style="color: #004000;">ADMIN_STORE_ID</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$valuesCollection</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>3. Make your choice &#8211; DROPDOWN</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setColor</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Blue'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// just do whatever you need to code 'Blue' instead of hard-coding it</span></pre></div></div>

<p><strong>3. Make your choices &#8211; MULTISELECT</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addData</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Blue'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">.</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Red'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">.</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Black'</span><span style="color: #009900;">&#93;</span>  <span style="color: #666666; font-style: italic;">// just putting together a comma-separated list of values</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>4. Save the product</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This builds off the solution posted by member &#8220;icvu&#8221; here: <a href="http://www.magentocommerce.com/boards/main.php/viewthread/69925/" class="external">http://www.magentocommerce.com/boards/main.php/viewthread/69925/</a> &#8212; specifically the addData() part.</p>
<p>Thanks, and have fun setting attribute values.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nvncbl.com/2010/03/magento-set-product-dropdown-and-multiselect-values-programmatically/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

