Browsing articles in "Magento frontend"
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
      }
  }
}
Aug 21, 2014
kalpesh

Magento check if customer registered in checkout page

If you want to check if the customer is guest, registered or just register to the site when they place the order, below script will help you identify that in success.phtml file.

You can find success / order confirmation phtml file at:
app/design/frontend/[package]/[theme]/template/checkout/success.phtml

Just at the end of this file put below lines of code:

<?php $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$quoteId = $order->getQuoteId();
$quote = Mage::getModel('sales/quote')->load($quoteId);
$method = $quote->getCheckoutMethod(true);
$customer_email = $order->getCustomerEmail();
if ($method == 'register'){ ?>
//code to handle if customer just registered to your site
<?php } elseif($method == 'guest') {?>
//code to handle if customer is guest
<?php } else { ?>
//code to handle for logged in customer
<?php } ?>

In the same file, success.phtml, you can request for order number, customer email, customer id, subtotal, grandtotal, order ID just created etc. like this:

$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$_totalData =$order->getData();
$_sub = $_totalData['subtotal'];
$_orderEmail = $_totalData['customer_email'];
$_orderNumber = $_totalData['increment_id'];
Jan 5, 2014
kalpesh

Magento continue shopping link to last added product’s category page

In Magento checkout cart, link Continue shopping button to last added product’s category page. Below code will check last added product’s id in cart and gets the last category assigned to the product.

Put below code at the start of the checkout/cart.phtml file

$lastProductIdAddedToCart = Mage::getSingleton('checkout/session')->getLastAddedProductId();
if($lastProductIdAddedToCart) {
    $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductIdAddedToCart)->getCategoryIds();
    //print_r($productCategoryIdsArray);    
    $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load(end($productCategoryIdsArray))->getUrl();
}

Replace continue shopping button with below code, in the checkout/cart.phtml file

<?php if($this->getContinueShoppingUrl()): ?>
    <button type="button" title="<?php echo $this->__('Continue Shopping') ?>" class="button btn-continue" onclick="setLocation('<?php echo (isset($continueShoppingCategoryUrl)) ? $continueShoppingCategoryUrl : $this->getContinueShoppingUrl(); ?>')"><span><span><?php echo $this->__('Continue Shopping') ?></span></span></button>
<?php endif; ?>
Pages:«1234567...14»

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