Magento Gift Messages – How to enable Magento’s gift messages on order and order item levels

Saturday, 30. July 2011

One of the nice improved features on the latest stable release of Magento (1.5.1.0) is the Gift Messages.

To some stores this is could be a really nice feature to implement on your store and especially because it just out of the box!

To enable Gift Message on your store:

– Login as Admin and navigate to: System > Configuration > Sales > Gift Messages.

– Click to open Gift Options section

– Select your appropriate Option

Magento Admin Login Failure – Can not login to Magento Admin Page

Saturday, 30. July 2011

I have recently come across what appeared to be a rather strange issue that I never seen before.

Basically, I did move the site from a client’s server to out develepment environment and everything seemed to be OK. The moment i tried to login to the admin page, the page was simply not doing anything. No errors, No warnings! Just seemed to try to redirect and fails.

After a little head-scratching moments, I realized it was the cookies that were causing the indecency!

Basically, the client did specify the cookie domains to use for his admin login and since my development domain didn’t match, I simply couldn’t login.

To resolve the issue (the easy way i think) is to connect to MySql database using either PhpMyadmin or Mysql Query analyzer and look for the config_data table

Search for all paths with “cookie” data – something like this.

SELECT * FROM `core_config_data` WHERE `path` LIKE '%cookie%'

Remove the domain entries. All should be good to go now.

Magento Index Management – catalogsearch_fulltext table crashed and needs repairing

Saturday, 30. July 2011

If you have been running Magento’s index management and suddenly came across this error like I did, here is a quick and nice solution that will help you.

Use PhpMyadmin or MySql Query analyser

You will need access to your database to perform this solution. You can also use your favorite GUI DB interface if you want.

1) Connect to your database.

2) Because catalogsearch_fulltext is a MyISAM type table, you can use this query (you can change options are you see fit) and replace table_name to your table name. Then Run

-- Syntax: REPAIR TABLE table_name [,table2, table3 ...] [options]
REPAIR TABLE table_name;

Available options are

QUICK – It’s the quick way and data file is not modified.

USE_FRM – If for some reason the .MYI is missing or corapted and the plan REPAIR TABLE query fails, you can add this option so MySql can rebuild the indexes for you.

EXTENDED – I will use this as a very last resort. This option will try to repair every single row it comes across with and in the end, it may result to some Gibraltar stuff on your table!

Click this link to learn more about: REPAIR TABLE

From a command line utility

There are also commands to run on your terminal if so preferred. I will not outline exactly how to do this but there is a more useful information already on the following links.

myisamchkClick here to learn more

mysqlcheckClick here to learn more.

Magento and Currencies – Get the correct currency symbol and code programatically

Thursday, 28. July 2011

In e-commerce business and business in general, you have got to get the currencies right or you are doomed!

In a multi-store environment, it is even more important to be able to automatically change the currency symbols for your current customers depending on their location. This is even more true, if you are doing international business.

Here are my tips on how to get the correct currency code and symbol per store.

Get currency symbol for current store:

Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();

Get currency symbol for a specify currency code:

  Mage::app()->getLocale()->currency('EUR')->getSymbol();

Get currency code for current store:

Mage::app()->getStore()->getCurrentCurrencyCode();

ATTENTION:
You will have to set the correct locale in the backend to ensure Magento works correctly. Here is how to do it.

1) Navigate to: System > Configuration > General > Locale.

2) Choose the Timezone and Locale for each store.

Show All Category Products By Default In Magento Programatically

Thursday, 28. July 2011

In order to show all products in a category by default you can follow this approach:

1) Overwrite the Core toolbar class: Mage_Catalog_Block_Product_List_Toolbar.php

2) From the base class, copy and override the function getLimit() in your new class. (method starts around line 723)

3) In your overridden function, Look for a line:

$limit = Mage::getSingleton('catalog/session')->getLimitPage();

And replace it with

$limit = 'all';

You can now clear your Magento cache in order to register your new extension and have fun testing your view all products on a category page by default!

Magento Checkout – “Unable to set Payment Method”

Thursday, 28. July 2011

I have come across this rather ambiguous error message while testing the checkout process with onepage checkout.

