Mage: A Fast and Reliable Solution to Magento Data Import Woes

  • By admin
  • 3 min read
  • 0 Comment

Magento developed by Varien, is an open source ecommerce web solution. Basic version of Magento, Magento Community Edition, is free of cost with limited features; that was launched on March 31, 2008. For advanced features, however, numerous paid plugins, themes and add-ons are available to make this platform appear stunning. Magento platform is based on PHP and MySQL, provides on-line shop owners an adorable, fascinating, featured and winning platform to do their business.


While dealing with large product reserves data, Magento is actually very slow to store data as it uses very complicated, Entity Attribute Value (EAV) database structure. Developers used to wast a lot of their time in attempting to import/export the large product reserve’s data, attributes, images into the platform but didn’t get the desired output.

Goggling “Magento Product Import”, let you know about most general solutions like; .CSV (comma separated values) and SOAP (simple object access protocol) API import and export functionalities to accomplish this task.

Adopting .CSV solution for general purpose small data sets is quite fine as crafting of .CSV file for import is the easy part but for large data sets it is nothing more than waste of time as it is practically impossible to deal with such a huge amount of data by this method because the actual import process itself is the painful.

SOAP API is a step up from the .CSV method of importing product data because of its better object-oriented method for importing products but its functionality is not up to the mark as it is also slow.

To perk up this functionality of Magento, a fast and reliable method named Mage is used to manage the large product reserves data. This method is based on PHP coding.

Now a small tutorial to let you know that how Mage method works :
First create a simple PHP file containing the following code:

require_once(“/path/to/your/magento/installation/Mage.php”);
umask(0);
Mage::app();
$product = Mage::getModel(‘catalog/product’);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Then construct an array containing all data of the product that you want to import such as title, description, price etc. Each of your product must have its own unique identifier so that inserting and updating of product details in database can be done simply without any duplication.Now we are going to loop through each and every product in our array and check to see if it already exists in our database or not If it does not, we will perform a product insert.

foreach ( $products as $p ) {
$product_id = $product->getIdBySku($p[‘sku’]);
if ( $product_id ) {

// The product already exists in our database. Update the product.

$product->load($product_id);
$product->setUpdatedAt(strtotime(‘now’));
/*
At this point, you can define any product settings just as would
when inserting the product, as seen below.
*/
$product->save();

} else {

// The product does not already exist in our database. Insert the product.

$product->setCreatedAt(strtotime(‘now’));
$product->setSku($p[‘sku’]);
$product->setName($p[‘name’]);
$product->setDescription($p[‘description’]);
$product->setShortDescription($p[‘short_description’]);
$product->setPrice($p[‘price’]);
$product->setTypeId(‘simple’);

/*
This number may be different for you. You can look this up in your administration panel.
You are looking for the number of the “default attribute set.”
*/
$product->setAttributeSetId(‘4’);

if ( $cat_id != ” ) {
/*
Again, you will need to look this up within your administration panel. Each product category you create has a unique ID,
and you will want to know what category you want to insert each product into.
*/
$product->setCategoryIds($cat_id);
}

$product->setWeight($p[‘weight’]);
$product->setTaxClassId(‘2’);
$product->setVisibility(‘4’);
$product->setStatus(‘1’);
$product->setCost($p[‘cost’]);
$product->setKeywords($p[‘keywords’]);
$product->setMetaDescription($p[‘meta_description’]);
$product->setMetaKeywords($p[‘keywords’]);

// Assign product to the default website.
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$stockData = $product->getStockData();
$stockData[‘qty’] = $p[‘available_quantity’];
$stockData[‘min_sale_qty’] = 1;
$stockData[‘is_in_stock’] = 1;
$stockData[‘manage_stock’] = 0;
$stockData[‘use_config_manage_stock’] = 1;
$product->setStockData($stockData);

$product->setMediaGallery(
array (
‘images’ => array (
array (
‘url’ => “http://www.mightypromos.com/media/catalog/product/your-product-image.jpg”,
‘file’ => “/your-product-image.jpg”,
‘label’ => $p[‘name’],
‘position’=> 1,
‘disabled’ => 0,
‘removed’ => 0
),
),
)
);
$product->setThumbnail(“your-product-image.jpg”);
$product->setSmallImage(“your-product-image.jpg”);
$product->setImage(“your-product-image.jpg”);
$product->setCanSaveCustomOptions(true);
$product->setProductOptions($options);
$product->save();

}

}

If you were seeking assistance related to Magento development, hire Magento developers here. We are always up for help. Give us a line!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Book Free Consultation

Our consultants will respond back within 8 business hours or less.

Error Message
Error Message
Error Message
Error Message
Error Message

=