Wednesday, August 8, 2012

Magento : sharing the cart between stores

The first thing enable the Use SID on Frontend at
System→Configuration→Web→Session

Then replace following code in start() method of Mage_Core_Model_Session_Abstract_Varien
if (isset($_SESSION)) {
            return $this;
        }

with
if (isset($_SESSION) && !isset($_GET['SID'])) {
            return $this;
        }

Magento : MassDelete & Status action in admin grid

Consider Package: Bd, Module : Test & Model : Test

1.Override _prepareMassaction() method in Grid.php with following code

protected function _prepareMassaction()
    {
        $this->setMassactionIdField('test_id');
        $this->getMassactionBlock()->setFormFieldName('test');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'    => Mage::helper('test')->__('Delete'),
             'url'      => $this->getUrl('*/*/massDelete'),
             'confirm'  => Mage::helper('test')->__('Are you sure?')
        ));

        $statuses = Mage::getSingleton('test/status')->getOptionArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));
        $this->getMassactionBlock()->addItem('status', array(
             'label'=> Mage::helper('test')->__('Change status'),
             'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
             'additional' => array(
                    'visibility' => array(
                         'name' => 'status',
                         'type' => 'select',
                         'class' => 'required-entry',
                         'label' => Mage::helper('test')->__('Status'),
                         'values' => $statuses
                     )
             )
        ));
        return $this;
    }


2.Create two action in admin controller with following code
public function massDeleteAction() {
        $testIds = $this->getRequest()->getParam('test');
        if(!is_array($testIds)) {
   Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
        } else {
            try {
                foreach ($testIds as $testId) {
                    $test = Mage::getModel('test/test')->load($testId);
                    $test->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted', count($testIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

    public function massStatusAction()
    {
        $testIds = $this->getRequest()->getParam('test');
        if(!is_array($testIds)) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
        } else {
            try {
                foreach ($testIds as $testId) {
                    $test = Mage::getSingleton('test/test')
                        ->load($testId)
                        ->setStatus($this->getRequest()->getParam('status'))
                        ->setIsMassupdate(true)
                        ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d record(s) were successfully updated', count($testIds))
                );
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

Object Expected error in Internet Explorer

This error usually happens in the following scenario. An HTML file contains an inline JavaScript to load remote.js and call a function in remote.js

<script type="text/javascript">
//<![CDATA[
    Event.observe(window, 'load', function() {
        product_zoom = new Product.Zoom('image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint');
    });
//]]>
</script>

And in the file remote.js:
remoteFunction code.....


You would expect the output in this order:

    Before loading remote.js
    After loading remote.js
    In remote function

IE gives an error message "Object Expected" because it calls the function remoteFunction() prematurely. It calls before loading the file remote.js, which contain the declaration of remoteFunction(). This is another unexpected behavior from IE. Fortunately, it can be corrected by adding the attribute defer="defer" to second script invocation. This will specifically prevent IE from executing in order to give a correct output. Other browsers are not affected by the change.

Tuesday, August 7, 2012

Magento: Tag Url Rewrite and Generate

Rewrite the tag/tag model and override the getTaggedProductsUrl() with the following code
public function getTaggedProductsUrl()
{
    $fullTargetPath = Mage::getUrl('tag/product/list', array(
        'tagId' => $this->getTagId(),
        '_nosid' => true
    ));
    $targetPath = substr($fullTargetPath, strlen(Mage::getBaseUrl()));
    $rewriteUrl = Mage::getModel('core/url_rewrite')->loadByIdPath($targetPath);
    if ($rewriteUrl->getId()) {
        return $rewriteUrl->getRequestPath();
    }
    return $fullTargetPath;
}

Friday, July 20, 2012

Magento : Define Global, Website, Store, Store View

    Global: This refers to the entire installation.
    Website: Websites are ‘parents’ of stores.  A website consists of one or more stores. Websites can be set up to share customer data, or not to share any data
    Store (or store view group): Stores are ‘children’ of websites.  Products and Categories are managed on the store level.  A root category is configured for each store view group, allowing multiple stores under the same website to have totally different catalog structures.
    Store View: A store needs one or more store views to be browse-able in the front-end.  The catalog structure per store view will always be the same, it simply allows for multiple presentations of the data in the front.  90% of implementations will likely use store views to allow customers to switch between 2 or more languages.

Magento : get file paths and URLs

Get URL paths of your magento folder structure – Absolute URL Path

Mage::getBaseUrl() => Gets base url path e.g. http://my.website.com/

Mage::getBaseUrl(‘media’) => Gets MEDIA folder path e.g. http://my.website.com/media/

Mage::getBaseUrl(‘js’) => Gets JS folder path e.g. http://my.website.com/js/

Mage::getBaseUrl(‘skin’) => Gets SKIN folder path e.g. http://my.website.com/skin/

Get DIRECTORY paths (physical location of your folders on the server) – Relative URL Path

Mage::getBaseDir() => Gives you your Magento installation folder / root folder e.g. /home/kalpesh/workspace/magento

Mage::getBaseDir(‘app’) => Gives you your Magento’s APP directory file location e.g. /home/kalpesh/workspace/magento/app

Mage::getBaseDir(‘design’) => Gives you your Magento’s DESIGN directory file location e.g. /home/kalpesh/workspace/magento/design

Mage::getBaseDir(‘media’) => Gives MEDIA directory file path

Mage::getBaseDir(‘code’) => Gives CODE directory file path

Mage::getBaseDir(‘lib’) => Gives LIB directory file path

Get Current URL – whole URL path

Mage::helper(‘core/url’)->getCurrentUrl()

Also we can use as

base Mage::getBaseDir()
Mage::getBaseDir(‘base’) /var/www/magento/
app Mage::getBaseDir(‘app’) /var/www/magento/app/
code Mage::getBaseDir(‘code’) /var/www/magento/app/code
design Mage::getBaseDir(‘design’) /var/www/magento/app/design/
etc Mage::getBaseDir(‘etc’) /var/www/magento/app/etc
lib Mage::getBaseDir(‘lib’) /var/www/magento/lib
locale Mage::getBaseDir(‘locale’) /var/www/magento/app/locale
media Mage::getBaseDir(‘media’) /var/www/magento/media/
skin Mage::getBaseDir(‘skin’) /var/www/magento/skin/
var Mage::getBaseDir(‘var’) /var/www/magento/var/
tmp Mage::getBaseDir(‘tmp’) /var/www/magento/var/tmp
cache Mage::getBaseDir(‘cache’) /var/www/magento/var/cache
log Mage::getBaseDir(‘log’) /var/www/magento/var/log
session Mage::getBaseDir(‘session’) /var/www/magento/var/session
upload Mage::getBaseDir(‘upload’) /var/www/magento/media/upload
export Mage::getBaseDir(‘export’) /var/www/magento/var/export

Thursday, July 19, 2012

Magento : add thumnail in admin grid

Remember that grid is working fine.Here module is consider as Bd_Collegeprogram

First add the code in the _prepareColumns() function
$this->addColumn("image", array(
                "header" => Mage::helper("collegeprogram")->__("Image"),
                "index" => "image",
                'type' => 'image',
                'width' => '50px',
                'renderer'  => 'collegeprogram/adminhtml_collegeprogram_renderer_image'
                ));
Now create a block as adminhtml_collegeprogram_renderer_image
class Bd_Collegeprogram_Block_Adminhtml_collegeprogram_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{

    public function render(Varien_Object $row)
    {
        $html = '<img ';
        $html .= 'id="' . $this->getColumn()->getId() . '" ';
        $html .= 'src="' . $row->getData($this->getColumn()->getIndex()) . '"';
        $html .= 'class="grid-image ' . $this->getColumn()->getInlineCss() . '"/>';
        return $html;
    }
}