Wednesday, August 22, 2012

Magento : Allow tags (iframe, embed) in CMS editor

Add following code in file: js/mage/adminhtml/wysiwyg/tiny_mce/setup.js
after line
theme_advanced_resizing : true,


extended_valid_elements : 'iframe[src|style|width|height|scrolling|marginwidth|marginheight|frameborder],style,script',

Friday, August 17, 2012

Magento : Enablle specific payment method for specific IP address

Consider module as Bd_Testpament
Add a filed for Ip in system.xml with follwoing code
<config>
   <sections>
        <payment>
            <groups>
                <testpayment translate="label comment" module="testpayment">
                    <label>Test</label>
                    <comment>Test</comment>
                    <frontend_type>text</frontend_type>
                    <sort_order>256</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>0</show_in_store>
                    <fields>
            <allowed_ips translate="label">
            <label>Allowed Ips</label>
            <frontend_type>text</frontend_type>
            <sort_order>130</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
            </allowed_ips>
                    </fields>
                </secureebsoffer_standard>
            </groups>
        </payment>
    </sections>
</config>

And edit __construct of model with following code

public function __construct()
    {
        parent::__construct();
       
        $allowed_ips=Mage::getStoreConfig('payment/testpayment/allowed_ips');
       
        if($allowed_ips !='')
        {
       
            if(!in_array($_SERVER['HTTP_X_FORWARDED_FOR'],explode(",",$allowed_ips)))
            {
                $this->_canUseCheckout=false;
               
            }
        }
        else
        {
                $this->_canUseCheckout=false;
        }

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.