It’s a popup with a simple message “Unable to set Payment Method”! That’s very telling!

The truth is that Magento does in deed throw an error and it’s captured. You can quickly allow the full error to be displayed on frontend by doing the following:

Navigate to: /app/code/core/Mage/Checkout/controllers/OnepageController.php

Search for a line

$result['error'] = $this->__('Unable to set Payment Method.');

and replace it with

$result['error'] = $e->getMessage();

Try the checkout again and hopefully you should now see a more meaningful error message for a developer!

ATTENTION:
I think you have noticed you have just edited the very CORE file here! I would normally not recommend doing this. It’s much better to overwrite the controller instead.

I will leave it to your judgement but I will recommend you atleast undo your changes after you done fixing your problem.

Magento: How to save session data in Magento

Thursday, 28. July 2011

If you have ever wondered, this is how you can easily save data in Magento session

# Initiate session object
$session = Mage::getSingleton("core/session",  array("name"=>"your_session_name"));
#set the actual data:
$session->setData("state_id", 454);
# Access your session data:
$state_id = $session->getData("state_id");

Ubuntu: Restart Samba service from command line

Wednesday, 27. July 2011

Here is a little command you will need to be able to restart Samba service from a command line on ubuntu.

sudo service smbd start

You can also use this command

sudo /etc/init.d/smbd start

Enjoy!

Magento: Add an OR condition to addAttributeToFilter method programatically

Tuesday, 26. July 2011

If you have ever wondered how to get an OR clause on Magento while pulling a certain collection this post may help you!

The easy way is to use the IN clause and pass through an array of values.

 
        $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->addCategoryFilter($category)
        # add and OR condition on material
        ->addAttributeToFilter('material', array('IN' => array(val1, val2, val3,...)))
        ->load();

You will then get something that looks similar to this output

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, IF(_table_material.value_id>0, _table_material.VALUE, _table_material_default.VALUE) AS `material` FROM `mag_catalog_product_entity` AS `e` INNER JOIN `mag_catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.category_id='367' AND cat_index.is_parent=1 INNER JOIN `mag_catalog_product_entity_int` AS `_table_material_default` ON (_table_material_default.entity_id = e.entity_id) AND (_table_material_default.attribute_id='122') AND _table_material_default.store_id=0 LEFT JOIN `mag_catalog_product_entity_int` AS `_table_material` ON (_table_material.entity_id = e.entity_id) AND (_table_material.attribute_id='122') AND (_table_material.store_id='1') WHERE (((((IF(_table_material.value_id>0, _table_material.VALUE, _table_material_default.VALUE) = '138')<strong> OR </strong>(IF(_table_material.value_id>0, _table_material.VALUE, _table_material_default.VALUE) = '134')))))

The method accept arrays of attributes as well and this can be great for making your code neater like this:

$collection->addAttributeToFilter(
  array(
      array('attribute'=>'material', 'eq'=>'silk'),
      array('attribute'=>'brand', 'neq'=>'13')
    )
  );

You can also tell Magento what kind of join to use by supplying the third argument of the method addAttributeToFilter like so

$collection->addAttributeToFilter(
  array(
      array('attribute'=>'material', 'eq'=>'silk'),
      array('attribute'=>'brand', 'neq'=>'13')
    ),
    '',
    'left'
  );

Magento Fatal Error : Call to undefined method Mage_Customer_Model_Entity_Attribute_Collection ::addFilterToMap()

Saturday, 23. July 2011

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

If you ever come across this annoying error while upgrading Magento from 1.4.* to latest versions the issues can be resolved by overwriting these classes

1) Mage_Sales_Model_Mysql4_Order_Collection /*Fixes issue on the backend*/

2) Mage_Eav_Model_Mysql4_Entity_Attribute_Collection /*Fixes the issue on basket page*/

and add the following script to each

public function addFilterToMap($filter, $alias)
{
	if (!is_array($this->_mappedFields)) {
		$this->_mappedFields = array();
	}
	$this->_mappedFields[$filter] = $alias;
	return $this;
}
 
public function _getMappedField($field)
{
	if (!isset($this->_mappedFields[$field])) {
		return $field;
	}
	return $this->_mappedFields[$field];
}