<?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>Agence de Développement Informatique du Nord &#187; options</title>
	<atom:link href="http://blog.adin.pro/tag/options/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adin.pro</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Thu, 26 Dec 2019 08:54:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Magento product options create and delete</title>
		<link>http://blog.adin.pro/2015-06-05/magento-product-options-create-and-delete/</link>
		<comments>http://blog.adin.pro/2015-06-05/magento-product-options-create-and-delete/#comments</comments>
		<pubDate>Fri, 05 Jun 2015 13:14:06 +0000</pubDate>
		<dc:creator><![CDATA[blogadmin]]></dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[options]]></category>

		<guid isPermaLink="false">http://blog.adin.pro/?p=482</guid>
		<description><![CDATA[<p>Here, some of my code to manipulate Magento product options. Creation You first need an array with the options you want to save Then add them to your product, I have seen a lot of different way to do it &#8230; <a href="http://blog.adin.pro/2015-06-05/magento-product-options-create-and-delete/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2015-06-05/magento-product-options-create-and-delete/">Magento product options create and delete</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Here, some of my code to manipulate Magento product options.</p>
<p>Creation</p>
<p>You first need an array with the options you want to save</p>
<pre class="brush: php; title: ; notranslate">
$values = array();
foreach($options as $option)
{
    $values[] = array(
        'title' =&gt; $option['name'],
        'price' =&gt; $option['price'],
        'price_type' =&gt; 'fixed',
        'sku' =&gt; $option['sku'],
        'sort_order' =&gt; $x,
        'is_delete' =&gt; '0',
        );
    $x++;
}
$newOption = array(
            'title' =&gt; &quot;Public&quot;,
            'type' =&gt; 'drop_down',
            'is_require' =&gt; 1,
            'sort_order' =&gt; 20,
            'is_delete' =&gt; '',
            'previous_type' =&gt; '',
            'previous_group'    =&gt; '',
            'values' =&gt;$values,
        );
</pre>
<p>Then add them to your product, I have seen a lot of different way to do it </p>
<pre class="brush: php; title: ; notranslate">
$product-&gt;setCanSaveCustomOptions(true);
$product-&gt;getOptionInstance()-&gt;addOption($newOption);
//or
$product-&gt;getOptionInstance()-&gt;setOptions($newOption);

$product-&gt;getOptionInstance()-&gt;saveOptions();

$product-&gt;setHasOptions(1);
$product-&gt;setRequiredOptions(1);

$product-&gt;save();
</pre>
<p>or via OptionInstance</p>
<pre class="brush: php; title: ; notranslate">
$optionInstance = $product-&gt;getOptionInstance();
$optionInstance-&gt;setOptions($newOption);
$optionInstance-&gt;setProduct($product);
$optionInstance-&gt;-&gt;saveOptions();
</pre>
<p>But it doesn&#8217;t work, I finally use this </p>
<pre class="brush: php; title: ; notranslate">
$product-&gt;setProductOptions(array($newOption));
$product-&gt;setCanSaveCustomOptions(true);
$product-&gt;save()
</pre>
<p>To delete product options, there is several way too</p>
<pre class="brush: php; title: ; notranslate">
$oldOptions = $product-&gt;getOptionInstance()-&gt;getOptions();
foreach ($oldOptions as $key =&gt; $option){
       $oldOptions[$key]['is_delete'] = 1;
}
$product-&gt;getOptionInstance()-&gt;setOptions($oldOptions);
$product-&gt;getOptionInstance()-&gt;saveOptions();

//or 
$optionInstance = $product-&gt;getOptionInstance()-&gt;unsetOptions();
</pre>
<p>But it doesn&#8217;t work too, so I use :</p>
<pre class="brush: php; title: ; notranslate">
$options = Mage::getModel('catalog/product_option')-&gt;getCollection()-&gt;addFieldToFilter(&quot;product_id&quot;, $product-&gt;getId());
foreach($options as $option)
{
   $option-&gt;delete();
}
</pre>
<p>Some more informations:<br />
To see options on admin, your product entity (table catalog_product_entity) should have attribute (column) has_options set to 1.<br />
Options are saved in tables : catalog_product_option, catalog_product_option_price, catalog_product_option_title, catalog_product_option_type_price, catalog_product_option_type_title and catalog_product_option_value.</p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2015-06-05/magento-product-options-create-and-delete/">Magento product options create and delete</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adin.pro/2015-06-05/magento-product-options-create-and-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
