Browsing articles in "Magento"
Feb 20, 2013
kalpesh

Magento: Reset download limit for downloadable product item/order

In Magento, downloadable products are the products which can be downloaded and does not require any shipping. When customer purchases this type of products, they get certain download limits to download their purchased items. This can be set in Products page in admin under “Downloadable Products” tab. In many cases store owner only want to give certain amount of downloads, so customers can’t download beyond that limit. But it may happen that due to bad internet or some interrupts downloads can’t be successful and customer want to reset that limit. For doing this, there is no option in backend to reset the download limits for the downloadable product items or for an entire order, and unfortunately admin use to re-order to achieve this.

Here is the query which will reset download limits for an entire order.

$q = Mage::getModel('sales/order_item')->getCollection()->addFieldToSelect('order_id');
$q->getSelect()->joinRight('downloadable_link_purchased_item as d', 'main_table.item_id = d.order_item_id',
        array('item_id','number_of_downloads_used'));
$q->getSelect()->where('main_table.order_id = '. (int)$orderId);
$q->load();

//echo $q->getSelect()->__toString();exit;

$items = $q->getColumnValues('item_id');
foreach($items as $id) {
        Mage::getModel('downloadable/link_purchased_item')->load($id)
                ->setStatus('available')->setNumberOfDownloadsUsed(0)->save();
}

The raw SQL query for the above Magento Convention query is:
UPDATE sales_flat_order_item s RIGHT JOIN downloadable_link_purchased_item d ON s.item_id = d.order_item_id SET d.number_of_downloads_used = 0 WHERE s.order_id = $orderId;
Continue reading »

Feb 9, 2013
kalpesh

Magento 500 internal server error

[Resolved]: Magento 500 internal server error
If you are getting “500 Internal Server Error” then the reason might be permissions issue or fatal error.

In Magento, you can check the errors occured in files:
var/log/system.log
var/log/exception.log

You can even allow the error to output to your browser by editing your Magento index.php with:
error_reporting(E_ALL | E_STRICT);
ini_set(‘display_errors’, 1);

Also, try this to solve the error:
– Check the owner of your magento project. It should be the server (www-data for apache)
chown -R www-data:www-data .

– Change the directory permissions to 755 and file permissions to 644 for your project
find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;
chmod 550 pear
chmod 550 mage
chmod 755 -R var

– Check after upgrade do you have your .htaccess file inside Magento root

Feb 9, 2013
kalpesh

Linux/Magento: Daily useful development commands

These are the daily commands I use at work. This will definately help someone who is not familiar with all these commands in Linux and Magento.

Linux: Search / Replace recursively

find /path/here/ -type f -exec sed -i ‘s/search string/replace string/g’ {} \;

Linux: Find all files matching a string

grep -r “string to search” /path/to/search/

Linux: In vi editor, copy/paste/delete/replace/insert/save/undo/redo/search-replace/goto
Open file: vi filename
Copy: yy (yank) (for multiple lines copy, 5yy – for copying 5 lines)
Paste: p
Delete: dd (for multiple lines delete, 5dd – for deleting 5 lines)
Replace: r (on character you wish to replace, press r and enter new character)
Insert: i or insert key
Save: escape and : x (without space between : and x) (x = write and quit, w = write, q = quit, q! = quit without saving)
Undo: u
Redo: Ctrl+R
Search: escape key THEN / THEN write string to find THEN enter
Search-Replace: :%s/search string/replacement string/g
Goto Line number: escape key THEN : THEN line number (e.g. :22 to go to 22nd line of current file)

Linux: Search previous execute command in terminal

in terminal, press Ctrl+R and type few characters.
This will show you previous command matching that sequence of characters
To check another command matching same characters, press Ctrl+R again

Linux: Recursively change files/directories permissions

find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;

owner / read (4) + write (2) + execute (1)
group / read (4) + write (2) + execute (1)
world / read (4) + write (2) + execute (1)

Linux: Show unique values of any column in CSV

cat file.csv | cut -f 1 -d , | sort|uniq -c | tee tempfile

here, 1 = 1st column, which can be changed to view any column
uniq -c = unique values with count of occurences of each value. You can omit -c to just see the unique values
file.csv = filename, you can have any file here with any extension
, = delimiter used to separate columns
tee tempfile = moves the output to file “tempfile”

Linux: Recursively change ownership of directories/files

chown -R www-data:www-data magento
-R = for recursive
www-data:www-data = change owner to www-data (apache)
magento = directory to change permission for, recursively

Linux: Remove all Magento cache files

From your magento root in terminal, execute this:
rm -rf var/cache/*
DON’T put any space after cache/ and *, otherwise it will delete your WHOLE magento project 😀

To delete all the active sessions on your magento store,
rm -rf var/session/*
Continue reading »

Feb 7, 2013
kalpesh

Magento: Add product custom attribute options dynamically

Once you have the product attributes in place, and you want to add lot of options to these attributes it becomes time consuming and hectic task. If you have only few attribute options to add, it’s easy to go to backend and add manually. But if you have many attribute options, more than 10 options for many attributes, it’s very tiresome and feels like data entry job. Magento is not built to do stuff manually, so here I will provide you the code snippet which will push all the attribute options to their respective attribute.

Create a file in the root of your Magento installation and copy this code there. I generally name it test.php

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

//Change this....
$aColors = array('PINK','BLACK','BLUE','BROWN','GOLD','GRAY','GREEN','MULTI','NEUTRALS/CREAM/WHITE','ORANGE','PURPLE','RED','RUST','TAUPE','YELLOW');
$attrCode = 'color'; //this should be there already
//........

$aColors = array_map('utf8_encode',$aColors); //If you're using special characters
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, $attrCode);

for($iCount=0;$iCount<sizeof($aColors);$iCount++){
   $aOption['value']['option'.$iCount][0] = $aColors[$iCount];
}
$installer->addAttributeOption($aOption);

$installer->endSetup();

Now you just need to give the existing product attribute name AND future attribute options to this script. Run it and rest all script will do for you! 🙂

Feb 2, 2013
kalpesh

Magento: Create database backups daily programatically by cron

It’s a good practice to create your database backup every day/week. But to manually go to Magento admin and create backup daily is a cumbersome process. You can’t guarantee that your database will be secure especially if you are on a cloud storage. Backups are MUST and if it’s automated, life becomes very easier.

This code will create backup of your Magento DB and place it to var/backups directory.

public function backup()
{
    try {

        $backupDb = Mage::getModel('backup/db');
        $backup   = Mage::getModel('backup/backup')
            ->setTime(time())
            ->setType('db')
            ->setPath(Mage::getBaseDir("var") . DS . "backups");

        $backupDb->createBackup($backup);

        return Mage::helper('core')->__('Backup successfully created');

    } catch (Exception  $e) {
        Mage::logException($e);
    }

    return $this;
}

Continue reading »

Pages:«1...15161718192021...29»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions