If you want to display Images in Same Size in grid in List page use Following Code
helper('catalog/image')->init($_product, 'small_image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(135,135)?>
in list.phtml file for images
Good day
RegardsDarshan Dodiya
Friday, March 16, 2012
Friday, February 24, 2012
Add Attributes Value From Admin using Csv
F:\xampplite\htdocs\darshan\app\code\core\Mage\Catalog\Model\Convert\Adapter
create import.php and paste following content
////////////////
class Mage_Catalog_Model_Convert_Adapter_Import extends Mage_Catalog_Model_Convert_Adapter_Product {
/**
* Save product (import)
* @param array $importData
* @throws Mage_Core_Exception
* @return bool
*/
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);
}
public function saverow(array $importData)
{
$product = $this->getProductModel()
->reset();
$importData=array_filter ($importData);
if (!isset($importData['sku'])) {
$message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined.', 'sku');
Mage::throwException($message);
}
if(isset($importData['rsize']))
{
$rsize=explode("/",$importData['rsize']);
$asprim=explode("R",$rsize[1]);
$importData=array_merge($importData,array('secwidth'=>$rsize[0],'aspratio'=>$asprim[0],'rim'=>$asprim[1]));
}
if(!isset($importData['store'])||!isset($importData['websites'])||!isset($importData['attribute_set'])||!isset($importData['type'])||!isset($importData['tax_class_id'])||!isset($importData['status'])||!isset($importData['visibility']))
{
$defaultary=array('store'=>'default','websites'=>'base','attribute_set'=>'Tires','type'=>'simple','tax_class_id'=>'None','status'=>'Enabled','visibility'=>"Catalog, Search");
$importData=array_merge($defaultary,$importData);
}
if(isset($importData['is_in_stock']) && strcmp($importData['is_in_stock'],'Y')==0||strcmp($importData['is_in_stock'],'y')==0)
{
$importData['is_in_stock']=1;
}
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 product name is "'.$importData['name'].'"', '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);
}
$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) {
continue;
}
$isArray = false;
$setValue = $value;
if ($attribute->getFrontendInput() == 'multiselect') {
$temp= explode(self::MULTI_DELIMITER, $value);
$value=array();
foreach($temp as $values){
$value[]=trim($values);
$isArray = true; // This line is added to activate Multi select option insert. - By Rohit.
}
}
if ($value && $attribute->getBackendType() == 'decimal') {
$setValue = $this->getNumber($value);
}
/**CODE MODIFICATION STARTS HERE*/
$optionLabelArray=array();
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
/**
* Update the Source of the attribute when source has no options.
*/
if(count($options)<1){
if($isArray){
foreach($value as $key=>$subvalue){
if(!in_array($subvalue,$newOptionLabelArray)){
$setValue[]=$this->updateSourceAndReturnId($field,trim($subvalue));
array_push($newOptionLabelArray,trim($subvalue));
}
}
}
else{
if(!in_array($value,$newOptionLabelArray)){
$setValue=$this->updateSourceAndReturnId($field,trim($value));
array_push($newOptionLabelArray,$value);
}
}
}
/**
* Work on the source when it has options
*/
else{
/**
* This is the case of Multi-Select
*/
if ($isArray) {
$setValue = array(); // This line is added to activate Multi select option insert. - By Rohit.
$newOptionLabelArray=array(); // This line is added to activate Multi select option insert. - By Rohit.
foreach ($options as $item) {
/** Setting the option's ID if Label matches with the current value of XML column.*/
if (in_array($item['label'], $value)) {
$setValue[] = trim($item['value']);
array_push($optionLabelArray,$item['label']); /**Adding Reference to worked attribute option**/
}
}
/**
* Checking in the current XML column value if all values were used in the above loop or not
* If not used then they are new options value, then new option is created and then assigned.
**/
foreach($value as $key=>$subvalue){
if(!in_array($subvalue,$optionLabelArray)){
$setValue[]=$this->updateSourceAndReturnId($field,trim($subvalue));
}
}
}
/**This is the case of single select**/
else {
$setValue = null;
$newOptionLabelArray=array();
foreach ($options as $item) {
if ($item['label'] == $value) {
$setValue = $item['value'];
array_push($optionLabelArray,$item['label']); /**Adding Reference to worked attribute option**/
}
}
/**
* Checking in the current XML column value if all values were used in the above loop or not
* If not used then they are new options value, then new option is created and then assigned.
**/
if(!in_array($value,$optionLabelArray)){
$setValue=$this->updateSourceAndReturnId($field,trim($value));
}
}
}
}
/**CODE MODIFICATION ENDS HERE*/
$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;
}
}
$imageData=array_filter($imageData);
$importData=array_merge($importData,array('remove_all_images'=>'yes'));
//MRD added to remove all images for product before uploading new images
if(isset($importData['image']) && isset($importData['small_image'])){
//check if gallery attribute exists then remove all images if it exists
//Get products gallery attribute
$attributes = $product->getTypeInstance()->getSetAttributes();
if (isset($attributes['media_gallery'])) {
$gallery = $attributes['media_gallery'];
//Get the images
$galleryData = $product->getMediaGallery();
foreach($galleryData['images'] as $image){
//If image exists
if ($gallery->getBackend()->getImage($product, $image['file']))
{
$gallery->getBackend()->removeImage($product, $image['file']);
}
}
}
#$gallery->clearMediaAttribute($product, array('image','small_image','thumbnail'));
}
//END Remove Images
foreach ($imageData as $file => $fields)
{
if(isset($file))
{
$fileary=explode("/",$file);
$l=count($fileary);
// print($file);
//print_r($fileary);die;
//*copy($fields,Mage::getBaseDir('media').DS.'import'.DS.);
if ($this->copyFile($file,Mage::getBaseDir('media').DS.'import'.DS))
{
$product->addImageToMediaGallery(Mage::getBaseDir('media').DS.'import'.DS.$fileary[$l-1], $fields,$move=false,$exclude=false);
}
}
}
$product->setIsMassupdate(true);
$product->setExcludeUrlRewrite(true);
$product->save();
return true;
}
/**
* Updates the source of the attribute by the current new value in the XML column, and returns
* the id of the newly created option.
*
* @param string $attribute_code
* @param string $newOption
* @return int|string New Option Id
*/
public function updateSourceAndReturnId($attribute_code,$newOption){
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
$attribute_code = $attribute_model->getIdByCode('catalog_product', $attribute_code);
$attribute = $attribute_model->load($attribute_code);
$attribute_table = $attribute_options_model->setAttribute($attribute);
try{
$value['option'] = array(trim($newOption),trim($newOption));
$result = array('value' => $value);
$attribute->setData('option',$result);
$attribute->save();
}
catch(Exception $e){}
$options = $attribute_options_model->getAllOptions(false);
foreach($options as $option)
{
if ($option['label'] == $newOption)
{
return $option['value'];
}
}
return "";
}
function copyFile($url,$dirname){
//echo $dirname;
//die;
@$file = fopen ($url, "rb");
if (!$file) {
//echo"Failed to copy $url!
";
return false;
}else {
$filename = basename($url);
$fc = fopen($dirname."$filename", "wb");
while (!feof ($file)) {
$line = fread ($file, 1028);
//$line=resizeImage($url,150,150);
fwrite($fc,$line);
}
fclose($fc);
// echo "File $url saved to PC!
";
return true;
}
}
}
?>
put your csv in F:\xampplite\htdocs\valuetires\var\import
add following script in ActionXMl text box
file
var/import
true
1
catalog/convert_adapter_import
saverow
RegardsDarshan Dodiya
Subscribe to:
Posts (Atom)