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
My Magento Import & Product Management Classes
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

The attached library folder holds the classes that I used to add and update products.

The attached init folder holds the scripts that I used to add products, simple or configurable.

The attached update folder hold the scripts that I used to update product attributes.

As you will notice, I connect to a mysql database (which is a mapped dump from our AS400 business system). All of my init/update calls pull from this mysql database. Many of you will use Dataflow, xml, SOAP, etc… and will have to customize this code.

For updating I simply query every simple product in magento, then update the desired attribute for that simple product by calling for that product in mysql. Same goes for configurable products,but all I update for configurable products is their existence in our business system.

For adding simple products, I query every active/show_online simple product in our business system and run it through the createSimpleProduct function.

For adding configurable products, I query every active/show_online configurable product in our business system and run it through the createConfigurableProduct function.

For updating attributes such as price, special_price, qty… the function looks to see if the business system and magento already match. If the two already match, then I dont’ have to run an update on that product and I can continue on to the next product. This saves an INCREDIBLY ENORMOUS amount of time when updating products.

For updating attributes such as Size or Color, this class is extremely helpful. For example, updateColor queries every simple product in magento and checks to see the color in magento matches the color in our business system. If it does, that product is skipped. If the magento color and the business system color doesn’t match it checks to see if that color exists in magento as an attribute value. If the color already exists in magento the function selects that color’s attribute value and the color is updated. If the color doesn’t already exist in magento the function creates that color, selects that color’s attribute value and the color is updated.

This attachment is by no means ready to plug and play, so please don’t ftp it to your magento installation and post why it doesn’ t magically connect to your business system and update magento accordingly. This class should be a reference tool for how to build update procedures that are custom designed for your business. How to get data from your business system is out of the scope of this thread.

This attachment was customized for this posting so it will need some editing. You will need to set your /app/Mage.php location in the class file Magento.php. I have constructed this in an incredible hurry, so I apologize if there are any spelling mistakes in the functions or classes. If you find a more efficient or dynamic way to code any of these functions please post your findings!

I assume this thread is going to flood with questions and comments. I will try my absolute hardest to respond asap, but I am extremely busy with my job so please bare with me.

File Attachments
blakew_magento.zip  (File Size: 7KB - Downloads: 1500)
 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

blakew - Thank you! This should help me get a proper import function working today.

Question: What version of Magento is this coded for?

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

This is written for 1.1.6, but I have been testing it with 1.2.0.3 today so far its working just fine. I will post if I have any complications with 1.2.0.3

 
Magento Community Magento Community
Magento Community
Magento Community
 
abijah
Sr. Member
 
Total Posts:  130
Joined:  2008-10-01
 

You’re awesome

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

In your code you assign data to the product before it gets written to the Magento database, like this:

$product->setType('Simple Product');
$product->setName($arg_data['name']);
$product->setDescription('[description]');
$product->setShortDescription('[short_description]');
$product->setPrice($arg_data['price']);
$product->setWeight($arg_data['weight']);
$product->setStatus(1);
$product->setTaxClassId(2);
$product->setCategoryIds($arg_data['category_ids']);
$product->setVisibility(4);
$product->setModel($arg_data['model']);

Where did you find a list of what attributes you can fire off a setSomething() call like that? I’ve been digging for hours, and learning a lot of other things, but still am not sure where this is. I know that the setXXXX are trapped by the __call function and sent to something that just stores data neatly in the object… I just don’t know what data is expected or usable when save() is called.

It seems that you are using only a small subset of the available data that can be stored for a given product, obviously only that which you need for your store. I assume the other options take on some default value?

And also, the setModel(...) that you have - is that a custom attribute for your products? It doesn’t ring a bell.

I just need a point in the right direction on these… Thanks for any advice you can give.

BTW, to everyone watching this, I’m trying to turn this into a semi-generic configurable product importer. It will still be specialized for my needs but a little less specialized than blakew’s code.. I’ll post what I come up with here. Maybe it’ll help someone else.

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

