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();
Aug 22, 2014
kalpesh

Magento auto remove out of stock items from shopping cart

Magento automatically remove product items from shopping cart page which are out of stock. You may need this feature when the situation aries where product goes out of stock and that particular product is already there in other customer’s cart who have not yet checked it out. With this script Magento will auto-check if all the items in the cart are available and in-stock before proceeding for checkout page.

In config.xml file:

1
2
3
4
5
6
7
8
9
10
11
<events>
  <controller_action_predispatch_checkout_cart_index>
    <observers>
      <namespace_module_autoremove_outofstock>
        <type>singleton</type>
        <class>namespace_module/observer</class>
        <method>autoRemoveOutOfStockItems</method>
      </namespace_module_autoremove_outofstock>
    </observers>
  </controller_action_predispatch_checkout_cart_index>
</events>

In Observer.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function autoRemoveOutOfStockItems($observer) {
    $quote = Mage::getModel('checkout/session')->getQuote();
    $cartItems = $quote->getAllItems();
    foreach ($cartItems as $item)
    {
        //$productType = $item->getProduct()->getTypeId();
        //if($productType!='configurable') {
        $productId = $item->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);
        $stockItem = $product->getStockItem();
        if(!$stockItem->getIsInStock())
        {
                Mage::helper('checkout/cart')->getCart()->removeItem($item->getId())->save();
        }
        //}
    }

}
Aug 22, 2014
kalpesh

Magento get CMS page content without header/footer

Magento get CMS page content anywhere on the site without header and footer using below script. You may need to do this for displaying CMS page on part of the page, like popup or sidebar of the page without using header and footer of the CMS page that generally comes when loading the page from the CMS URL.

1
2
3
4
5
6
$page = Mage::getModel('cms/page');
$page->setStoreId(Mage::app()->getStore()->getId());
$page->load('CMS-IDENTIFIER-HERE','identifier'); //EDIT IN THIS LINE
$helper = Mage::helper('cms');
$processor = $helper->getPageTemplateProcessor();
echo $html = $processor->filter($page->getContent());
Pages:«123456789...34»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications