Monday 31 October 2011

Magento: Add to cart button every where

Sometime you need to add “Add to cart” button outside the product details view. So, you can’t use
getAddToCartUrl($product)

Don’t worry, you can use this code instead for:
echo Mage::helper('checkout/cart')->getAddUrl($_item)

Monday 24 October 2011

Product images not uploading

When trying to upload an image for the product the section with the image name gets a red border and an error message appears.
When trying to upload something for the downloadable products same error found.


Here is what I did to fix it:
The problem seems to be caused by the prototype v1.7.
It works great with prototype 1.6
Get the 1.6 version from here http://prototypejs.org/assets/2009/8/31/prototype.js
or get it from older magento version.


Create this file: js/prototype/prototype1.6.js and put the contents from the link above in it.
Now edit app/design/adminhtml/default/default/layout/main.xml and change this line

prototype/prototype.js
with
prototype/prototype1.6.js


I don’t think it’s a problem if you edit this core file.  


I hope this helps someone

Friday 21 October 2011

Magento: this.images.toJSON is not a function

If you can’t upload your images to product. And you get this error: {Object}.toJSON is not a function in firebug console. This issue will be fix easily if you add $H() to your object.

var myData = this.images.toJSON();


replace by:
   
var myData = $H(this.images).toJSON();

Thursday 20 October 2011

Magento 1.5 admin - Categories are not appearing when editing products

I have similar pb in all my websites in 1.5 
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php
i have changed it by the 1.4.2 code and yeahh; it works fine. Categories are auto expanded  change the last function by this code.
public function getSelectedCategoriesPathIds($rootId = false)
    {
        $ids = array();
        $collection = Mage::getModel('catalog/category')->getCollection()
            ->addFieldToFilter('entity_id', array('in'=>$this->getCategoryIds()));
        foreach ($collection as $item) {
            if ($rootId && !in_array($rootId, $item->getPathIds())) {
                continue;
            }
            foreach ($item->getPathIds() as $id) {
                if (!in_array($id, $ids)) {
                    $ids[] = $id;
                }
            }
        }
        return $ids;
    }

Wednesday 19 October 2011

Remove “+$” or “-$” price value in the drop down in configurable product

If you want to remove "+$" or "-$" price value in the drop down in configurable product.

STEP 1
app/code/core/Mage/Catalog/Block/Product/View/Type/ Configurable.php

change this line
'template'  => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
TO
'template'          => Mage::helper('catalog')->__(''),

STEP 2
js/varien/product.js
change this function
formatPrice: function(price, showSign){ 
    var str = ''; 
    price = parseFloat(price); 
    if(showSign){ 
    if(price<0){ 
    str+= '-'; 
    price = -price; 
    } 
    else{ 
    str+= '+'; 
    } 
} 
TO
formatPrice: function(price, showSign){ 
var str = ''; 
price = parseFloat(price); 
if(showSign){ 
if(price < 0){ 
str+= ''; 
price = -price; 
} 
else{ 
str+= ''; 
} 
}

Magento ParadoxLabs Gallery Lightbox not work on ie9

I used to spend a lot more time hacking websites to work with paradoxlabs gallery in Magento but won't work in – IE9. 

Finally i got solution for that. i just upgrade prototype.js to newer version which is located in to js/prototype/prototype.js

you can find the file from below link.

it work for me. its works like a charm.

Using collection in Magento for database manipulation

Basically a collection is a model type that contains other model.In other words we can say a collection is basically a set of models in magento. These Models are used to manipulate database as per our requirement.

With the collection we will get the data from database. Here is the simplest way to do this.
//  Creating model object on which we want to do manipulation
$model = Mage::getModel("customer/account_history");
 
// Generating Collection of above model.
 $collection = $model->getCollection();
 
//We are adding filter on collection that
// we want the data of currently logged in user only.
$collection->addCustomerFilter(Mage::registry('current_customer')->getId());
 
//Adding where condition into the query
 $collection->addFieldToFilter('status', array('neq' => '0'));
 
// Adding order by clause to the mysql query through collection
$collection->getSelect()->order(new Zend_Db_Expr('created_time DESC'));
 
// Adding limit to mysql query through collection
$collection->getSelect()->limit(100);
 
//Below code will  print full mysql query for you so that you can see it
echo $collection->getSelect();
 
// Now use your collection. Simply loop through it
$this->setCollection($collection);
Now you can use this above collection to get your intended data
// Looping collection to get data
foreach($collection as $product) {
var_dump($product->getData());
}

Tuesday 18 October 2011

Fatal error: Call to undefined method Mage_Adminhtml_Block_Widget::getrowurl()

If you getting blank pages or fatal errors on MANAGE CATEGORIES page, then here is the solution :
Go To app/code/core/Mage/Adminhtml/Block/Widget/Grid.php

Search function getRowUrl in the file.
public function getRowUrl($item)
{
$res = parent::getRowUrl($item);
return ($res ? $res : '#');
}
Replace this function with below function:
public function getRowUrl($row) 
{ 
return $this->getUrl('*/*/edit', array('instance_id' => $row->getId())); 
}

Permanently change the ‘view x items per page’ in the admin backend from the default of 20

In sections like CMS->Static Blocks, CMS->Pages, Catalog->Manage Product etc.  if you have more than 20 items magento will split them into multiple pages of results.

The fix itself is pretty easy, just open up:
app/code/core/Mage/Adminhtml/Block/Widget/Grid.php

Somewhere near the top of the file (around line 70) you'll find the following code:
protected $_defaultLimit    = 20;
Replace it with: 
protected $_defaultLimit    = 999;
As your updating a core file within Magento this change will be overwritten every time you upgrade your magento so you'll have to go back and alter it again after each upgrade. 

If you want to add new page no to pager than open file 
app\design\adminhtml\default\default\template\widget\grid.phtml

Somewhere in the file (around line 81) you'll find the select tag code and copy option line and paste with your desire page no.

Sunday 9 October 2011

Magento Code to get General Store Information


Most of the time we want to access Store Contact info at different places of our magento site to do this we have a very simple and useful code i.e
echo Mage::getStoreConfig('general/store_information');
This code will fetch you an array containing complete store which is provided at backend From System->Configuration->General->Store Information. use address
echo Mage::getStoreConfig('general/store_information/address'); 
use phone no
echo Mage::getStoreConfig('general/store_information/phone'); 
or you can get using
$info = Mage::getStoreConfig('general/store_information');
store name : $info['name']
store address : $info['address']
store Phone : $info['phone']