WP E-Commerce add_to_cart shortcode with Quantity Box

A quick little code snippet that extends the in-built add_to_cart shortcode and adds a Quantity Box with it. Feel free to modify this code all you like.
Tested on WordPress 3.5.x and WP-E-Commerce 3.8.x

The original functions can be found in :
/wp-content/plugins/wp-e-commerce/wpsc-includes/display.functions.php (approx line 198)
/wp-content/plugins/wp-e-commerce/wpsc-includes/theme.functions.php (approx line 1196)

Just copy and paste the code into your functions.php file in your theme.
Then you can use the shortcode [cdl_add_to_cart=99] in your post or page, remember to replace the 99 with your own product ID.

[sourcecode language=”php”]
/*——————————————————————————————-*/
/* cdl_add cart button function used for php template tags and shortcodes
/*——————————————————————————————-*/

function cdl_add_to_cart_button( $product_id, $return = false ) {
global $wpdb,$wpsc_variations;
$output = ”;
if ( $product_id > 0 ) {
// grab the variation form fields here
$wpsc_variations = new wpsc_variations( $product_id );
if ( $return )
ob_start();
?>
<div class=’wpsc-add-to-cart-button’>
<form class=’wpsc-add-to-cart-button-form’ id=’product_<?php echo esc_attr( $product_id ) ?>’ action=” method=’post’>
<?php do_action( ‘wpsc_add_to_cart_button_form_begin’ ); ?>
<div class=’wpsc_variation_forms’>
<?php while ( wpsc_have_variation_groups() ) : wpsc_the_variation_group(); ?>
<p>
<select class=’wpsc_select_variation’ name=’variation[<?php echo wpsc_vargrp_id(); ?>]’ id='<?php echo wpsc_vargrp_form_id(); ?>’>
<?php while ( wpsc_have_variations() ): wpsc_the_variation(); ?>
<option value='<?php echo wpsc_the_variation_id(); ?>’ <?php echo wpsc_the_variation_out_of_stock(); ?>><?php echo esc_html( wpsc_the_variation_name() ); ?></option>
<?php endwhile; ?>
</select>
</p>
<?php endwhile; ?>
</div>

<legend>Quantity</legend>
<div class="wpsc_quantity_update">
<input id="wpsc_quantity_update_<?php echo esc_attr( $product_id ) ?>" name="wpsc_quantity_update" size="2" value="1" type="text">
<input name="key" value="-1" type="hidden">
<input name="wpsc_update_quantity" value="true" type="hidden">
</div><!–close wpsc_quantity_update–>

<input type=’hidden’ name=’wpsc_ajax_action’ value=’add_to_cart’ />
<input type=’hidden’ name=’product_id’ value='<?php echo $product_id; ?>’ />
<input type=’submit’ id=’product_<?php echo $product_id; ?>_submit_button’ class=’wpsc_buy_button’ name=’Buy’ value='<?php echo __( ‘Add To Cart’, ‘wpsc’ ); ?>’ />
<?php do_action( ‘wpsc_add_to_cart_button_form_end’ ); ?>
</form>
</div>
<?php

if ( $return )
return ob_get_clean();
}
}

function cdl_add_to_cart_shortcode( $content = ” ) {
static $fancy_notification_output = false;
if ( preg_match_all( "/\[cdl_add_to_cart=([\d]+)\]/", $content, $matches ) ) {
foreach ( $matches[1] as $key => $product_id ) {
$original_string = $matches[0][$key];
$output = cdl_add_to_cart_button( $product_id, true );
$content = str_replace( $original_string, $output, $content );
}

if ( ! $fancy_notification_output ) {
$content .= wpsc_fancy_notifications( true );
$fancy_notification_output = true;
}
}
return $content;
}
add_filter( ‘the_content’, ‘cdl_add_to_cart_shortcode’, 12 );
[/sourcecode]


Export products from Magento with multiple images

How to export additional images for products

Many people have described how to import additional images in Magento store. The question often arises as to how export additional images for products in Magento store.

Magento’s architecture is module-based, which means that you can easily modify existing and add new functions by creating your own modules.

Used example
For this howto, I will create a module that overrides the “unparse()” function, which returns the csv Data for a export products. This module has been written for Magento 1.4 but should work on 1.3 as well. This will NOT work on version 1.7.x. It was written for 1.4 and I have yet to write an updated version. Sorry.

1. Create directories

First of all, you need to create some directory structure that will hold your module files. Go to /app/code/local and create a directory representing your’s or your company’s name. All your modules will go into that directory. I will use this for the example:
/app/code/local/CDL.
Now we need to create the module directory. The module directory represents the name your module gets. I will use:
/app/code/local/CDL/Export in this example.
Every module needs a configuration file. This file is located under the etc/ subdirectory, so you need to create that directory as well. In my example it is:
/app/code/local/CDL/Export/etc.

The last needed directory is the Model/ subdirectory. I will explain what this is for later.

2. Config File

Now that we have all the directories we need, we can create a new file named config.xml inside the etc/ directory. This file will be used by the Magento module system to know what your module does and where the needed files are located. Here is the config.xml that we will need for our example:
[sourcecode language=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<CDL_Export>
<version>0.1.0</version>
</CDL_Export>
</modules>
<global>
<models>
<dataflow>
<rewrite>
<convert_parser_csv>CDL_Export_Model_Export</convert_parser_csv>
</rewrite>
</dataflow>
</models>
</global>
</config>
[/sourcecode]

As you can see, we will override the “Convert/Parser/Csv.php” model inside the “Dataflow” Module. Note that the underscores in represent the directory structure the core model is in. This is the same with CDL_Export_Model_Export, which points to CDL/Export/Model/Export.php.

3. Create the Model

Now we need to create the model. As you can see in the config.xml file, we have to create a new file named Export.php in the Model/ subdirectory. Here is the content of the file:

[sourcecode language=”php”]
class CDL_Export_Model_Export extends Mage_Dataflow_Model_Convert_Parser_Abstract
{
protected $_fields;
protected $_mapfields = array();
public function parse()
{
// fixed for multibyte characters
setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().’.UTF-8′);
$fDel = $this->getVar(‘delimiter’, ‘,’);
$fEnc = $this->getVar(‘enclose’, ‘"’);
if ($fDel == ‘\t’) {
$fDel = "\t";
}
$adapterName = $this->getVar(‘adapter’, null);
$adapterMethod = $this->getVar(‘method’, ‘saveRow’);
if (!$adapterName || !$adapterMethod) {
$message = Mage::helper(‘dataflow’)->__(‘Please declare "adapter" and "method" node first’);
$this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
return $this;
}
try {
$adapter = Mage::getModel($adapterName);
}
catch (Exception $e) {
$message = Mage::helper(‘dataflow’)->__(‘Declared adapter %s not found’, $adapterName);
$this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
return $this;
}
if (!is_callable(array($adapter, $adapterMethod))) {
$message = Mage::helper(‘dataflow’)->__(‘Method "%s" not defined in adapter %s’, $adapterMethod, $adapterName);
$this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
return $this;
}
$batchModel = $this->getBatchModel();
$batchIoAdapter = $this->getBatchModel()->getIoAdapter();
if (Mage::app()->getRequest()->getParam(‘files’)) {
$file = Mage::app()->getConfig()->getTempVarDir().’/import/’
. urldecode(Mage::app()->getRequest()->getParam(‘files’));
$this->_copy($file);
}
$batchIoAdapter->open(false);
$isFieldNames = $this->getVar(‘fieldnames’, ”) == ‘true’ ? true : false;
if (!$isFieldNames && is_array($this->getVar(‘map’))) {
$fieldNames = $this->getVar(‘map’);
}
else {
$fieldNames = array();
foreach ($batchIoAdapter->read(true, $fDel, $fEnc) as $v) {
$fieldNames[$v] = $v;
}
}
$countRows = 0;
while (($csvData = $batchIoAdapter->read(true, $fDel, $fEnc)) !== false) {
if (count($csvData) == 1 && $csvData[0] === null) {
continue;
}
$itemData = array();
$countRows ++; $i = 0;
foreach ($fieldNames as $field) {
$itemData[$field] = isset($csvData[$i]) ? $csvData[$i] : null;
$i ++;
}
$batchImportModel = $this->getBatchImportModel()
->setId(null)
->setBatchId($this->getBatchModel()->getId())
->setBatchData($itemData)
->setStatus(1)
->save();
}
$this->addException(Mage::helper(‘dataflow’)->__(‘Found %d rows’, $countRows));
$this->addException(Mage::helper(‘dataflow’)->__(‘Starting %s :: %s’, $adapterName, $adapterMethod));
$batchModel->setParams($this->getVars())
->setAdapter($adapterName)
->save();
//$adapter->$adapterMethod();
return $this;
// // fix for field mapping
// if ($mapfields = $this->getProfile()->getDataflowProfile()) {
// $this->_mapfields = array_values($mapfields[‘gui_data’][‘map’][$mapfields[‘entity_type’]][‘db’]);
// } // end
//
// if (!$this->getVar(‘fieldnames’) && !$this->_mapfields) {
// $this->addException(‘Please define field mapping’, Mage_Dataflow_Model_Convert_Exception::FATAL);
// return;
// }
//
// if ($this->getVar(‘adapter’) && $this->getVar(‘method’)) {
// $adapter = Mage::getModel($this->getVar(‘adapter’));
// }
//
// $i = 0;
// while (($line = fgetcsv($fh, null, $fDel, $fEnc)) !== FALSE) {
// $row = $this->parseRow($i, $line);
//
// if (!$this->getVar(‘fieldnames’) && $i == 0 && $row) {
// $i = 1;
// }
//
// if ($row) {
// $loadMethod = $this->getVar(‘method’);
// $adapter->$loadMethod(compact(‘i’, ‘row’));
// }
// $i++;
// }
//
// return $this;
}
public function parseRow($i, $line)
{
if (sizeof($line) == 1) return false;
if (0==$i) {
if ($this->getVar(‘fieldnames’)) {
$this->_fields = $line;
return;
} else {
foreach ($line as $j=>$f) {
// $this->_fields[$j] = ‘column’.($j+1);
$this->_fields[$j] = $this->_mapfields[$j];
}
}
}
$resultRow = array();
foreach ($this->_fields as $j=>$f) {
$resultRow[$f] = isset($line[$j]) ? $line[$j] : ”;
}
return $resultRow;
}
/**
* Read data collection and write to temporary file
*
* @return Mage_Dataflow_Model_Convert_Parser_Csv
*/
public function unparse()
{
$batchExport = $this->getBatchExportModel()
->setBatchId($this->getBatchModel()->getId());
$fieldList = $this->getBatchModel()->getFieldList();
/* CDL Export start*/
$fieldList[‘gallery’] = ‘gallery’;
/* CDL Export finish*/
$batchExportIds = $batchExport->getIdCollection();
if (!$batchExportIds) {
return $this;
}
$io = $this->getBatchModel()->getIoAdapter();
$io->open();
if ($this->getVar(‘fieldnames’)) {
$csvData = $this->getCsvString($fieldList);
$io->write($csvData);
}
foreach ($batchExportIds as $batchExportId) {
$csvData = array();
$batchExport->load($batchExportId);
$row = $batchExport->getBatchData();
/* CDL Export start*/
$productid = Mage::getModel(‘catalog/product’)->getIdBySku($row[‘sku’]);
// Initiate product model
$product = Mage::getModel(‘catalog/product’);
// Load specific product whose tier price want to update
$product->load($productid);
$mediaGallery = $product->getMediaGallery();
$mediaGallery = $mediaGallery[‘images’];
$add_images = ”;
foreach ($mediaGallery as $add_image) {
if (!$add_image[‘disabled’]){
$add_images .= $add_image[‘file’].’;’;
}
}
// Remove last ‘;’ from images list
$add_images = substr_replace($add_images ,"",-1);

$row[‘gallery’] = $add_images;
/* CDL Export finish*/
foreach ($fieldList as $field) {
$csvData[] = isset($row[$field]) ? $row[$field] : ”;
}
$csvData = $this->getCsvString($csvData);
$io->write($csvData);
}
$io->close();
return $this;
}
public function unparseRow($args)
{
$i = $args[‘i’];
$row = $args[‘row’];
$fDel = $this->getVar(‘delimiter’, ‘,’);
$fEnc = $this->getVar(‘enclose’, ‘"’);
$fEsc = $this->getVar(‘escape’, ‘\\’);
$lDel = "\r\n";
if ($fDel == ‘\t’) {
$fDel = "\t";
}
$line = array();
foreach ($this->_fields as $f) {
$v = isset($row[$f]) ? str_replace(array(‘"’, ‘\\’), array($fEnc.’"’, $fEsc.’\\’), $row[$f]) : ”;
$line[] = $fEnc.$v.$fEnc;
}
return join($fDel, $line);
}
/**
* Retrieve csv string from array
*
* @param array $fields
* @return sting
*/
public function getCsvString($fields = array()) {
$delimiter = $this->getVar(‘delimiter’, ‘,’);
$enclosure = $this->getVar(‘enclose’, ”);
$escapeChar = $this->getVar(‘escape’, ‘\\’);
if ($delimiter == ‘\t’) {
$delimiter = "\t";
}
$str = ”;
foreach ($fields as $value) {
if (strpos($value, $delimiter) !== false ||
empty($enclosure) ||
strpos($value, $enclosure) !== false ||
strpos($value, "\n") !== false ||
strpos($value, "\r") !== false ||
strpos($value, "\t") !== false ||
strpos($value, ‘ ‘) !== false) {
$str2 = $enclosure;
$escaped = 0;
$len = strlen($value);
for ($i=0;$i<$len;$i++) {
if ($value[$i] == $escapeChar) {
$escaped = 1;
} else if (!$escaped && $value[$i] == $enclosure) {
$str2 .= $enclosure;
} else {
$escaped = 0;
}
$str2 .= $value[$i];
}
$str2 .= $enclosure;
$str .= $str2.$delimiter;
} else {
$str .= $enclosure.$value.$enclosure.$delimiter;
}
}
return substr($str, 0, -1) . "\n";
}
}
[/sourcecode]

As you can see, we create a new model class which extends the core model class. that means your new class will have all the properties and functions the core class has. all functions you put in your class will be used instead of the core classes functions, and your can also add completely new functions to it. For now we changed the unparse() function, so it will return additional images for products (during Export Products).

4. Load your module

Now we finished our module, but Magento doesn’t know of it’s existence yet. We have to create a file in /app/etc/Modules that loads our module. Name that file CDL_Export.xml and put this in it:

[sourcecode language=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<CDL_Export>
<active>true</active>
<codePool>local</codePool>
</CDL_Export>
</modules>
</config>
[/sourcecode]

All xml files inside /app/etc/Modules will be loaded automatically. By placing the above file in that directory, Magento will know that is has to look for a config.xml in /app/code/local/CDL/Export/etc and load your module.

Step 4

Test it out! Please clear cash before. This is how it works: Use the Magento product export functionality (Admin >> System >> Import/Export >> Profiles >> Export All Products). Check your file. You can see, we added a column ”gallery” and pictures are separated with “;”


Import products into Magento with multiple images

We did a migration from Zen Cart to Magento and we had to import all products with additional images. Almost every product had multiple images which was a problem because Magento doesn’t have built-in ability to upload multiple images for products. Why, i don’t know but we can easily extend Magento and create this ability.

The best way to accomplish this is to create a new module that will override the basic import module and perform the additional tasks we need. We also do it this way so that we do not touch the core code.

A few notes:

  • This will NOT work on version 1.7.x. It was written for 1.4 and I have yet to write an updated version. Sorry.
  • All pictures must be uploaded into the folder: “/media/import” else they will not be imported
  • The import module uses the “sku” to identify each product, if you have already imported the products but now just want to add the extra images you only need the “sku” and “gallery” columns, you do not have to re-import all your products.

1. Create Directories

First of all, you need to create some directory structure that will hold your module files. Go to /app/code/local and create a directory representing your’s or your company’s name. All your modules will go into that directory. I will use this for the example:
/app/code/local/CDL.
Now we need to create the module directory. The module directory represents the name your module gets. I will use:
/app/code/local/CDL/Import in this example.
Every module needs a configuration file. This file is located under the etc/ subdirectory, so you need to create that directory as well. In my example it is:
/app/code/local/CDL/Import/etc.

2. Config File

Save this in the file: /app/code/local/CDL/Import/etc/config.xml

[sourcecode language=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<CDL_Import>
<version>0.1.0</version>
</CDL_Import>
</modules>
<global>
<models>
<catalog>
<rewrite>
<convert_adapter_product>CDL_Import_Model_Gallery</convert_adapter_product>
</rewrite>
</catalog>
</models>
</global>
</config>
[/sourcecode]

3. Create the Model File

We create model in /App/code/local/CDL/Import/Model/Gallery.php

[sourcecode language=”php”]
<?php
/**
* Import Multiple Images during Product Import
*
*/

class CDL_Import_Model_Gallery extends Mage_Catalog_Model_Convert_Adapter_Product
{
/**
* Save product (import)
*
* @param array $importData
* @throws Mage_Core_Exception
* @return bool
*/
protected $custom_options = array();

public function saveRow(array $importData)
{
$product = $this->getProductModel()
->reset();

if (empty($importData[‘store’])) {
if (!is_null($this->getBatchParams(‘store’))) {
$store = $this->getStoreById($this->getBatchParams(‘store’));
} else {
$message = Mage::helper(‘catalog’)->__(‘Skip import row, required field "%s" not defined’, ‘store’);
Mage::throwException($message);
}
}
else {
$store = $this->getStoreByCode($importData[‘store’]);
}

if ($store === false) {
$message = Mage::helper(‘catalog’)->__(‘Skip import row, store "%s" field not exists’, $importData[‘store’]);
Mage::throwException($message);
}

if (empty($importData[‘sku’])) {
$message = Mage::helper(‘catalog’)->__(‘Skip import row, required field "%s" not defined’, ‘sku’);
Mage::throwException($message);
}
$product->setStoreId($store->getId());
$productId = $product->getIdBySku($importData[‘sku’]);

if ($productId) {
$product->load($productId);
}
else {
$productTypes = $this->getProductTypes();
$productAttributeSets = $this->getProductAttributeSets();

/**
* Check product define type
*/
if (empty($importData[‘type’]) || !isset($productTypes[strtolower($importData[‘type’])])) {
$value = isset($importData[‘type’]) ? $importData[‘type’] : ”;
$message = Mage::helper(‘catalog’)->__(‘Skip import row, is not valid value "%s" for field "%s"’, $value, ‘type’);
Mage::throwException($message);
}
$product->setTypeId($productTypes[strtolower($importData[‘type’])]);
/**
* Check product define attribute set
*/
if (empty($importData[‘attribute_set’]) || !isset($productAttributeSets[$importData[‘attribute_set’]])) {
/*$value = isset($importData[‘attribute_set’]) ? $importData[‘attribute_set’] : ”;
$message = Mage::helper(‘catalog’)->__(‘Skip import row, is not valid value "%s" for field "%s"’, $value, ‘attribute_set’);
Mage::throwException($message);*/
}
$product->setAttributeSetId($productAttributeSets[$importData[‘attribute_set’]]);

foreach ($this->_requiredFields as $field) {
$attribute = $this->getAttribute($field);
if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) {
$message = Mage::helper(‘catalog’)->__(‘Skip import row, required field "%s" for new products not defined’, $field);
Mage::throwException($message);
}
}
}

$this->setProductTypeInstance($product);

if (isset($importData[‘category_ids’])) {
$product->setCategoryIds($importData[‘category_ids’]);
}
/* if category name is in csv file */
if (isset($importData[‘categories’])) {

$categoryIds = $this->_addCategories($importData[‘categories’], $store);
if ($categoryIds) {
$product->setCategoryIds($categoryIds);
}
}
foreach ($this->_ignoreFields as $field) {
if (isset($importData[$field])) {
unset($importData[$field]);
}
}

if ($store->getId() != 0) {
$websiteIds = $product->getWebsiteIds();
if (!is_array($websiteIds)) {
$websiteIds = array();
}
if (!in_array($store->getWebsiteId(), $websiteIds)) {
$websiteIds[] = $store->getWebsiteId();
}
$product->setWebsiteIds($websiteIds);
}

if (isset($importData[‘websites’])) {
$websiteIds = $product->getWebsiteIds();
if (!is_array($websiteIds)) {
$websiteIds = array();
}
$websiteCodes = explode(‘,’, $importData[‘websites’]);
foreach ($websiteCodes as $websiteCode) {
try {
$website = Mage::app()->getWebsite(trim($websiteCode));
if (!in_array($website->getId(), $websiteIds)) {
$websiteIds[] = $website->getId();
}
}
catch (Exception $e) {}
}
$product->setWebsiteIds($websiteIds);
unset($websiteIds);
}

foreach ($importData as $field => $value) {
if (in_array($field, $this->_inventoryFields)) {
continue;
}
if (in_array($field, $this->_imageFields)) {
continue;
}
$attribute = $this->getAttribute($field);
if (!$attribute) {

if(strpos($field,’:’)!==FALSE && strlen($value)) {
$values=explode(‘|’,$value);
if(count($values)>0) {
@list($title,$type,$is_required,$sort_order) = explode(‘:’,$field);
$title = ucfirst(str_replace(‘_’,’ ‘,$title));
$custom_options[] = array(
‘is_delete’=>0,
‘title’=>$title,
‘previous_group’=>”,
‘previous_type’=>”,
‘type’=>$type,
‘is_require’=>$is_required,
‘sort_order’=>$sort_order,
‘values’=>array()
);
foreach($values as $v) {
$parts = explode(‘:’,$v);
$title = $parts[0];
if(count($parts)>1) {
$price_type = $parts[1];
} else {
$price_type = ‘fixed’;
}
if(count($parts)>2) {
$price = $parts[2];
} else {
$price =0;
}
if(count($parts)>3) {
$sku = $parts[3];
} else {
$sku=”;
}
if(count($parts)>4) {
$sort_order = $parts[4];
} else {
$sort_order = 0;
}
switch($type) {
case ‘file’:
/* TODO */
break;

case ‘field’:
case ‘area’:
$custom_options[count($custom_options) – 1][‘max_characters’] = $sort_order;
/* NO BREAK */

case ‘date’:
case ‘date_time’:
case ‘time’:
$custom_options[count($custom_options) – 1][‘price_type’] = $price_type;
$custom_options[count($custom_options) – 1][‘price’] = $price;
$custom_options[count($custom_options) – 1][‘sku’] = $sku;
break;

case ‘drop_down’:
case ‘radio’:
case ‘checkbox’:
case ‘multiple’:
default:
$custom_options[count($custom_options) – 1][‘values’][]=array(
‘is_delete’=>0,
‘title’=>$title,
‘option_type_id’=>-1,
‘price_type’=>$price_type,
‘price’=>$price,
‘sku’=>$sku,
‘sort_order’=>$sort_order,
);
break;
}
}
}
}

continue;
}

$isArray = false;
$setValue = $value;

if ($attribute->getFrontendInput() == ‘multiselect’) {
$value = explode(self::MULTI_DELIMITER, $value);
$isArray = true;
$setValue = array();
}

if ($value && $attribute->getBackendType() == ‘decimal’) {
$setValue = $this->getNumber($value);
}

if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);

if ($isArray) {
foreach ($options as $item) {
if (in_array($item[‘label’], $value)) {
$setValue[] = $item[‘value’];
}
}
}
else {
$setValue = null;
foreach ($options as $item) {
if ($item[‘label’] == $value) {
$setValue = $item[‘value’];
}
}
}
}

$product->setData($field, $setValue);
}

if (!$product->getVisibility()) {
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
}

$stockData = array();
$inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()])
? $this->_inventoryFieldsProductTypes[$product->getTypeId()]
: array();
foreach ($inventoryFields as $field) {
if (isset($importData[$field])) {
if (in_array($field, $this->_toNumber)) {
$stockData[$field] = $this->getNumber($importData[$field]);
}
else {
$stockData[$field] = $importData[$field];
}
}
}
$product->setStockData($stockData);

$imageData = array();
foreach ($this->_imageFields as $field) {
if (!empty($importData[$field]) && $importData[$field] != ‘no_selection’) {
if (!isset($imageData[$importData[$field]])) {
$imageData[$importData[$field]] = array();
}
$imageData[$importData[$field]][] = $field;
}
}

foreach ($imageData as $file => $fields) {
try {
$product->addImageToMediaGallery(Mage::getBaseDir(‘media’) . DS . ‘import’ . $file, $fields, false, false);
}
catch (Exception $e) {}
}

/**
* Allows you to import multiple images for each product.
* Simply add a ‘gallery’ column to the import file, and separate
* each image with a semi-colon.
*/
try {
$galleryData = explode(‘;’,$importData["gallery"]);
foreach($galleryData as $gallery_img)
/**
* @param directory where import image resides
* @param leave ‘null’ so that it isn’t imported as thumbnail, base, or small
* @param false = the image is copied, not moved from the import directory to it’s new location
* @param false = not excluded from the front end gallery
*/
{
$product->addImageToMediaGallery(Mage::getBaseDir(‘media’) . DS . ‘import’ . $gallery_img, NULL, false, false);
}
}
catch (Exception $e) {}
/* End Modification */

$product->setIsMassupdate(true);
$product->setExcludeUrlRewrite(true);

$product->save();
/* Add the custom options specified in the CSV import file */

if(isset($custom_options)){
if(count($custom_options)) {
foreach($custom_options as $option) {
try {
$opt = Mage::getModel(‘catalog/product_option’);
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
catch (Exception $e) {}
}
}
}
return true;
}

protected $_categoryCache = array();
/* Add category and sub category. */
protected function _addCategories($categories, $store)
{

$rootId = $store->getRootCategoryId();

if (!$rootId) {
/* If stoder not create that mense admin then assign 1 to storeId */
$storeId = 1;
$rootId = Mage::app()->getStore($storeId)->getRootCategoryId();

}

if($categories=="")
return array();
$rootPath = ‘1/’.$rootId;
if (empty($this->_categoryCache[$store->getId()])) {
$collection = Mage::getModel(‘catalog/category’)->getCollection()
->setStore($store)
->addAttributeToSelect(‘name’);
$collection->getSelect()->where("path like ‘".$rootPath."/%’");

foreach ($collection as $cat) {
$pathArr = explode(‘/’, $cat->getPath());
$namePath = ”;
for ($i=2, $l=sizeof($pathArr); $i<$l; $i++) {
$name = $collection->getItemById($pathArr[$i])->getName();
$namePath .= (empty($namePath) ? ” : ‘/’).trim($name);
}
$cat->setNamePath($namePath);
}

$cache = array();
foreach ($collection as $cat) {
$cache[strtolower($cat->getNamePath())] = $cat;
$cat->unsNamePath();
}
$this->_categoryCache[$store->getId()] = $cache;
}
$cache =& $this->_categoryCache[$store->getId()];

$catIds = array();
foreach (explode(‘,’, $categories) as $categoryPathStr) {
$categoryPathStr = preg_replace(‘#\s*/\s*#’, ‘/’, trim($categoryPathStr));
if (!empty($cache[$categoryPathStr])) {
$catIds[] = $cache[$categoryPathStr]->getId();
continue;
}
$path = $rootPath;
$namePath = ”;
foreach (explode(‘/’, $categoryPathStr) as $catName) {
$namePath .= (empty($namePath) ? ” : ‘/’).strtolower($catName);
if (empty($cache[$namePath])) {
$cat = Mage::getModel(‘catalog/category’)
->setStoreId($store->getId())
->setPath($path)
->setName($catName)
->setIsActive(1)
->save();
$cache[$namePath] = $cat;
}
$catId = $cache[$namePath]->getId();
$path .= ‘/’.$catId;
}
if ($catId) {
$catIds[] = $catId;
}
}
return join(‘,’, $catIds);
}
}
[/sourcecode]

4. Load the Moduel

Save this in the file “/app/etc/modules/CDL_Import.xml”

[sourcecode language=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<CDL_Import>
<active>true</active>
<codePool>local</codePool>
</CDL_Import>
</modules>
</config>
[/sourcecode]

5. Clear Cache and Check Results

Clear the cache!

We import products with csv file and example of csv:

[sourcecode language=”csv”]
"store","websites","attribute_set","type"……"gallery"
"admin","base","Default","simple"……"/tees/nautical_girls650.jpg;/tees/nautical_girls750.jpg"
[/sourcecode]

You can see, we added a column ”gallery”
and pictures are separated with “;”

All pictures must be uploaded in folder: “/media/import” and you can go to: (Admin > System > Import/Export > Profiles >> Import All Products) and import can begin…