Category Archives: Magento

Magento update product sku programmatically using csv

Put a skucsv.csv file which containt 2 columns.
1st column containt OLD sku list and 2nd column containt New sku list.
Create a updateProductSku.php file in root and paste below script on this file.





error_reporting(E_ALL);
ini_set('display_errors', 1);
define('MAGENTO_ROOT', getcwd());
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::init();
$row = 1;

if (($handle = fopen("skucsv.csv", "r")) !== FALSE) {

    while (($sku = fgetcsv($handle, 1000, ",")) !== FALSE) {
 // $sku[0] OLD sku list
 // $sku[1] New sku list
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',
                $sku[0]);
       
        if ($product && $product->getTypeId() == 'simple') {
            $product->setSku($sku[1]);
            $product->save(); 
        }else{            
            echo $sku[0].' Does not found SKU <br />';
        }

        unset($product);       
        $row++;
    }

    fclose($handle);
}

Magento remove product from category programatically

Create a file in root directory of project and paste below code on that file and run this file on browser.
Please take a DB backup before use it.




In below script we are checking New category product From Date and To Date. if any product does not meet the date range than product automatically removed from that category


error_reporting(E_ALL);
ini_set('display_errors', 1);
define('MAGENTO_ROOT', getcwd());
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
Mage::init();
$newCatId = 147; //category Id those you want to check it.


$cat = Mage::getModel('catalog/product')->setId($newCatId);
$collection = Mage::getModel('catalog/product')->getCollection()
->addCategoryFilter($cat)
->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', 1);
$collection->addFieldToFilter(array(array('attribute' => 'visibility', 'neq' => "1")));
$collection->addUrlRewrite($cat);
//$collection->getSelect()->order('rand()');
//$collection->getSelect()->limit(4);



//Subtract 1 Day from current date
$date = strtotime(date('Y-m-d'));
$newDateDay = date('Y-m-d', strtotime('-1 day', $date)) . ' ' . date('H:i:s'); 


if (count($collection) > 0):
    foreach ($collection as $item):
        if (($item->getNewsFromDate() != '' && $item->getNewsFromDate() < date('Y-m-d H:i:s'))
                && ($item->getNewsToDate() == '' or $item->getNewsToDate() >= $newDateDay)) {
		// put the code that you want to meet the requirement
            
        } else {
            //Remove product from that category	    
            Mage::getSingleton('catalog/category_api')
		->removeProduct($newCatId,$item->getId());  // First Parameter is Category Id and Second is Product Id
        }

    endforeach;
endif;

// Indexing of the category
$process = Mage::getModel('index/indexer')->getProcessByCode('catalog_category_product');
$process->reindexAll();

magento change choose an option text in product detail page

It’s very simple to change the text of default Magento choose an option to size or whatever we want.

Go to your theme file location

app/design/frontend/YOUR-THEME_NAME(default)/default/template/catalog/product/view/type/options/configurable.phtml


Rplace this code 
 <script type="text/javascript">
        var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
 </script>

to this one

<script type="text/javascript">
    <?php
        $jsonConfig = $this->getJsonConfig();
        $jsonConfig = str_replace("Choose an Option...", "Size", $jsonConfig);
    ?>
    var spConfig = new Product.Config(<?php echo $jsonConfig; ?>);
</script>

magento pincode number digit validation

<input type="text" maxlength="6" placeholder="Zip/Postal Code*" class="input-text required-entry validate-length maximum-length-6 minimum-length-6 validate-digits" value="" id="billing:postcode" name="billing[postcode]" title="Pincode">

if you want to use any number of integer validation on input filed use this 2 syntax.
maxlength=”6″
class=”input-text required-entry validate-length maximum-length-6 minimum-length-6 validate-digits”

10 digit mobile number validation in magento

 <input type="text" maxlength="10" name="mobile" id="mobile" placeholder="Mobile"  title="<?php echo $this->__('Mobile') ?>" class="input-text required-entry validate-length maximum-length-10 minimum-length-10 validate-digits" />

if you want to use any number of integer validation on input filed use this 2 syntax.
maxlength=”10″
class=”input-text required-entry validate-length maximum-length-10 minimum-length-10 validate-digits”