Let's talk Contact us. No cost. No obligation.

Fill out this form and we will contact you with in 24 hrs.

    captcha

     

    Knowledge Base

    How to use the magento block in other application

    May 4, 2017

    Presently, many entrepreneurs are using ecommerce business with several other non ecommerce applications to make their presence online. Since magento have MVC architecture, its fairly simple to grab the magento block and include them in other PHP applications.

    Example of using magento block in a non-magento application is magento wordpress plugin integration. We can use it with other application with joomla, drupal and other cms as per our requirements.

    There are two main thing that we need to manage.
    1-Initialize the magento
    2-include the block

    Initialize the magento

    We need to do these step only once,preferably earlier in the execution of the application.We are going to load the magento bootstrap file and load the magento framework

    include ‘/path/to/magento/app/Mage.php’;
    umask(0);
    Mage::app();

    Now there is few extra step,that way we can correctly use its blocks

    Mage::getSingleton(‘core/translate’)->setLocale(Mage::app()->getLocale()->getLocaleCode())->init(‘frontend’, true);
    Mage::getSingleton(‘core/session’, array(‘name’ =’frontend’));
    Mage::getSingleton(“checkout/session”);
    Mage::getDesign()
    // Set the package, usually “default” or “enterprise”
    ->setPackageName(‘enterprise’)
    // Set the theme, usually “default”
    ->setTheme(‘default’);
    $layout = Mage::app()->getLayout();
    $module = Mage::app()->getRequest()->getModuleName();
    if (!$module)
    {
    $customerSession = Mage::getSingleton(‘customer/session’);

    $layout->getUpdate()->addHandle(‘default’)->addHandle($customerSession->isLoggedIn()? ‘customer_logged_in’: ‘customer_logged_out’)->load();
    $layout->generateXml()->generateBlocks();

    }

    That’s it magento is now loaded and we are ready to include the block.

    Including the layout blocks

    To include a block created through Magento’s layout system all you need is the name. For instance, we can find the Magento header in layout/page.xml












    top-container

    Now we have the name like “header” that we can include the block as

    echo $layout->getBlock(‘header’)->toHtml();

    Including Static Blocks

    Including a CMS static block is very simple. All you need is the identifier. For instance, if we had a block called “home-slider”

    echo $layout->createBlock(‘cms/block’)->setBlockId(‘home-slider’)->toHtml();