Write the output of var_dump to a text file in PHP

This code snippet has been handy for me over the past few years when I needed to log something in php but I didn’t want to include it in the normal logs.  This routine gives you a separate log file.

Put the following code in the routine that contains the array that you want to dump.  Be sure that /PATH_TO_OUTPUT_FILE/output.txt has the correct permissions.  Usually this is 777 but don’t keep it that way very long.  To avoid using 777 as your permissions, try setting the owner of the file and directory to apache:apache.

<?php

ob_start();
var_dump($STRING_TO_DUMP);
$output = ob_get_clean();

$outputFile = “/PATH_TO_OUTPUT_FILE/output.txt”;
$filehandle = fopen($outputFile, ‘a’) or die(“File creation error.”);
fwrite($fileHandle, $output);
fclose($fileHandle);

?>

Script to backup a database

Create a filed called “database_backup.sh

Put the following code in that file:

#!/bin/bash
filename=`date ‘+%m%d%y’`
mysqldump -u YOUR_USERNAME -p YOUR_DATABASE_NAME —password=YOUR_DATABASE_PASSWORD > /PATH_TO_SAVE_FILE
-backups/database-$filename.sql
echo Database backup completed for $filename.

Replace YOUR_USERNAME, YOUR_DATABASE_NAME, YOUR_DATABASE_PASSWORD and PATH_TO_SAVE_FILE with the values appropriate for your database and backup file save locations.

Either with FTP or SSH, change the file permission to 755.  In SSH this can be accomplished by running this command:

chmod 755 database_backup.sh

You can run the command by typing:

./database_backup.sh

You can set this up in crontab to run the command once per day at 1AM by doing the following:

crontab -e

This will open vi to allow you to add to the cron jobs.

Add this to the cron by typing “i” or hitting the INSERT key, then paste:

0 1 * * *     /PATH_TO_FILE/database_backup.sh

Replace PATH_TO_FILE with the path to your database_backup.sh file.  If you’re in the folder you can type pwd to get the full path.

Hit : (colon) and w to save the file, then : (colon) and q to quit.  If it updates successfully you’ll see crontab: installing new crontab.

Now every day your database will be backed up.  Be sure to use a location BELOW web root so the file won’t be accessible from the web.

Get all products under a category in Magento

If you need to get all of the products under a category in Magento then this code will help.

<?php

# Uncomment the next three lines if you’re calling this outside of Magento
#require_once ‘./app/Mage.php’;
#umask(0);
#Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

# If you’re calling this from inside a phtml file then you can use something like this to get the category ID
#$category_id = $this->getCurrentCategory()->getId();
# or use the following line:
$category_id = 1; # Change category ID here.

$category = Mage::getModel(‘catalog/category’)->load($category_id);

$products = Mage::getModel(‘catalog/product’)
->getCollection()
->addAttributeToSelect(‘*’)
->addCategoryFilter($category)
->load();

foreach($products as $product){
echo ‘<a href=”‘.$product->getProductUrl().'”>’.$product->getName().”</a><br>”;
}
?>

How to fix integrity constraint violation when adding columns to Order Grid in Magento

For an excellent post on how to add columns to your Sales Order Grid refer to this site:

http://inchoo.net/ecommerce/magento/how-to-extend-magento-order-grid/

You’ve just added a new column to your Magento Order Grid and everything looks good until you try to search or sort.  When you attempt any type of filtering you get redirected to your Dashboard and upon returning to the Sales Order Grid you’re greeted with the following error:

There has been an error processing your request

SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘status’ in where clause is ambiguous

Modify the Column code that is causing the error like this:

$this->addColumn(‘status’, array(
‘header’ => Mage::helper(‘sales’)->__(‘Status’),
‘index’ => ‘status’,
‘type’  => ‘options’,
‘width’ => ’70px’,
‘options’ => Mage::getSingleton(‘sales/order_config’)->getStatuses(),
‘filter_index’=>’main_table.status’

));

Adding ‘filter_index’=>’main_table.status’ will get rid of the the integrity constraint violation.

Magento not magenta

I was wondering today why Magento’s color is orange and not magenta.  I found this thread on MagentoCommerce.com that offers some insight into it.

http://www.magentocommerce.com/boards/viewthread/1334/

Here are a couple of excerpts:

November 5, 2007

We specifically selected Magento (instead of Magenta) because of trademark issues. We felt is was safer.

Roy Rubin 
Magento Team

Magento.com was not available. In any case, we didn’t care much for the domain name – we don’t think it was that critical. People will know how to find us (thanks to Google).

Roy Rubin 
Magento Team
It seems that since they didn’t want to be identified with the color they decided to be orange instead of magenta.

Magento not transferring cart contents to Paypal Express

I recently came across this problem after a server transfer.  Prior to the database move Paypal Express was working.  After the move Magento was not transferring the cart information to Paypal.

To fix this make sure the following setting is correct in Admin -> Configuration -> Paypal -> Express Checkout Settings:

Transfer Cart Line Items: Yes