Browsing articles in "Magento"
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.

$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:

<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:

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.

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

Magento event to check if customer have subscribed to newsletter

Check if the customer has subscribed to the newsletter from registration page or checkout page by using event observer. You may need to take some action programatically if customer subscribes to newsletter, below code will help you exactly in that.

Code to put in your config.xml

<newsletter_subscriber_save_after>
  <observers>
    <namespace_module_model_observer>
      <class>Namespace_Module_Model_Observer</class>
      <method>subscribedToNewsletter</method>
    </namespace_module_model_observer>
  </observers>
</newsletter_subscriber_save_after>

Code to put in your Observer.php file

class Namespace_Module_Model_Observer {
        public function subscribedToNewsletter(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $subscriber = $event->getDataObject();
        $data = $subscriber->getData();
        $email = $data['subscriber_email'];
        
        
        $statusChange = $subscriber->getIsStatusChanged();
        if ($data['subscriber_status'] == "1" && $statusChange == true) {
                        //code to handle if customer is just subscribed...
                }
        }
}
Aug 21, 2014
kalpesh

Magento event observer for customer registration success

If you are looking for how to execute some code when customer successfully sign up in your website, you can use below code to check if the registration was successful. Note, this will NOT check if customer was registered from checkout page, if you are looking for that please go to this post.

In your xml file:

<events>
  <customer_register_success>
    <observers>
      <namespace_module_customer_register_success>
        <type>singleton</type>
        <class>Namespace_Module_Model_Observer</class>
        <method>customerRegisterSuccess</method>
      </namespace_module_customer_register_success>
    </observers>
  </customer_register_success>
</events>

And in your Observer.php file:

class Namespace_Module_Model_Observer {
  public function customerRegisterSuccess(Varien_Event_Observer $observer) {
      $event = $observer->getEvent();
      $customer = $event->getCustomer();
      $email = $customer->getEmail();
      if($email) {
          //code to handle if customer is successfully registered
      }
  }
}
Pages:«123456789...29»

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

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions