Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 3
Product Import (OR the simplest batch)
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

Hi there, I need a simple batch/task to import product by a cron. I noticed the usefull method “importFromTextArray” from the “catalog/product” model.
Here is my snippet:

<?php
define
('MAGENTO'realpath(dirname(__FILE__) . '/../../magento'));
ini_set('memory_limit''32M');

require_once 
MAGENTO '/app/Mage.php';

Mage::app();

$row = array (
    
'sku'                           => 'test-sku-123',
    
'store'                         => 0,
    
'attribute_set'                 => 'Default',
    
'type'                          => 'Simple Product',
    
'categories'                    => '12,25',
    
'name'                          => 'My Uber Product',
    
'description'                   => 'Here is the description of this product.',
    
'price'                         => 33.95,
    
'short_description'             => 'Here is the description...',
    
'weight'                        => 0,
    
'status'                        => 'Enabled',
    
'tax_class_id'                  => 'Taxable Goods',
    
'qty'                           => 100,     // do not work :(
    
'use_config_min_qty'            => 1,       // do not work :(
    
'use_config_backorders'         => '1',     // do not work :(
    
'use_config_min_sale_qty'       => true,    // do not work :(
    
'use_config_max_sale_qty'       => 1,       // do not work :(
    
'use_config_notify_stock_qty'   => '1',     // do not work :(
    // ...
);

$line = array (
    
'i'     => 1,
    
'row'   => $row
);

$product Mage::getModel('catalog/product');
$product->importFromTextArray($line);
$product->save();

?>

This code snippet works almost but not fully, some attributes do not work like “qty” for example. Am i doing wrong something or is it some kind of bug?

Is there a simple way to go over it? Any advices, code samples, ... would be nice smile

Thank you everyone…

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

"qty” is not stored with the product and is stored in a different table cataloginventory_stock_item’. The importFroTextArray is useful for importing product attributes alone. For code to import the stock refer to “$stockItem->save();” in Mage_Catalog_Model_Convert_Adapter_Product.saveRow() method

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

BTW would you be able to share the steps you do to carry this process out as a cron ...would help me a great deal smile ,..thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

Ok thanx srinigenie, I’ll keep you in touch smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
cibernoid
Sr. Member
 
Avatar
Total Posts:  208
Joined:  2008-02-12
 

I would be interested too smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

OK now stock is working fine...(i guess?)

<?php
define
('MAGENTO'realpath(dirname(__FILE__) . '/../../magento'));
ini_set('memory_limit''32M');

require_once 
MAGENTO '/app/Mage.php';

Mage::app();

$row = array (
    
'sku'                           => 'test-sku-1234',
    
'store'                         => 0,
    
'attribute_set'                 => 'Default',
    
'type'                          => 'Simple Product',
    
'categories'                    => '12,25',
    
'name'                          => 'My Uber Product',
    
'description'                   => 'Here is the description of this product.',
    
'price'                         => 33.95,
    
'short_description'             => 'Here is the description...',
    
'weight'                        => 0,
    
'status'                        => 'Enabled',
    
'tax_class_id'                  => 'Taxable Goods',
    
// ...
    
'thumbnail'                     => 'c/h/charger.jpg',   // doesn't work if anyone has a clue :)
);

$line = array (
    
'i'     => 1,
    
'row'   => $row
);

$product Mage::getModel('catalog/product');
$product->importFromTextArray($line);
$product->save();

$stockItem Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());

if (! 
$stockItem->getId()) {
    $stockItem
->setProductId($product->getId())->setStockId(1);
}

$stockItem
->setData('qty'100);
$stockItem->setData('is_in_stock'1);

$stockItem->save();

But now I have to make sure the thumbnail is working… I shoulfd probably use some kind of media stuff object, does anyone has a clue wink

Thx

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

I finally made it!

Now I also can import the picture corresponding to this product… I thought I needed another object like for the stock item, but the tricky part is in fact that I need to do a fake uplad (in fact I just copy this picture in a temp folder...)

Here is the import code for a simple product…

<?php
define
('MAGENTO'realpath(dirname(__FILE__) . '/../../magento'));
ini_set('memory_limit''32M');

require_once 
MAGENTO '/app/Mage.php';

Mage::app();

$source         '/var/www/MagentoTest/magento/media/catalog/product/c/h/charger.jpg';
$destination    '/var/www/MagentoTest/magento/media/tmp/catalog/product/c/h/charger.jpg';
copy($source$destination);

$row = array (
    
'sku'                           => 'test-sku-1234',
    
'store'                         => 0,
    
'attribute_set'                 => 'Default',
    
'type'                          => 'Simple Product',
    
'categories'                    => '12,25',
    
'name'                          => 'My Uber Product',
    
'description'                   => 'Here is the description of this product.',
    
'price'                         => 33.95,
    
'short_description'             => 'Here is the description...',
    
'weight'                        => 0,
    
'status'                        => 'Enabled',
    
'tax_class_id'                  => 'Taxable Goods',
    
'media_gallery'                 => array (
        
'images' => array (
            array (
                
'url'       => "http://localhost/MagentoTest/magento/media/tmp/catalog/product/c/h/charger.jpg",
                
'file'      => "/c/h/charger.jpg",
                
'label'     => '',
                
'position'  => 1,
                
'disabled'  => 0,
                
'removed'   => 0
            
),
        ),
        
'values' => array (
            
'thumbnail'     => 'no_selection',
            
'small_image'   => null,
            
'image'         => 'no_selection'
        
),
    ),
);

