Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a product from PHP - Magento
    text
    copied!<p>A friend of mine created a magento webshop recently. He wants to have a few thousand products in his store and asked the manufacturer to give him some kind of data he could simple import to magento. The manufacturer said that he does not own something like that or doesn't want to give it out (I'm not 100% sure about this one). But he was allowed to use all the product information on his site to complete his webshop. So I wrote a webcrawler who grabs all the information. After researching a bit, people told me not to insert all the information using SQL, but rather using a PHP script and the Magento API.</p> <p>I have some probelms with the following script. It should create a product to add into the Magento database. The insert values are testing values.</p> <pre><code>&lt;?php error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 1); require("app/Mage.php"); echo "Test \n"; Mage::init(); $product = Mage::getModel('catalog/product'); $product-&gt;setName('Peter Parker'); $product-&gt;setDescription('Peter Parker Description'); $stock_data=array( 'use_config_manage_stock' =&gt; 0, 'qty' =&gt; 0, 'min_qty' =&gt; 0, 'use_config_min_qty'=&gt;0, 'min_sale_qty' =&gt; 1, 'use_config_min_sale_qty'=&gt;1, 'max_sale_qty' =&gt; 9999, 'use_config_max_sale_qty'=&gt;1, 'is_qty_decimal' =&gt; 0, 'backorders' =&gt; 0, 'notify_stock_qty' =&gt; 0, 'is_in_stock' =&gt; 0 ); $product-&gt;setData('stock_data',$stock_data); $product-&gt;setTaxClassId(2); // default tax class $product-&gt;setAttributeSetId(9); //9 is for default $product-&gt;setWebsiteIds(array(1)); $product-&gt;setCategoryIds(array(9)); $product-&gt;setStatus(1);//1=Enabled; 2=Disabled; $product-&gt;setVisibility(4);//4 = catalog &amp;amp; search. $image_name = 'blabla.JPEG'; $url = 'http://www.rasch-tapeten.de/shop/media/catalog/product/cache/1/image/bf8bb7ab75fe41b467eed88aa79f7917/1/3/133806.JPEG'; $img = 'media/'.$image_name; echo $img."\n"; function save_image($inPath,$outPath){ //Download images from remote server $in= fopen($inPath, "rb"); $out= fopen($outPath, "wb"); while ($chunk = fread($in,8192)) { fwrite($out, $chunk, 8192); } fclose($in); fclose($out); } save_image($url, $img); try { $product-&gt;addImageToMediaGallery($img, array('image', 'small_image', 'thumbnail'), false, false); }catch(Mage_Core_Exception $e) { echo $e-&gt;getMessage(); } try { $product-&gt;save(); }catch(Exception $e) { echo $e-&gt;getMessage(); } ?&gt; </code></pre> <p>Well if i execute this one it throws the following exception:</p> <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DE) </code></pre> <p>Does anyone know what I did wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload