3

I've to import some products from a txt file where, for every row, there's product sku, product name, product description, short description, price and category id. The products are imported but price and quantity aren't set and the products aren't assigned to categories. The code I use is the following:

include_once('C:/xampp/htdocs/magento/app/Mage.php');

Mage::app('admin');

$rootMagento = Mage::getBaseDir();
$files = scandir($rootMagento);
$count = sizeof($files);
for ($i =0; $i< $count; $i++) {
    if ($files[$i] == 'productData.txt') {   //i_12345678.txt
        //$fileAperto = fopen($files[$i], "r");
        $righe = file('productData.txt');
        $countRighe = sizeof($righe);
        for ($ii = 0; $ii < $countRighe; $ii++) {
            list($sku, $name, $description, $shortDescription, $price, $categoryId) = split("[';']", $righe[$ii]);
            if ($sku == '' || $name == '' || $description == '' 
                || $shortDescription == '' ||  $price == '') {
                Mage::log('esportazione non corretta: manca uno dei dati');
            } 
            $product_model = Mage::getModel('catalog/product');
            $productId = $product_model->getIdBySku($sku);
            if (!$productId) {
                //inserimento
                /*
                $productData = array(
                    'sku' => $sku,
                    'name' => $name,
                    'description' => $description,
                    'short_description' => $shortDescription,
                    'status' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED,
                    'visibility' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                    'attribute_set_id' => 10
                );
                */
                $product_model->setSku($sku);
                $product_model->setName($name);
                $product_model->setDescription($description);
                $product_model->setShortDescription($shortDescription);
                $product_model->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
                $product_model->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
                $product_model->setAttributeSetId(10);
                $product_model->setPrice($price);
                $product_model->setCategoryId($categoryId);
                $qty = 1;
                try {
                    $productId = $product_model->save()->getId();
                    $product = $product_model->load($productId);
                    $product->setQty($qty);
                    $product->setIsInStock(1); //->isInStock(1)
                    $product->save();
                } catch(Exception $e) {
                    Mage::log($e->getMessage());
                }
            } else {
                //aggiornamento
                $product = $product_model->load($productId);
                $productData = array (
                    'sku' => $sku,
                    'name' => $name,
                    'description' => $description,
                    'short_description' => $shortDescription,
                    'status' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED ,
                    'visibility' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                    'attribute_set_id' => 10
                );
                $product->setData($productData); // + codice x stock
                $qty = 1;
                try {
                    $productId = $product->save()->getId();
                    $product = $product_model->load($productId);
                    $product->setQty($qty);
                    $product->setIsInStock(1); //->isInStock(1)
                    $product->save();
                } catch(Exception $e) {
                    Mage::log($e->getMessage());
                }
            }
        }
        //fclose($fileAperto);
    } 
}

Why I don't try to set price, quantity and category also? Can you help me, please?

Thank you very much!!

1 Answer 1

3

I dont know But you have to use

$product_model->setCategoryIds($categoryId)

instead of

$product:model->setCategoryId($categoryId);

Please try and let me know.

9
  • I've this error: 'Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Invalid category IDs.' in C:\xampp\htdocs\magento\app\Mage.php:595 Stack trace: #0 C:\xampp\htdocs\magento\app\code\core\Mage\Catalog\Model\Product.php(357): Mage::throwException('Invalid categor...') #1 C:\xampp\htdocs\magento\prova3.php(43): Mage_Catalog_Model_Product->setCategoryIds(NULL) #2 {main} thrown in C:\xampp\htdocs\magento\app\Mage.php on line 595'
    – Federica
    Sep 7, 2016 at 9:52
  • Category Ids should be in array Sep 7, 2016 at 9:53
  • product is not yet assigned to category..
    – Federica
    Sep 7, 2016 at 10:03
  • Provide me updated code what you try Sep 7, 2016 at 10:08
  • $categoriesIds[] = $categoryId; $product_model->setCategoryIds($categoriesIds);
    – Federica
    Sep 7, 2016 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.