I was confused a while back where all the Set’s were coming from as well. They are a shortcut for $product->setData(’attribute’, ‘value’);

For example, you can do…

$product->setData('price'59.99);

or

$product->setPrice('59.99);

This list can be found in the admin menu: “Catalog-> Attributes-> Manage Attributes” or I supposed you might be able to set any attribute that exists in the attribute table, which you can view via phpmyadmin.

You are correct, I am using a small subset specific to my use for importing. The other attributes are either not required or assume a default value when received as null.

SetModel is a custom attribute that I use for the product’s model number. I tried to remove it for this example as I figured it would complicate things. I simply wasn’t able to fully remove it. If you can turn this into a more dynamic model, I would love to see the code. Thanks for posting your questions, I hope this helps you out. Let me know what else I can help you with.

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

if(!$this->existsInMagento($arg_data['sku']))
{
    $product
->setStockData($stockData);
    
$product->save();
}

Any particular reason you dont setStockData until after you check if the product already exists?

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

To be honest, I blazed through this project with lightening speed in an attempt to get a working solution out on the table asap. I assume I should put the entire product creation in the “try” so there is no overhead if the product already exists. That is a good find and should definitely increase my product import.

I want to note that when I import new products, I run all the products that already exist in magento through this function. If it already exists in magento, it is ignored, if it doesn’t exist in magento, it is added.

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

In this snip of code from createConfigurableProduct:

$attribute_size_id      $model->getIdByCode('catalog_product','size');
$attribute_size         $product->getResource()->getAttribute($attribute_size_id);

$attribute_size is always false, not what it should be, unless I change the $attribute_size_id on the second line to be an attribute code instead of id, like ‘size’ in the line above. Then it works.

This code works for you? This is straight from your files. Maybe I’m messing it up some other way who knows…

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

sorry I forgot to mention that size is a custom attribute that I added myself.

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

I need to mention that I have come up with a much faster way to process each product when updating. I haven’t tested it more than just briefly, but it makes sense.

I was reading an article on activecodeline, Branko’s blog site. I am not sure if any of you have been to this site, but Branko is seriously amazing. He is leaps and bounds ahead of me with manipulating magento, and I learn so much from him! I wouldn’t be as far with magento as I am today if it wasn’t for his site…

http://activecodeline.com/custom-category-menu-navigation-in-magento/comment-page-1/

From his guide, I am going to re write my updates to do the following…

<?php

require_once("/var/www/mage/app/Mage.php");
Mage::app();

$productModel Mage::getModel('catalog/product');
$products $productModel->getCollection()->getAllIds();

foreach(
$products as $productId)
{
    $product 
$productModel->load($productId);
    
$product->setPrice(59.99);

    
$product->save();
}

With my old method of updating, I could update product categoryId arrays about 1 every 2 seconds. After this modification, I can do about 10 - 20 a second.

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

Oh that makes a ton of sense for the updates! Just load it all and change the one datum… saves a ton of code execution.

I just finished my hack of your hack: http://www.magentocommerce.com/boards/viewthread/30394/

THANK YOU for getting me started in the right direction! Now I sleep.

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

wow great work! I know how those long nights are… get some sleep!

 
Magento Community Magento Community
Magento Community
Magento Community
 
michael_g08
Sr. Member
 
Avatar
Total Posts:  108
Joined:  2009-03-06
Ukraine
 

To blakew:
Hi! I’m new in magento, and i need a help with Your script.
I have a table ‘products’ in MySQL with fields:
- sku
- manufacturer
- name
- description
-short_description
- price
- qty

I just want to import products from this table into magento as a simple products.

Can You explain me how?

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

Are you familiar with Object Oriented Programming?

 
Magento Community Magento Community
Magento Community
Magento Community
 
michael_g08
Sr. Member
 
Avatar
Total Posts:  108
Joined:  2009-03-06
Ukraine
 

Yes. But quite new in magento.

Trying to study both magento and php in parralel. Have no choice.

But I’m quite well in Ruby and Delphi.... smile

Thank You in advance.

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