Magento: Adding new products programmatically -
Magento: Adding new products programmatically -
i trying add together products magento 1.5 programmatically. script cron job, regularly updating , adding products dictated xml file supplied accounts system.
i have problem in creating new products. relevant code segment script is:
$attributesetid = 4; //$newproduct = mage::getmodel('catalog/product'); $newproduct = new mage_catalog_model_product(); $newproduct->settypeid('simple'); $newproduct->setweight($product->unitweight); $newproduct->setvisibility(mage_catalog_model_product_visibility::visibility_both); $newproduct->setstatus(1); $newproduct->setsku($sku); $newproduct->settaxclassid(0); $newproduct->setwebsiteids(array(0)); $newproduct->setstoreids(array(1)); $newproduct->setstockdata(array( 'is_in_stock' => 1, 'qty' => $xmlproduct->qtyinstock, 'manage_stock' => 1 )); $newproduct->setattributesetid(4); $newproduct->setname($product->name); $newproduct->setcategoryids(array(3)); // array of categories relate $newproduct->setdescription($product->longdescription); $newproduct->setshortdescription($product->description); $newproduct->setprice($xmlproduct->saleprice); seek { if (is_array($errors = $newproduct->validate())) { $strerrors = array(); foreach($errors $code=>$error) { $strerrors[] = ($error === true)? mage::helper('catalog')->__('attribute "%s" invalid.', $code) : $error; } $this->_fault('data_invalid', implode("\n", $strerrors)); } $newproduct->save(); } grab (mage_core_exception $e) { $this->_fault('data_invalid', $e->getmessage()); }
the product 'half' created, script gives throwing next error:
php fatal error: uncaught exception 'pdoexception' message 'sqlstate[23000]: integrity constraint violation: 1452 cannot add together or update kid row: foreign key constraint fails (`littledickybird`.`catalog_category_product_index`, constraint `fk_catalog_category_prod_idx_category_entity` foreign key (`category_id`) references `catalog_category_entity` (`entity_id`) on )' in /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/zend/db/statement/pdo.php:228 stack trace: #0 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/zend/db/statement/pdo.php(228): pdostatement->execute(array) #1 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/zend/db/statement.php(300): zend_db_statement_pdo->_execute(array) #2 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/zend/db/adapter/abstract.php(479): zend_db_statement->execute(array) #3 /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/zend/db/adapter/pdo/abstract.php(238): zend_db_adapter_abstract->query('insert `ca...', array) #4 /home/default/littledic in /home/default/littledickybird.co.uk/user/htdocs/1.5/lib/zend/db/statement/pdo.php on line 234
can anyone, please, throw lite onto missing or doing wrong. may able tell tone, pretty desperate, help much appreciated.
thank you
amp, op, self-answered question.
quote:
cool, have found problem, line: $newproduct->setwebsiteids(array(0));
should be: $newproduct->setwebsiteids(array(1));
amazing how smallest thing can waste hours!
note: it's absolutely ok self-answer own question. please post real answer, not in question or comment. posting real reply helps maintain "unanswered" list more clear (avoids making other people wasting time).
magento magento-1.5
Comments
Post a Comment