Browsing articles in "Magento"
Oct 7, 2014
kalpesh

Magento fix for error “Code already exists.”

Fix for Magento error Code already exists. in Sales > Tax > Manage Tax Rules, when saving the rule.

When trying to save Tax rules in Magento, you may get “Code already exists.” error if there are lots of tax rates passing in POST. Magento error is not making sense here, as the issue is something different. Basically when you post the data in PHP, it limits maximum post variables which should pass to the server. For me, that limit was 1000 in max_input_vars, which by changing to 10000 solved this issue and Magento successfully accepted my changes without any error.

To change max_input_vars to higher value, you need to edit in PHP.ini file for max_input_vars like this:

1
max_input_vars 10000

But as editing PHP.ini requires restarting apache to reflect the changes, I did the change in .htaccess file just for Tax rule modification and later reverted it.

You can edit .htaccess to make this change by copy-pasting below line at the end of the file:

1
php_value max_input_vars 10000

Now you should be able to change Tax rules even if you have lots of tax rates posting through for that change! I prefered to revert .htaccess back to what it was as I didn’t need to change tax rules frequently.

Note: If the above does not solve the issue for you, make sure your Tax Rule name is not duplicating with another tax rule name.

Sep 23, 2014
kalpesh

Magento: Show gift card details in admin order page

Magento display gift card details (code and amount) in Order View page of admin panel. When customer uses gift card in their order Magento does not display gift card code and amount details in admin panel which makes it tough to know. Customer can pay partially through gift card which makes even tougher as Magento only shows payment information of other method and not gift card amount deductions.

Below code will show you an additional block in magento admin panel with Gift Card details. If gift card is not used in an order, the block will simply no shown.

Open the file app/design/adminhtml/default/default/template/sales/order/view/edit.phtml

Add the below lines of code anywhere you want the gift card block to appear. I have used it after Account Information block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php $cards = unserialize($_order->getGiftCards());
        if($cards!='' && count($cards)>0) { ?>
<div class="box-left">
    <div class="entry-edit">
        <div class="entry-edit-head">
            <h4 class="icon-head head-account"><?php echo Mage::helper('sales')->__('Gift Cards') ?></h4>
        </div>
        <div class="fieldset">
            <div class="hor-scroll">
                <table cellspacing="0" class="form-list">
                <?php foreach ($cards as $card) { ?>
                <tr>
                    <td class="label"><label><?php echo Mage::helper('sales')->__('Giftcard Code') ?></label></td>
                    <td class="value">
                        <div style="font-weight:bold"><?php echo $card['c'];?></div>
                    </td>
                </tr>
                <tr>
                    <td class="label"><label><?php echo Mage::helper('sales')->__('Giftcard Amount') ?></label></td>
                    <td class="value">
                        <div style="font-weight:bold"><?php echo $card['a'];?></div>
                    </td>
                </tr>
                <?php }?>
                </table>
            </div>
        </div>
    </div>
</div>
<?php }?>
Sep 4, 2014
kalpesh

Magento add/remove product attribute programatically

Magento add product attribute using sql setup file in your module. Also assign your custom attribute to attribute set Default and group General programatically.

Below code will add your new attribute in Manage Products edit screen at the end of General tab with dropdown values Yes/No. It will not display in frontend website but you can change visibible on front to 1 if you wish. Note that it will assign to Default attribute set only but you can change it to whatever as per your requirement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$model = Mage::getResourceModel('catalog/setup','catalog_setup');

$data=array(
'type'=>'int',
'input'=>'boolean', //for Yes/No dropdown
'sort_order'=>50,
'label'=>'CUSTOM ATTRIBUTE CODE LABEL',
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required'=>'0',
'comparable'=>'0',
'searchable'=>'0',
'is_configurable'=>'1',
'user_defined'=>'1',
'visible_on_front' => 0, //want to show on frontend?
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'required'=> 0,
'unique'=>false,
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable
'is_configurable' => false
);

$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data);

$model->addAttributeToSet(
    'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE'
); //Default = attribute set, General = attribute group

To remove the product attribute using the setup file, use below code instead:

1
2
$model = Mage::getResourceModel('catalog/setup','catalog_setup');
$model->removeAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE');
Sep 2, 2014
kalpesh

Magento set session cookie lifetime to 1 day

Is your Magento shopping cart kicking out all the items after 24 minutes? Are you frustrated because your customer’s cart items are getting flushed every few hours even though you have correct setting in Magento? Do you want to increase your shopping cart sessions to last for 1 day (or anything you wish)?

Here are the things to look for to increase/decrease Magento’s session cookie lifetime.

In your php.ini file:

1
session.gc_maxlifetime 86400

If you have not changed this, it should be by default 1440 i.e. 24 minutes. Change it to 86400 for one day session lifetime

In your Magento admin:

1
2
System -> Configuration -> Checkout -> Shopping Cart
Quote Lifetime (days) -> 1
1
2
System -> Configuration -> Web -> Session Cookie Management
Cookie Lifetime -> 86400

Make sure to check if you are in correct website/store if using multi-website/multi-store Magento setup. You can change the scope by changing the website/store from the upper left Store Switcher drop down in System -> Configuration screen.

If you are changing php.ini value, make sure you reload apache to reflect the changes.

Aug 22, 2014
kalpesh

Magento reload top mini cart programatically

Magento reload/refresh top mini cart programatically. This can be useful when you are using ajax to add/remove product to cart and want to reflect that item changes in the top cart immediately. Simply put below code where you are modifying cart programatically and it will start working.

Right now it’s only coded for simple and configurable products, but you can add bundle, group, virtual and downloader product item renderers too if you are using them in your website.

1
2
3
4
5
6
$b = $this->getLayout()
->createBlock('checkout/cart_sidebar')
->addItemRender('simple', 'checkout/cart_item_renderer', 'checkout/cart/sidebar/default.phtml')
->addItemRender('configurable', 'checkout/cart_item_renderer_configurable', 'checkout/cart/sidebar/default.phtml')
->setTemplate('checkout/cart/cartheader.phtml')
->toHtml();
Pages:1234567...25»

Welcome to my Blog

This blog is merely to record the things that I find interesting. It can be technical, humor, news, creative or anything that deserves to be posted here. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications