views:

35

answers:

0

Hi guys,

I need to add about 100.000 records in my Magento Store. I have already the function that helps me to add a product but I need to improve it using the collections of products.

This is my custom function:

public function Products($data){

    try{
        foreach($data as $record){

            $product = Mage::getSingleton('catalog/product');
            $productId = $product->getIdBySku($record[3]);

            if($productId){
                $product->load($productId);
            }               

            // Build the product
            $product->setManufacturer($this->addManufacturers(utf8_encode($record[4])));
            $product->setSku($record[3]);
            $product->setAttributeSetId($this->attribute_set);# 9 is for default
            $product->setTypeId('simple');
            $product->setName(utf8_encode($record[5]));
            $product->setCategoryIds(array(1,2,3)); # some cat id's,
            $product->setWebsiteIDs(array(1)); # Website id, 1 is default
            $product->setDescription(utf8_encode($record[6]));
            $product->setShortDescription($this->shortText(utf8_encode($record[6]), 150));
            $product->setPrice($record[9]); # Set some price

            //Default Magento attribute
            $product->setWeight($record[12]);
            $product->setStatus( Mage_Catalog_Model_Product_Status::STATUS_ENABLED );
            $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

            $product->setTaxClassId(0); # default tax class

            // Manage stock
            $product->setStockData(array(
                'is_in_stock' => Mage_CatalogInventory_Model_Stock::STOCK_IN_STOCK, 
                          // ou Mage_CatalogInventory_Model_Stock::STOCK_OUT_OF_STOCK
                'qty' => 1000,
            ));

            $product->setCreatedAt(strtotime('now'));
            $product->setIsMassupdate(true);
            $product->setExcludeUrlRewrite(true);

            $product->save();
            $ID = is_numeric($productID) ? $productID : $product->getId();
            unset($product);
            unset($productId);
        }

    }catch(Exception $e){
        die($e->getMessage());
    }
}

How do I have to change this function using the collection?

Thanks