0

I'm trying to access magento from wordpress. Here below is the simple code I used to access magento sidebar cart.

<?php

/*
 * Initialize magento.
 */
require_once '/Applications/XAMPP/xamppfiles/htdocs/conover-store/app/Mage.php';
Mage::init();

/*
 * Add specific layout handles to our layout and then load them.
 */
$layout = Mage::app()->getLayout();
$layout->getUpdate()
    ->addHandle('default')
    ->addHandle('some_other_handle')
    ->load();

/*
 * Generate blocks, but XML from previously loaded layout handles must be
 * loaded first.
 */
$layout->generateXml()
       ->generateBlocks();

/* 
 * Now we can simply get any block in the usual way.
 */
$cart = $layout->getBlock('header')->toHtml();
echo $cart;

?>

I'm looking for a full list/documentation of available block codes like "cart_sidebar", "header", etc.,

Logesh Paul
  • 7,600
  • 3
  • 26
  • 23
  • This should solve this: http://stackoverflow.com/questions/6347384/load-block-outside-magento-and-apply-current-template – ShopWorks Nov 29 '12 at 12:41

2 Answers2

1

Technically there is no full list, as they are all arbitrary!

Every <block name="..." /> declaration from layout XML may or may not be available for a rendering scope. Rendering scope is determined by the layout update handles which are set for the update object when load() is called. Additionally, blocks can be instantiated directly in PHP by the layout object.

So, for a particular rendering scope, there are many options. When all that is needed is a particular block and its children, it's up to the developer to determine if it's worth instantiating all of the blocks for a rendering scope. For the current question, it seems that this is the case. So all of the available blocks can be determined by inspecting the layout object's protected _blocks property:

// after generateBlocks() is called...

$blocks = $layout->getAllBlocks();
sort($blocks);

$list = "<table><thead><tr><th>Name in Layout</th><th>Class</th><th>Template</th></tr></thead>";
foreach ($blocks as $block) {
    $list .= sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>',$block->getNameInLayout(),get_class($block),$block->getTemplateFile());
}
$list .= "</table>";

echo $list;
benmarks
  • 23,384
  • 1
  • 62
  • 84
0

Give the current path to access Mage.php file.

For example,

Wordpress is under /Applications/XAMPP/xamppfiles/htdocs/wordpress/
And Magento is under /Applications/XAMPP/xamppfiles/htdocs/magento/

Use the below code to include Mage file from a file under wordpress root.

require_once '../magento/app/Mage.php';
Sankar Subburaj
  • 4,992
  • 12
  • 48
  • 79