Magento : how to load product details on homepage - magento

I've been trying to show product details on homepage. After long hours searching on google and stackoverflow, i've come with this solution:
Mage::register('product',Mage::getModel('catalog/product')->load('17'));
$block = Mage::app()->getLayout()
->createBlock('catalog/product_view')
->setData('area','frontend')
->setTemplate('catalog/product/view.phtml');
echo $block->toHtml();
It does render the catalog/product_view block but no its children, i'd like to understand how to render the children too ?
Note: my homepage as the layout "myviewer.phtml" and the code above is in "myviewer.phtml".

You are currently manually creating the product view block and not using the standard layout xml, so you are not getting any of the default child blocks that would be loaded on the product page via the product page layout handle;
catalog_product_view
Viewing this layout handle in the catalog.xml file will show you which blocks are loaded, and importantly which child blocks are added to the block named 'product.info' which usually uses the template section you are trying to render.
If you view the product page template 'catalog/product/view.phtml' that you are rendering, you will see it calls blocks that will not be available such as;
<?php echo $this->getChildHtml('addtocart') ?>
So to fix this you have two options,
Manually initialize and add each of the child blocks to the product block you are creating using the append() method.
Or
Update the controller action that was used to load and render the page to include the product page layout handle. Call the following in the controller,
$update = $this->getLayout()->getUpdate();
$update->addHandle('catalog_product_view');
This will cause the layout to include all of the correct product page child blocks, then in the template just call;
$_product = Mage::getModel('catalog/product')->load('17');
Mage::register('product',$_product);
Mage::register('current_product',$_product);
$block = Mage::app()->getLayout()->getBlock('product.info');
echo $block->toHtml();
Adding the handle could be done in the controller which would need to be overridden using a rewrite in a custom module (there are already lots of articles on this).
Use an event observer for any event fired before the controller action.

Magento creates its block hierarchy based on its layout XML configuration. For example, the catalog/product_view block is used in the catalog.xml layout file (usually found in app/design/frontend/base/default/layout/catalog.xml). This file also defines all of the children blocks of the catalog/product_view block.
Magento decides which layout instructions to carry out depending on the active layout handles. For example, the catalog.xml file references the catalog_product_view layout handle, which corresponds to the catalog module, product controller, view action. Whenever that specific controller action is invoked, that layout is applied.
In your case, the home page is probably a CMS page, which doesn't apply the catalog_product_view layout handle (it typically applies the cms_page handle). As such, the block hierarchy is not defined correctly for you to utilize the catalog/product_view block.
There are many ways of resolving this issue, but first you have to think about exactly what you're trying to do. If you try to use the catalog/product_view block, your home page will look almost exactly like a regular product page, which may or may not be what you want. One way of achieving that is to use the <update/> tag, which takes another layout handle and merges it with the current layout handle.
I recommend reading some more into Magento's layout XML system. Alan Storm has some great resources on the subject, such as this blog post (warning, it's a bit outdated), and his book on Magento's layout system.

you can do it by inserting in CMS > Pages > Home
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" LATEST="0" template="catalog/product/list.phtml"}}
In this case you have to change in template/catalog/product/list.phtml
Find this code around in lines 74 , 133 and 180
<?php
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach($_nameAfterChildren as $_nameAfterChildName):
Replace adding a if statement , should be something like this :
<?php
$_nameAfter = $this->getChild('name.after');
// New if here
if($_nameAfter):
$_afterChildren = $this->getChild('name.after')->getSortedChildren();
foreach($_afterChildren as $_afterChildName):

Related

Magento2 separting product info block

What I need is - to move the Product Options block below the addtocart block.
In the template file
Magento_Catalog/templates/product/view/detail_layout.phtml
the info is coming from
echo $block->getChildHtml('product.info') ?>
product.info block holds the data both for custom options and addtocart
I am not able to locate which file holds this data, so that I can move or switch position.
Thanks.
You need to overrule the layout options which are set by the default /vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml file with your own theme. Add after="product.info.options.wrapper.bottom" to the product options block, like so:

Load Magento module template

For a Magento module I need to load template file and let it replace the complete page. It is a XML file (but could also be any content whatsoever).
Generally speaking when
MYNS_MYMODULE_controllernameController is triggered and calls fooAction() I need to be able to display a clean site with the content from my template file.
Please let me know where to place the template file and how to tell Magento to load this file as a root template without anything else around.
Edit, to clarify it more:
For http://domain.tld/modulename/controller/action/
Where do I have to place template files and how should I reference them?
You could do it like this
$this->loadLayout()->getLayout()->getBlock('root')->setTemplate('page/1column.phtml');
$this->renderLayout();
or to set a custom response
//$file = myfile or html
$this->getResponse()->setBody($file);
$this->renderLayout();
Ideally your template should sit in /app/design/frontend/mypackage/mytheme/template/mytemplate.phtml
You should read Magento doc and Alan Storm Blog. Alan also wrote a book about Magento Layout: No Frills Magento Layout.
Your url: http://domain.tld/modulename/controller/action/
The modulename_controller_action Handle is created by combining the route name (modulename), Action Controller name (controller), and Action Controller Action Method (action) into a single string. This means each possible method on an Action Controller has a Handle associated with it.
In your layout xml handles this request:
<modulename_controller_action>
......
</modulename_controller_action>
Hope my suggestion is useful for you.

Set homepage from theme's layout

I am trying to set a CMS homepage via a theme's local.xml layout update file in the <cms_index_index> node. I swear I've seen functions to change the store configuration temporarily within a layout node (but maybe I dreamt it), but I'm having trouble finding the layout function in classes like Mage_Core_Block_Abstract and its children classes.
For reference, I've checked in Mage_Cms_IndexController and found the function which renders the homepage:
public function indexAction($coreRoute = null)
{
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE);
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultIndex');
}
}
Or, am I doing this completely the wrong way? What would be best practice for a problem like this? I do not want to add a store view for the new theme, as the new theme is for mobile platforms and requires the same settings from the store view. Thanks guys!
This is not possible. The layout configuration is not invoked until after checks occur to see if there is a valid page which has been specified; because these checks fail, the Default router will match and (by default) the application will display the 404 page.

joomla Modules - Between Article or inside Article? What is correct?

I want to place articles and modules on my page. It should look like my scatch:
I am wondering if i can make 4 articles and one module (information box) and place this inbetween those articles, Or should i just create one article wich looks like the hole area (article 1-4 + INformation box)? So far i have tried to do {loadposition infobox-pos} into article 2. But than the box-width is not 100% but 50%.
Than aggain if i would make one big article with the contents from article 1-4 it would not fit great into my responsive layout.
I use T3-Framework.
Joomla doesn't support this natively, since module output is controlled by the template, and component output (in this case, com_content, view=category, layout=blog) is handled before modules are rendered.
You can proceed in many ways, I am listing them in order of decreasing ease:
Write a jquery script that moves the pieces in the browser. You could also use mootools and it's bundled with joomla, but odds are you already have jquery loaded anyways.
Write a view override for abovementioned view: so copy /components/com_content/view/category/tmpl to /templates/your_template/html/com_content/category (copy all files). Then edit blog.php and insert your module there, use something like /plugins/content/loadmodule/loadmodule.php :
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$modules = JModuleHelper::getModules($position);
$params = array('style' => $style);
foreach ($modules as $module) {
echo $renderer->render($module, $params);
}
Write a content plugin
you can also use {loadmodule YourCustomModuleTitle} and place this in <div> with custom style attributes like
<div style="width:100%;float:right">{loadmodule MyModule}</div>
or
<div class="something">{loadmodule MyModule}</div>
Note: the module which loaded must be assigned to the same menu item or "all"

Displaying a static block as a popup window in Magento

I'm trying to display a static block in Magento as a popup window, but can't seem to get the code to work.
I've found code in various places on the internet that seems to be fairly close to what I want but I can't get any results. I've used the basic code to return the "top links" to my site so I know that the basics work.
I've created a delpopup.php script in my Magento root folder and put in this code:
<?php
require_once('app/Mage.php');
umask(0);
$layout = Mage::app()
->getLayout();
$layout
->getUpdate()
->addHandle('default')
->load();
$layout
->generateXml()
->generateBlocks();
echo '<p>before</p>';
echo $layout
->createBlock('cms/block')
->setBlockId('delivery-info')
->toHtml();
echo '<p>after</p>';
?>
Unfortunately the code doesn't display the static block. That part of the output is blank.
The idea is that I can place a link in a regular page in Magento and have my delivery into pop up. Like this:
<a title="" onclick="popWin('http://www.mysite.com.au/delpopup.php', 'deliveryinfo', 'width=300,height=300,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;" href="#">Delivery Info</a>
Basically I want to be able to display a static block in a popup window that uses my Magento site's theme. Any ideas would be greatly appreciated.
As I thought, the problems lies on incorrect Block Id.
Now that the cms is able to be shown.
So now the question is: how to get the theme work?
Not really sure what do you mean by theme, if what you mean is css that was included in <default> tag, you can use:
Mage::getDesign()->setTheme('your theme');
echo $layout->getBlock('head')->toHtml();
After the xml has been finished generated, it means put that code after:
$layout
->generateXml()
->generateBlocks();
I'd first setup a controller and block(s) to render the type of layout that you want to throw within the popup. So, instead of doing this within a standalone php file, do it within the regular mage framework within a specific controller action.
Then, once you have that working - the question is how to pull it into a popup. I'd go with maybe a nice jquery popup widget that allows you to specify a URL to hit - and then just pass in the one that you prepped for step 1 above.
You may want to look at adminhtml/default/default/template/popup.phtml for inspiration. That's actually a popup template for the admin, not the frontend, but you can see what they've done.
They're pulling in some standard magento blocks including the head block, which should pull in all your CSS and JS, in order to give you the general color scheme / look&feel of your frontend, but without all of the navigation, etc.

Resources