Magento: Added to cart confirmation page

The module we are going to create will make the add to cart button to redirect to an added to cart confirmation page after a product is added to the shopping cart. This module demonstrates the usage of Observer, Controller, Block and template as well as passing variables between observer, block and template.
1. Module installation xml file, MAGEROOT/app/etc/modules/MyExtensions_Checkout.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyExtensions_Checkout>
            <active>true</active>
            <codePool>local</codePool>
        </MyExtensions_Checkout>
    </modules>
</config>

2. Module configuration xml file, it configures the php classes for Block, Controller, and Model, as well as controller url router and frontend template layout file. MAGEROOT/app/code/local/MyExtensions/Checkout/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <MyExtensions_Checkout>
      <version>0.1.0</version>
    </MyExtensions_Checkout>
  </modules>
  <global>
  	<models>
  		<custom_checkout>
  			<class>MyExtensions_Checkout_Model</class>
  		</custom_checkout>
  	</models>
	<blocks>
	  <custom_checkout>
		  <class>MyExtensions_Checkout_Block</class>
	  </custom_checkout>
	</blocks>
  </global>
  <frontend>
    <routers>
      <custom_checkout>
        <use>standard</use>
          <args>
            <module>MyExtensions_Checkout</module>
            <frontName>custom_checkout</frontName>
          </args>
      </custom_checkout>
    </routers>
	<layout>
	  <updates>
  		<custom_checkout>
  		  <file>custom_checkout.xml</file>
  		</custom_checkout>
	  </updates>
	</layout>
	<events>
	   	<checkout_cart_add_product_complete>
	   		<observers>
	   			<MyExtensions_Checkout_Model_Observer>
	   				<type>singleton</type>
	   				<class>MyExtensions_Checkout_Model_Observer</class>
	   				<method>addToCartConfirmation</method>
	   			</MyExtensions_Checkout_Model_Observer>
	   		</observers>
	   	</checkout_cart_add_product_complete>
    </events>
  </frontend>

</config>

3. Controller class, controls the frontend page based on the url. In this case, the frontend url /custom_checkout/add/confirm will trigger this controller. MAGEROOT/app/code/local/MyExtensions/Checkout/controllers/AddController.php

<?php
//custom_checkout/add/confirm
//customer_checkout defined in the module config.xml, <frontName>custom_checkout</frontName>
//add corresponds to the Add of the AddController.php
//confirm corresponds to the confirm of the function confirmAction in the AddController class.
class MyExtensions_Checkout_AddController extends Mage_Core_Controller_Front_Action{
    public function confirmAction() {
  	  $this->loadLayout();

  	  //gets the head block and do the modifications 
   	  $this->getLayout()->getBlock("head")->setTitle($this->__("Added To Cart"));
  	  $breadcrumbs = $this->getLayout()->getBlock("breadcrumbs");
        $breadcrumbs->addCrumb("home", array(
                  "label" => $this->__("Home Page"),
                  "title" => $this->__("Home Page"),
                  "link"  => Mage::getBaseUrl()
  		   ));

        $breadcrumbs->addCrumb("added to cart", array(
                  "label" => $this->__("Added To Cart"),
                  "title" => $this->__("Added To Cart")
  		   ));
  	  
        $this->renderLayout(); //<file>custom_checkout.xml</file> controls what to display on the frontend.
    }
}
?>

4. Observer class, observes the checkout_cart_add_product_complete event, and redirect it to the added to cart confirm page on this event. MAGEROOT/app/code/local/MyExtensions/Model/Observer.php

<?php
class MyExtensions_Checkout_Model_Observer
{
	function addToCartConfirmation(Varien_Event_Observer $observer)
	{
		$product = $observer->getEvent()->getProduct();
		
		$response = $observer->getResponse();
		$request = $observer->getRequest();
		$response->setRedirect(Mage::getUrl('custom_checkout/add/confirm'));//redirection happens here
		
		//Varien magic setter, we can get the value back Mage::getSingleton('checkout/session')->getNoCartRedirect(); and Mage::getSingleton('checkout/session')->getLastAddedItemQty();
		Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
		Mage::getSingleton('checkout/session')->setLastAddedItemQty($product->getQty());
	}
}
?>

5. Block class, it provides logics and functionalites for the frontend template. $this in the template for this block corresponds to this class. MAGEROOT/app/code/local/MyExtensions/Checkout/Block/AddConfirm.php

<?php   
class MyExtensions_Checkout_Block_AddConfirm extends Mage_Core_Block_Template{   
		
	
	 // Retrieve just added to cart product object 
	 // @return Mage_Catalog_Model_Product
	public function getLastAddedProduct()
	{
		$productId=Mage::getSingleton('checkout/session')->getLastAddedProductId();
		
		if ($productId) {
			$product = Mage::getModel('catalog/product')->load($productId);
			return $product;
		} else {
			return false;
		}
	}
	
	public function getCartSubtotal()
	{
		$cartQuote=Mage::getSingleton('checkout/session')->getQuote();
		if($cartQuote)
		{
			return Mage::helper('checkout')->formatPrice($cartQuote->getSubtotal());
		}
		else
		{
			return false;
		}
	}
	
	public function getCartItemCount()
	{
		$cart=Mage::helper('checkout/cart');
		if($cart)
		{
			return $cart->getSummaryCount();
		}
		else
		{
			return false;
		}
	}

}
?>

6. Template layout xml file, it controls what to display on the frontend html page and it was defined in the module config.xml above. MAGEROOT/app/design/frontend/default/default/layout/custom_checkout.xml

<?xml version="1.0"?>   
<layout version="0.1.0">   
  <custom_checkout_add_confirm>   
    <reference name="root">   
      <action method="setTemplate"><template>page/1column.phtml</template></action>   
    </reference>   
    <reference name="content">   
      <block type="custom_checkout/AddConfirm" name="add_product_confirm" template="custom/addConfirm.phtml"/>
    </reference>   
  </custom_checkout_add_confirm>   
</layout> 

7. The template phtml file that does the last rendering of the added to cart confirmation page. MAGEROOT/app/design/frontend/default/default/template/custom/addConfirm.phtml

<?php 
//$this @ MyExtensions_Checkout_Block_AddConfirm
$product=$this->getLastAddedProduct();
$lastAddedProductQty=Mage::getSingleton('checkout/session')->getLastAddedItemQty();
?>
<h1 id='confirm-prod-msg'><?php echo $lastAddedProductQty;?> item has been added to your cart</h1>
<?php
echo '<img id="add-confirm-img" style="height:400px;float:left;"src="'.Mage::helper('catalog/image')->init($product, 'image')->resize(310, 600, false, true).'" />';
?>
<ul id='add-confirm-info' style="float:left;">
<li><h5><a href="<?php echo $product->getProductUrl();?>"'><?php echo $product->getName();?></a></h5></li>
<li><span><?php echo $product->getFormatedPrice();?></span></li>
<li><span>Qty: <?php echo $lastAddedProductQty;?></span></li>
</ul>

<div id="cart-details" style="float:left;margin-left:30px;">
<legend style="color:blue;font-weight:bold;">Cart Details</legend>
<ul>
<li>Order Subtotal: <?php echo $this->getCartSubtotal();?></li>
<li><?php echo $this->getCartItemCount();?> items in your cart</li>
<li><a href="<?php echo Mage::getBaseUrl();?>"><span>Continue Shopping</span></a></li>
<li><a href="<?php echo $this->getUrl('checkout/cart');?>"><span>Checkout</span></a></li>
</ul>
</div>

Search within Codexpedia

Custom Search

Search the entire web

Custom Search