Including magento header outside of magento. Problems with $this->getChildHtml() - magento

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

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.

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();.

Related

How to "intercept" site images from Magento static cms blocks

I have an extension that overrides getSkinUrl() and intercepts images coming through it, makes changes, then continues. This works for any images referenced in PHTML files.
However, This isn't catching images in CMS blocks. In the CMS blocks, I'm using
{{skin url="images/your_amazing_image.gif"}}
Magento's template variables are defined in the following file.
app/code/core/Mage/Core/Email/Template/Filter.php
For the {{skin}} variable, the following code is used around line 264 in filter.php
public function skinDirective($construction)
{
$params = $this->_getIncludeParameters($construction[2]);
$params['_absolute'] = $this->_useAbsoluteLinks;
$url = Mage::getDesign()->getSkinUrl($params['url'], $params);
return $url;
}
I'm not sure how you are overriding getSkinUrl, but make sure that the getSkinUrl method is the same method you are overriding.

Render module in the right position from code (Joomla 2.5)

In my component view, I can show a module by this code:
$module = JModuleHelper::getModule('mod_login');
$html = JModuleHelper::renderModule($module);
echo $html;
or
echo JHtml::_('content.prepare', '{loadmodule login}');
But that will usually place the module in the middle of the screen in the main-content div.
How can I place the module in the position defined in the module params?
Take a look at how the admin template Isis renders the quickicons module on the home page or at how the error page in protostar renders the search modules.
$this->searchmodules = JModuleHelper::getModules('position-0');
foreach ($this->searchmodules as $searchmodule)
{
$output = JModuleHelper::renderModule($searchmodule, array('style' => 'none'));
$params = new JRegistry;
$params->loadString($searchmodule->params);
echo $output;
}
You just want one module, but you get the basic idea.
Try This.
You can Pass the module parameters too.
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$Module = &JModuleHelper::getModule('mod_fmDataGrid');
$Params = "param1=bruno\n\rparam2=chris";
$Module->params = $Params;
echo $renderer->render($Module);
This may help you..
You can't, jdoc includes in the template are processed after the component has finished producing its output.
Why not let Joomla render the modules in the right positions? that should be one of the reasons for using it, i.e. taking care of placement and order of modules.
If you have other reasons for doing this, please explain further.

Hide Products without images magento

I have followed this answer to hide products without images on the category listing page. It worked nicely for a while.
Now, for some reason it seems the products without images are still showing up on the listing page.
Any ideas as to why this may be happening?
Note:
The same list.phtml page is being used.
Thank you.
Add the following to list.phtm:
//$_productCollection=$this->getLoadedProductCollection();
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
->load();
This answer recommended the following:
->addAttributeToFilter('image', array('neq' => 'no_selection'))
Whereas I have set it to:
->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
The reason the previous answer did not work was because the product collection doesn't load the regular image, and therefore the regular image cannot be added as an attribute to filter, so instead, I added the small_image as the attribute to filter.
You can also try R.S's answer where he adds the image to the page and hence the collection. You may have to also add all attributes using:
->addAttributeToSelect('*')
There are some tricks to keeping Magento in line. One thing I've learned is that the Magento Model will change for many different reasons, and its kinda hard to figure out why. There are better ways to do this (modifying the collection, etc) but it sometimes just does not work and you don't have days to figure out why.
If you want a surefire way to make sure your image exists, use the following code... It may not be the 'magento way' but it works, and I tested it on my site (Magento EE 1.12). Put it in a function, or use it directly in your phtml if you want!
It basically just makes sure the URL exists.
$exists = false;
$entity_id = 8800;
$product = Mage::getModel('catalog/product')->load($entity_id);
$mediaUrl= Mage::getBaseUrl('media');
$imageUrl = $mediaUrl . "catalog/product" . $product->getImage();
$file_headers = #get_headers($imageUrl);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
var_dump($exists);
var_dump($imageUrl);
echo '<img src="' . $imageUrl . '" />';
$exists will either be true (image does exist) or false (image does not)
I think that the issue is that you are trying to get the 'image' (base image) property on the list.phtml (by default i can only access the thumbnail, small_image).
On list.phtml (not loading the full product resource like on view.pthml)
echo $_product->getImage() //null
echo $_product->getThumbnail() // path/name.jpg
echo $_product->getSmallImage() // path/name.jpg
I think you may need to add adding something like this to app/design/frontend/default/yourtheme/layout/catalog.xml
<action method="addAttribute"><name>image</name></action>
See How to get access to custom Image Attribute in list.phtml in Magento

MAGENTO - Registration form (+validation and callback) outside magento files?

I'm trying to integrate a php file inside an aplication that gives users the posibility to register an account. This account has to be registered inside a magento store.
This is what i have to this momment:
<?php
require_once '../../../app/Mage.php';
umask(0);
Mage::app('default');
Mage::app()->getTranslator()->init('frontend');
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
// get layout object
$layout = Mage::getSingleton('core/layout');
//get block object
$block = $layout->createBlock('core/template');
//print_r(get_class_methods(get_class($block))); <- use for seeing classes
$block = $block->setTemplate('customer/form/register.phtml')->renderView();
echo $block;
?>
This code renders the registration form but stops when he is showing the input fields. I tried with "mini.login.phtml" and it renderes correctly. I'm not very good at Magento, or english. I can provide any other information if necessary.
Any help will be appreciated!
Regards
You are using the wrong block type when dynamically creating the block. Try using this:
createBlock('customer/form_register')

login state in top.links when called

I'm trying to pull the header of our Magento store in a standalone php page. Everything works as expected except the 'Log In' link does not appear. The customer.xml file uses the standard 'customer_logged_in' node to 'addLink' but it seems like the login status isn't getting assessed with the method I'm using. How do I get this Log In | Log Out link to display?
Here is the code I'm using:
require_once $mage_path;
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$layout = Mage::app()->getLayout();
$layout->getUpdate()->addHandle('default')->load();
$layout->generateXml()->generateBlocks();
echo $layout->getBlock('header')->toHtml();
I'm able to get the correct login state independently using the following:
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
if ($session->isLoggedIn()) {
/* logged in */
} else {
/* not logged in */
}
However, I don't want to manage two different styles (one through the default magento XML and another for this custom page). I would rather have the getBlock call return the whole block with the correct login status. Any insight is appreciated.
You need to add the customer_logged_in to your handles, as well as default. For example:
...
$handles = array('default');
if (Mage::helper('customer')->isLoggedIn()) {
$handles[] 'customer_logged_in';
}
$layout->getUpdate()->addHandle($handles);
...

Resources