Monday, October 19, 2015

Magento: Product Bulk Images Deletion

<?php
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
ini_set('display_errors', 1);
echo 'Delete Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
    $csv = new Varien_File_Csv();
    $csvfilepath = '../../../../tmp/deleteimages.csv';
   
    $cssfile = fopen($csvfilepath,"r");  
    $data = $csv->getData($csvfilepath);
    //Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
    echo 'CSV file exsist <br/>';
    $rows = count($data);
    for ($j = 1; $j<= $rows; $j++) {
        $productSKU = $data[$j][0];
        if(!empty($productSKU)){
            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
            if (!$product){
                //Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
                echo $productSKU.' SKU doesn`t exsist <br/>';
            }else{
                //deleting
                Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);              
                $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
                $product = Mage::getModel('catalog/product')->load($product->getId());
                $items = $mediaApi->items($product->getId());
                $attributes = $product->getTypeInstance()->getSetAttributes();
                $gallery = $attributes['media_gallery'];
                foreach($items as $item){
                    if ($gallery->getBackend()->getImage($product, $item['file'])) {
                        $gallery->getBackend()->removeImage($product, $item['file']);
                    }
                }
                $product->save();
                echo 'Images deleted for sku '.$productSKU.'<br/>';
            }
        }
    }
    fclose($cssfile);
    echo 'Deletion Successfully at '.date("Y-m-d h:i:sa").' <br/>';
}  catch (Exception $e){
    //Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
    echo $e->getMessage();
}  
       

Magento: Import Product Bulk Images

<?php
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
echo 'Bulk Import Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
    $csv = new Varien_File_Csv();
   
    $csvfilepath = '../../../../tmp/importimage.csv';    
    $imagepath = '../../../../tmp/import/';
    $cssfile = fopen($csvfilepath,"r");  
    $data = $csv->getData($csvfilepath);
    //Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
    echo 'CSV file exsist <br/>';
    $rows = count($data);
   
   
   
    for ($j = 1; $j<= $rows; $j++) {
        $productSKU = $data[$j][0];
        if(!empty($productSKU)){
            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
            if (!$product){
                //Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
                echo $productSKU.' SKU doesn`t exsist <br/>';
            }else{
            //Mage::log($productSKU.' SKU product loaded', null, 'bulkimagesimport.log');
            echo $productSKU.' SKU product loaded <br/>';
            $imagesArray = explode('|', $data[$j][1]);          
            $count = 0;
            $mediaAttribute = array (
                'image',
                'small_image',
                'thumbnail',
                'reference_image',
                'faceimage'
            );
            foreach ($imagesArray as $image){
                $imagefile = $imagepath.trim($image);
                if (file_exists($imagefile)) {                          
                    if ($count == 0){
                        $product->addImageToMediaGallery( $imagefile , $mediaAttribute, false, false );
                    }else{
                        $product->addImageToMediaGallery( $imagefile , null, false, false );
                    }
                    $product->setStoreId(0);
                    //Mage::log($imagefile.' Image uploaded', null, 'bulkimagesimport.log');
                    echo $image.' Image uploaded <br/>';
                } else {
                    //Mage::log($imagefile.' Image not found', null, 'bulkimagesimport.log');
                    echo $image.' Image not found <br/>';
                }
                $count++;
            }

            $product->save();
            //Mage::log($productSKU.' SKU uploaded successfully', null, 'bulkimagesimport.log');
            echo $productSKU.' SKU uploaded successfully <br/>';
        }
        }
    }
    fclose($cssfile);
    echo 'Uploaded Successfully at '.date("Y-m-d h:i:sa").' <br/>';
}  catch (Exception $e){
    //Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
    echo $e->getMessage();
}  
       

Wednesday, August 13, 2014

How to change core configuration data in magento programmatically

$coreConfig = Mage::getModel('core/config');
$coreConfig ->saveConfig($path, $value, $scope = ‘default’, $scopeId = 0);

For it magento use core_config_data table and
core_config_data table contain two important fields scope and scope_id.
There are three scope types
    default
    websites
    stores
If scope is set to default then scope_id is always 0.
If scope is set to websites then scope_id is website_id.
If scope is set to stores then scope_id is store_id(store view).
Imagine that we need to get some config value.
How Magento will get the it for current store view?
Search value by priority:
    scope == stores and scope_id == store_id(store view)
    scope == websites and scope_id == website_id (to which belongs current store view)
    scope == default
    default section of config.xml

Wednesday, July 30, 2014

Magento : Difference between “Flush Magento Cache” and “Flush Cache Storage” in magento cache management

Flush Magento Cache

Removes all items in the default Magento cache (var/cache) and the var/full_page cache that have a Magento tag

Flush Cache Storage

Removes all items in the cache. This is the equivalent of deleting the entire contents of the cache folder on the server.If your system uses an alternate cache location, any cached files used by other applications will be removed.

Sunday, June 15, 2014

Add wysiwyg editor in Magento Custom Module

Considering custom module Bd_Demo

Add following function in Bd_Demo_Block_Adminhtml_Demo_Edit

protected function _prepareLayout() {
    parent::_prepareLayout();
    if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
        $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
    }
}

Then use as in grid from  Bd_Demo_Block_Adminhtml_Demo_Edit_Tab_Form
$fieldset->addField("wood_description", "editor", array(
"label" => Mage::helper("woodflooring")->__("Description"),
"class" => "required-entry",
"required" => true,
"name" => "wood_description",
'config'    => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
'wysiwyg'   => true,
));

Wednesday, May 14, 2014

Accordion using jQuery

<div id="accordion">
  <h3 class="title">First header</h3>
  <div class="content">First content panel</div>
  <h3 class="title">Second header</h3>
  <div class="content">Second content panel</div>
</div>

<style>
.content{display:none;}
</style>

//Type One
<script>
jQuery('#accordion .title').each(function(){
jQuery(this).addClass('active');
jQuery(this).toggle(function(){
jQuery(this).addClass('active').next().slideDown(200);
},function(){
jQuery(this).removeClass('active').next().slideUp(200);
})
}); 
</script>

//Type Two
<script>
a = jQuery('.footer-menu').find('#accordion .title');
console.log(a.hasClass('active'));
jQuery('#accordion .title').click(function(e){ 
e.preventDefault();
speed = 300;
if(jQuery(this).hasClass('active') === true) {
} else if(a.hasClass('active') === false) {
jQuery(this).addClass('active').next('.content').slideDown(speed);
} else {
a.removeClass('active').next('.content').slideUp(speed);
jQuery(this).addClass('active').next('.content').delay(speed).slideDown(speed);
}
});
</script>

Tuesday, May 6, 2014

Magento : SQL Injection in Magento

SQL injection is a technique where malicious users can inject SQL commands into an SQL statement, via web page input.

Binding is the way to go for direct queries in Magento.
As
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$query = "insert into table_name(name, email, company, description) values (:name, :email, :company, :desc)";
$binds = array(
    'name'      => "name' or 1=1",
    'email'     => "email",
    'company'   => "company",
    'desc'      => "desc",
);
$write->query($query, $binds);