how to call a custom module block in another block phtml file - magento

How to call a block in another block phtml file?
I have created a module to display special product.
My Question is that in featured product phtml file ,i have to check there is any special product,if present i want to display special product otherwise fetured product should be displayed.

you can call your custom block file in to another custom template files as below
<?php echo Mage::getSingleton('core/layout')->createBlock('custom/mycustomblock')->setTemplate('custom/test.phtml')->toHtml(); ?>
hope this will help you.

Create a helper class method in your custom module to check the special product condition.
your helper class will return the true or false value to your module block files (.phtml).
Call the module helper calls in using below code.
Mage::helper('yourmodule')->checkSpecialProduct();
try this one.

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.

Magento Translation via Ajax

I have a little problem with translation mechanism in Magento.
If I call translate mechanism from phtml template like this
<li class="landline"><span class="value"><?php echo Mage::helper('ssg_rates')->__("Landline") ?></span></li>
I get translated value from csv file for example "Vaste Lijn" and this is expected output, however when I do the same in block and get data back via AJAX, like this:
in controller I create block and call method to output html
the method outputs something like this:
echo '<li class="landline"><span class="value">'.Mage::helper("ssg_rates")->__("Landline").'</span></li>';
data returned to phtml via Ajax:
{{{Vaste lijn}}{{Vaste lijn}}{{Landline}}{{Ssg_Rates}}}
Why is that? Why it isn't only translated value like in the phtml template?
Cheers
Paul
Problem was caused by translation inline for frontend turned on in admin. Disabling it solved the problem.

Magento : how to load product details on homepage

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):

How do I give my controller function its own template?

I've built a widget module that has this basic controller:
class MyModule_OrderForm_HandlersController extends Mage_Core_Controller_Front_Action{
public function handleroneAction(){
// do some stuff
}
}
So this is giving me a page at mydomain.com/orderform/handlers/handlerone which is great, but how do I give that function its own template file.
I've searched Google for hours and not found a straight forward answer, I hope someone here can help me.
Thanks.
If you are looking for how to create a widget i'd check out http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-1/.
But for giving your controller action a template I would check out this article from inchoo:
http://inchoo.net/ecommerce/magento/programatically-create-magento-blocks-and-inject-them-into-layout/
As the article says the proper 'magento way' is to create a block file in your module that extends Mage_Core_Block_template and then insert that block with its template file into the page using layout updates.
The method outlined in the inchoo article lets you skip creating a custom block and layout updates and lets you insert your template directly into the content area of that action.
When you call $this->loadLayout() you apply your site's theme to that action. The template you insert would have everything you want to insert into that page's main content area.
The first param is the type of block you're inserting.In this example we are using Mage_Core_Block_Template, the basic block for asigning templates. The second param of the createBlock() function ('my_block_name_here') can be any arbitrary name. The third param is an array of attributes given to the block. In this example the only attribute we're assigning is 'template'. this is how we tell the block which template to use.

Resources