How to access Magento core inside a custom build

Posted on

Estimated reading time: 1 minute, 12 seconds

We have recently been working on a few Magento Ecommerce sites that either incorporate wordpress or a custom build using the Yii framework. If you, like us; have done custom builds like this you find yourself sometimes wanting to access Magento core functionality in your other pages.

Using the below code you can easily access your Magento core

<?php
   require_once('app/Mage.php'); // Path to Magento
   umask(0);
   Mage::app();

   // Target a specific product by ID (7 in our case)
   $_product= Mage::getModel()->load(7);
   echo->getName();

Another quick example would be to access your Magento navigation inside your custom built project:

<?php
   require_once('app/Mage.php'); // Path to Magento
   umask(0);
   Mage::app();
   
   // Include Magento left navigation
   $_layout  = Mage::getSingleton('core/layout');
   $_block   = $_layout->createBlock('catalog/navigation')->setTemplate('catalog/navigation/left.phtml');
 
   echo $_block->toHtml();

Another one we found ourselves using a lot was the inclusion of the basket into our WordPress site:

<?php
   require_once('app/Mage.php'); // Path to Magento
   umask(0);
   Mage::app();
   
   // Display shopping cart content
   $cart = Mage::getModel('checkout/cart')->getQuote();
   foreach ($cart->getAllItems() as $item) {
      $productName = $item->getProduct()->getName();
      $productPrice = $item->getProduct()->getPrice();
   }

As we said, these are merely a few examples but it should give you an idea of how to access most other core functionality you might be after.

Web Development

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top