Including magento header outside of magento. Probl

2019-08-28 04:52发布

问题:

I have researched this topic pretty thoroughly but can't find an answer.

I am trying to include a Magneto head into a WordPress page. I created a new wordpress template and added the following code to it.

try {
    require_once ABSPATH . '/app/Mage.php';

    if (Mage::isInstalled()) {
        $mage = Mage::app();
        Mage::getSingleton('core/session', array('name' => 'frontend'));

        if(Mage::getDesign()->designPackageExists('xxx')) {
            Mage::getDesign()->setPackageName('xxx');
            Mage::getDesign()->setTheme('xxx');             
        }


        // init translator
        $mage->getTranslator()->init('frontend');

        // init head
        $head = $mage->getLayout()->getBlockSingleton('page/html_head');        
} }   

Then a bit further down in the template I have

echo $head->toHtml();

Now what is happening is some parts of the head are being echoed and some parts are not.

When I go into head.phtml and try to figure out what is happening I notice that any line that contains

$this->getChildHtml()

does not get echoed.

I looked at this example and noticed that the author is manually adding the html and CSS. Why is this? Why don't they get added automatically? Is this a related problem

Thanks

回答1:

To show a block that is generated inside the header block, you need to first create it, then set it as child of the header block.

eg. Here is how I display within Wordpress a Magento header block, including the currency drop-down block that was generated by getChildHtml() inside the original header.phtml:

Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton("customer/session");
$layout = Mage::getSingleton('core/layout');

$headerBlock = $layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');
$currencyBlock = $layout->createBlock('directory/currency')->setTemplate('currency/currency.phtml');
$headerBlock->setChild('currency_selector', $currencyBlock);
$headerBlock = $headerBlock->toHtml();

Then you can write the block where you need it on the page:

echo $headerBlock;

I know it's a little late but hopefully this helps others with this issue.



回答2:

Are you familiar with how magento layouts are rendered? With $head = $mage->getLayout()->getBlockSingleton('page/html_head'); you create a new block without any children. That's why the author needs to add JS and CSS again. To load the default head block have a look at this thread Load block outside Magento. You can load it with $layout->getBlock('head')->toHtml();.



标签: magento