$line = array (
    
'i'     => 1,
    
'row'   => $row
);

$product Mage::getModel('catalog/product');
$product->importFromTextArray($line);
$product->save();

$stockItem Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());

if (! 
$stockItem->getId()) {
    $stockItem
->setProductId($product->getId())->setStockId(1);
}

$stockItem
->setData('qty'100);
$stockItem->setData('is_in_stock'1);

$stockItem->save();

I hope this can help some people like me, if you find a better/cleanner way just leave your advices under this post… I also wish this could be part of a wiki article but I don’t have much time to post this…

Anyway keep enjoying Magento smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
cornernote
Jr. Member
 
Total Posts:  16
Joined:  2008-03-11
 

this is fantastic!  can we get this on the wiki so that it’s easier to find ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
alanin
Member
 
Total Posts:  67
Joined:  2008-02-25
Nordhausen, Germany
 

HI there,

thanks for that script - kinda works for me

but i have to open and save all the added products, i looked into the database and i think there are some attributes that have to be set aswell

like meta title, meta keyword, meta description, url path, custom design and custom layout update (at least that attribs are set when saving the product via the backend)

seems that there are more tables touched, like the core_url_rewrite and so on…

FOUND IT OUT in catalog_product_website must be an entry with product_id and website_id - then the newly added product is shown in the shop…

$product = Mage::getModel(’catalog/product’);
$product->importFromTextArray($line);
$product->setWebsiteIds(array($storeId));
$product->save();

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 
alanin - 28 March 2008 01:23 AM

$product = Mage::getModel(’catalog/product’);
$product->importFromTextArray($line);
$product->setWebsiteIds(array($storeId));
$product->save();

Ok this is great, I think that if you forget the “websiteid”, the default is selected… For me the last missing(not working ?) feature is that I cannot tell Magento about the maedi_gallery and the values (this picture is the thumbnail, the other one is the base picture… etc).

If anyone has any idea it could be great…

 
Magento Community Magento Community
Magento Community
Magento Community
 
senders
Member
 
Avatar
Total Posts:  49
Joined:  2008-02-26
Germany, Mannheim
 

Hi Brikou Carré,

I used your script example to import my products.
It is working fine!

I am importing 3 pictures for each product. Thumbnail, small_image and image.
This is also working fine.
But how can I say, which picture I have imported is the thumbnail, which one is the small_image and which one is the image?

I do not understand the part

‘values’ => array (
‘thumbnail’ => ‘no_selection’,
‘small_image’ => null,
‘image’ => ‘no_selection’
),

Can anybody help me?

Or where can I find more informations about that?

I searched in the source codes but I didn’t found anything about this.

Please help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

The part you’ve just mentionned is not really usefull, first I thought but this is not in fact… You can just delete this part.

But with the 1.0 release, there is a new easy method to import picture corresponding to a product; I’ll try to do it this way soon…

 
Magento Community Magento Community
Magento Community
Magento Community
 
senders
Member
 
Avatar
Total Posts:  49
Joined:  2008-02-26
Germany, Mannheim
 

ooh ok. There is a new function for that in 1.0?
Can you giv me a hint?

or do you know how to do the import i descriped with the old function?
Everything is working. I only have to assign which picture is thumbnail, which one is small_image and so on.

Kind regards,

Sebastian

 
Magento Community Magento Community
Magento Community
Magento Community
 
senders
Member
 
Avatar
Total Posts:  49
Joined:  2008-02-26
Germany, Mannheim
 

The same problem seems to be with the data flow engine.

After importing a product with thumbnail small_image and base image, they aren’t assigned to any image type (thumbnail, small image, base image).

Anybody an idea?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

if Fact i found that you have to assigne attributes to the row…

$row = array (
    
// ...
    
'thumbnail'   => "/c/h/charger_1.jpg",
    
'small_image' => "/c/h/charger_2.jpg",
    
'image'       => "/c/h/charger_3.jpg",
    
// ... other attributes

It is not the cleaner way I guess because you have to know the specific path…

 
Magento Community Magento Community
Magento Community
Magento Community
 
senders
Member
 
Avatar
Total Posts:  49
Joined:  2008-02-26
Germany, Mannheim
 

Hi Brikou Carré,

thanks a lot for your fast answer and help.

It working fine for me.

Question is why it doesn’t work in the data flow engine? perhaps a bug?

A another short question.

Do you also know, how to import a manufacturer by the same way?


$row = array (
// ....
‘manufacturer’ => “ABC Publisher”
// ... other attributes
[/quotes]

This doesn’t work!
“Invalid attribute option specified for attribute attribute manufacturer”

Any ideas?

Kind regards,

Sebastian

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 3