I want to change the page title in magento.
In my app/design/frontend/default/customPackage/template/page/html/head.phtml, there are lines control all page title(what I want just to modify the catalog page's title)
<title>
<?php
if ($current_product = Mage::registry('current_product')) {
echo substr($current_product->getName() . " - " . Mage::helper('core')->currency($current_product->getFinalPrice(), true, false),0,69);
} else {
echo substr(str_replace("- Products","",$this->getTitle()),0,100);
}
?></title>
but I don't want to modify directly from head.phtml in app/design/frontend/default/customPackage/template/page/html, instead, I want to replace this head.phtml with another head.phtml in my own module tempate files.Let's say , make it app/design/frontend/default/customPackage/template/catalog/html/head.phtml instead
To answer your question, basically we need to find where is page/html/head.phtml file is defined. Answer is in layout files, more specifically in page.xml. Location is :app/design/frontend/<your_package>/<your_theme>/layout/page.xml. Inside that file, under the handle <default>, you can see
<default translate="label" module="page">
<label>All Pages</label>
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
<block type="page/html_head" name="head" as="head">
<action method="addJs"><script>prototype/prototype.js</script></action>
<action method="addJs"><script>lib/ccard.js</script></action>
<action method="addJs"><script>prototype/validation.js</script></action>
<action method="addJs"><script>scriptaculous/builder.js</script></action>
-------
</block>
---------
</block>
</default>
Where
<default> is known as layout handler. Blocks that comes under this
handler will show in every page in magento.
block page/html is your root block. It is the parent block of all other blocks. There should be only one root block per page. You can reference this block using its name root in your custom layout files, in order to alter anything inside this block.
block page/html_head is the block which is referenced in your question. This block is use to hold the <head /> section of your page (in terms of html tree). You can see that magento loads some of its core javascripts and css inside this block.
But page/html_head is not set with any template as you already see. Then how page/html/head.phtml came to the seen ??? It should set somewhere in magento. So let us go the backend side of this block, where all of its block methods are defined. The file location is app/code/core/Mage/Page/Block/Html/Head.php. Yes we found out it.
class Mage_Page_Block_Html_Head extends Mage_Core_Block_Template
{
/**
* Initialize template
*
*/
protected function _construct()
{
$this->setTemplate('page/html/head.phtml');
}
------
}
So Magento set template for page/html_head block here, through _construct() method. Change it to your template location
protected function _construct()
{
$this->setTemplate('app/design/frontend/default/customPackage/template/catalog/html/head.phtml');
}
It will now set location of page/html_head block to your custom template file.
If you want to the block file is also untouched, you can rewrite this block file using your own module. In your config.xml file, you should use this
<config>
<global>
<blocks>
<page>
<rewrite>
<html_head>Namespace_Modulename_Block_Html_Head</html_head>
</rewrite>
</page>
</blocks>
</global>
</config>
and you should define a block file in app/code/local/Namespace/Moduleame/Block/Html/Head.php
<?php
class Namespace_Modulename_Block_Html_Head extends Mage_Page_Block_Html_Head
{
protected function _construct()
{
$this->setTemplate('app/design/frontend/default/customPackage/template/catalog/html/head.phtml');
}
}
This way core files are untouched and still you can alter the template path.
Related
I would like to add a shopping cart block a cms page, but whenever I try, nothing happens...not even an error.
I've tried following this tutorial, http://www.magento.cc/how-to-use-php-on-a-cms-page.html.
So I created the new folders in app/code/local and then a Test.php file, yet when I try to include
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
in the cms page, nothing appears.
Here's my code in the Test.php page:
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
You can do a local.xml update. And place your content as .phtml file
<cms_index_index>
<reference name="content">
<block type="Your_custom/Block" name="home_main" as="home_main" template="cms/default/home.phtml">
</block>
</reference>
</cms_index_index>
Create a file named home.phtml in cms/default/ Then you can use your own block type to use your custom module / functions
Then add your home page content in there.
I don't know what are you trying to achieve. But from your question, I have a strong feeling that, you are trying to set a template of your own through CMS page. If that is the case, let us analyze why your block didn't show any output.
Your block definition is like this
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
There is no problem with this defintion. But it is good, if you add name to your block. If you set a template along with that, then you don't need any backend code for setting a template for your block. That is your block should look like this
{{block type="YourModule_Custom/test" name="test.block" template="test.phtml"}}
Now when magento encounter this, it will find your block, set described name to that block (for later reference this name will be used. However it is not relevant in this case), set template specified to your block and then render the content in that template.
So you should have a block of type that you specified. You had it right now(you dont need that _toHtml() inside that). Along with that you need a template file test.phtml and it should be in the location app/design/frontend/<your_package>/<your_theme>/template/test.phtml. You dont have that file right now. So create it and add this content inside that file
<p><?php echo "I am here. Can you see me ?"; ?></p>
Now check your CMS page output. You can see that content. Isn't it ?
So right now what you are trying to do is, instead of setting a template along with that block definition, you are trying to set it through your block. Is it wrong ? Obviously, NO. There are some cases, where we need to go with that way. I assume you really need it. So again let us redifine our block in this form.
{{block type="YourModule_Custom/test" name="test.block"}}
Hmm. Here we didn't set a template to this block right now. You can hence set it through your block definition. You used _toHtml() method.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
?>
This method is using to set a template and then renders the content. So you are in the right track. But the problem here is, you are not setting any template!! plus your method returns a variable $html which holds nothing. So what should we return through a _toHtml() ? The answer lies in Mage_Core_Block_Template. Let us look on _toHtml() definition
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}
$html = $this->renderView();
return $html;
}
Basically what this does is, it checks whether a template is set , if not return nothing. If it does renders it. That means, it is obvious that we need to set a template. So your block should look like this.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$this->setTemplate('test.phtml');
$html = parent::_toHtml();
return $html;
}
}
Note that your block extends from Mage_Core_Block_Template rather from Mage_Core_Bock_Abstract. This is because setTemplate() method is defined in Mage_Core_Block_Template class. In _toHtml(), we are setting our template and then leave rest to our parent block. Now check whether content in test.phtml is showing in your CMS page. It does right ?
I don't understand exact what do you want. but by your question my understanding is you want to display shopping cart block into any CMS page like Home page, About us page etc.
If my understanding is right then here is one solution for you.
You can insert this code into your cms page from admin.
Admin -> CMS -> Pages -> Select any page on which you want to display block -> Click on design tab from left navigation ->Under Page Layout section insert the below code in "Layout Update XML" field. Click on save.
<reference name="content">
<block type="checkout/cart" name="checkout.cart">
<action method="setCartTemplate"><value>checkout/cart.phtml</value></action>
<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
<action method="chooseTemplate"/>
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/item/default.phtml</template></action>
<block type="core/text_list" name="checkout.cart.top_methods" as="top_methods" translate="label">
<label>Payment Methods Before Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
</block>
<block type="page/html_wrapper" name="checkout.cart.form.before" as="form_before" translate="label">
<label>Shopping Cart Form Before</label>
</block>
<block type="core/text_list" name="checkout.cart.methods" as="methods" translate="label">
<label>Payment Methods After Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
<block type="checkout/multishipping_link" name="checkout.cart.methods.multishipping" template="checkout/multishipping/link.phtml"/>
</block>
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_shipping" name="checkout.cart.shipping" as="shipping" template="checkout/cart/shipping.phtml"/>
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
</block>
</reference>
After that you will see shopping cart block on your CMS page. If you don't need any block from them you can remove that blck from the above code.
if you try to call add to cart from cms page then add url like
checkout/cart/add?product=$id&qty=$qty
Example-
<a href="{{store url='checkout/cart/add?product=5&qty=3'}}">
<img src="{{skin url='images/addtocart.jpg'
}}" alt="product5" /></a>
Im trying to load an file sales/guest/form.phtml for added tab left navigation tab on customer my account, via customer.xml but didn't kindly help me.
Refund
I have done somewhat similar in my old project. here is what i do :
Added a new link in My Account section from customers.xml i.e
<action method="addLink" translate="label" module="preorder"><name>Previous_Orders</name><path>preorder/index/</path><label>Previous Orders</label></action>
In your case you can use an already running module instead of preorder and change the name in above action.
In module/contollers/IndexController.php add a view as follows :
public function viewAction()
{
$this->loadLayout();
$this->renderLayout();
}
Add preorder/preorder.phtml template folder in your theme template folder.
in your theme layout folder add preorder.xml as follows:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
</default>
<preorder_index_index>
<label>Customer My Account Previous Orders</label>
<update handle="customer_account"/>
<reference name="content">
<block type="preorder/preorder" name="preorder" template="preorder/preorder.phtml" />
</reference>
</preorder_index_index>
</layout>
Hope you ot the idea, what we have done is created a new module which loads the template on index action, you cn change the name of module "preorder" and also the action "index" while working on this.
I was very brief while writing this, Hope this helps you in someway.
I need to change shopping cart page title. but i could not find it. where i should change it. so any help appreciated.
thanks
Changing the XML will have no effect because the title is set by the controller at app/code/core/Mage/Checkout/controllers/CartController.php.
$this
->loadLayout()
->_initLayoutMessages('checkout/session')
->_initLayoutMessages('catalog/session')
->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart'));
It's never a good idea to modify core files, and overriding controllers can be tedious. Therefore, the correct and quickest place to change this is in your translation file, located at app/locale/YOUR_LANGUAGE/Mage_Checkout.csv. If you do not have this file in your relevant directory you may create it and just add this line:
"Shopping Cart","NEW TITLE HERE"
If you do have the file then simply edit that line, ensuring your new title follows the original title and comma and is enclosed in double quotes.
The right way to do it, is making an override on the checkout controllers, is so easy.
First:
Add a new module with two subdirectories: controllers and etc.
Mynamespace/Checkout/controllers
Mynamespace/Checkout/etc
Then, in the etc directory add the file: CartController.php with the next content:
require_once 'Mage/Checkout/controllers/CartController.php';
class Mynamespace_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction()
{
$cart = $this->_getCart();
if ($cart->getQuote()->getItemsCount()) {
$cart->init();
$cart->save();
if (!$this->_getQuote()->validateMinimumAmount()) {
$minimumAmount = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())
->toCurrency(Mage::getStoreConfig('sales/minimum_order/amount'));
$warning = Mage::getStoreConfig('sales/minimum_order/description')
? Mage::getStoreConfig('sales/minimum_order/description')
: Mage::helper('checkout')->__('Minimum order amount is %s', $minimumAmount);
$cart->getCheckoutSession()->addNotice($warning);
}
}
// Compose array of messages to add
$messages = array();
foreach ($cart->getQuote()->getMessages() as $message) {
if ($message) {
// Escape HTML entities in quote message to prevent XSS
$message->setCode(Mage::helper('core')->escapeHtml($message->getCode()));
$messages[] = $message;
}
}
$cart->getCheckoutSession()->addUniqueMessages($messages);
/**
* if customer enteres shopping cart we should mark quote
* as modified bc he can has checkout page in another window.
*/
$this->_getSession()->setCartWasUpdated(true);
Varien_Profiler::start(__METHOD__ . 'cart_display');
$this
->loadLayout()
->_initLayoutMessages('checkout/session')
->_initLayoutMessages('catalog/session')
->getLayout()->getBlock('head')->setTitle($this->__('Here it go the new title!!!!'));
$this->renderLayout();
Varien_Profiler::stop(__METHOD__ . 'cart_display');
}
}
and then, the config.xml file:
<config>
<modules>
<Mynamespace_Checkout>
<version>0.1.0</version>
</Mynamespace_Checkout>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<mynamespace_sales before="Mage_Checkout">Mynamespace_Checkout</mynamespace_sales>
</modules>
</args>
</checkout>
</routers>
</frontend>
And last, the module activator: app/etc/modules/Mynamespace_Checkout.xml
<config>
<modules>
<Mynamespace_Checkout>
<active>true</active>
<codePool>local</codePool>
</Mynamespace_Checkout>
</modules>
</config>
This was tested in a Magento Enterprise 1.13.
Greetings
The title is actually set in the XML for that page. You should open the checkout.xml file in the app/design/frontend/packagename/themename/layout/ directory and place this code inside of the node in the XML:
<reference name="head">
<action method="setTitle"><title>My New Checkout Title</title></action>
</reference>
By default, I believe it grabs the label associated of the handle if the page title is not assigned explicitly (right now the label is "Shopping Cart" which is why you are getting that title).
Here is what my code looks like:
<checkout_cart_index translate="label">
<label>Shopping Cart</label>
<remove name="right"/>
<remove name="left"/>
<!-- Mage_Checkout -->
<reference name="head">
<action method="setTitle"><title>My New Checkout Title</title></action>
</reference>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<!-- More Below -->
</checkout_cart_index>
One other thing to mention, you can also make these changes in a local.xml (which is what I would recommend). The local.xml file will load on top off all the other XML files and your changes in that file will override any other XML files in the layout directory. A pretty good tutorial can be found here.
Here is a short example how to overwrite the title.
Force title
You can change the variable and overwrite the title in the specified layout.xml files.
Try this :
<reference name="head">
<action method="setTitle"><title>My New Checkout Title</title></action>
</reference>
I am including a new template for the layout menu with this in the config.xml :
<layouts>
<new module="page" translate="label">
<label>Foo</label>
<template>page/foo.phtml</template>
<layout_handle>page_foo</layout_handle>
</new>
</layouts>
This all works fine and then from admin I can go to Category->Custom Design and Foo will be a choice in the dropdown for Page Layout.
My problem lies in the foo.phtml which is in the page directory
In that file the footer for example is included with
<?php echo $this->getChildHtml('footer') ?>
if add another file in the same place as the footer.phtml, say bar.phtml in /page/html/ directory. Then I would expect to be able to include it the same way. When I do :
<?php echo $this->getChildHtml('bar') ?>
It does not show up. I imagine I must have to declare the bar file somewhere in the xml too? I want the bar file only to be included when someone chooses the foo layout option for a category. What am I missing?
The layout_handle node in your config refers to a node it expects to find inside one of your layout files. You can find examples of the other page layouts in app/design/base/default/layout/page.xml. But essentially you're going to want something along the lines of...
<layout>
<page_foo translate="label">
<reference name="root">
<block type="core/template" template="some/file.phtml" name="my_block" as="bar" />
</reference>
</page_foo>
</layout>
I asked this question yesterday Static block on home page in Magento, which answered my question about hooking a cms/block to an existing block (content, in that example).
But now I would like to know how to create my own block.
I have this in my .phtml template:
<?php echo $this->getChildHtml('home_flash') ?>
And this in my cms.xml file
<reference name="home_flash">
<block type="cms/block" name="home-page-flash" before="content">
<action method="setBlockId"><block_id>home-page-flash</block_id></action>
</block>
</reference>
But this is not working.
I have also tried to create my own block type, (by copying the breadcrumbs declaration) in the page.xml file:
<block type="page/html_home_block" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
That file exists but isn't being rendered.
However when I reference the block like this:
<block type="page/html_breadcrumbs" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
It renders my home block template, but the original cms/block is not attached to it.
Hope all the different cases show what is happening and highlight the gap in my knowledge well enough for someone to answer, do I have to "register" my new "home_block" type somewhere?
There are many different blocks available that you can use without creating your own. In this case I think core/text_list would be suitable because it doesn't require a template and can have as many child blocks within it as you need.
<?xml version="1.0"?>
<layout version="0.1.0"><!-- All layout files start with this -->
<cms_index_index><!-- Index directive is the same as "home" page -->
<reference name="root"><!-- For more blocks that can be referenced see "default" directive -->
<block type="core/text_list" name="home_flash">
<block type="cms/block" name="home-page-flash">
<action method="setBlockId"><block_id>home-page-flash</block_id></action>
</block>
</block>
</reference>
</cms_index_index>
<!-- More directives might go here -->
</layout>
Other useful block types worth knowing are core/text and core/template which correspond to Mage_Core_Block_Text and Mage_Core_Block_Template respectively. They get used the most.
Your home made block type of page/html_home_block didn't have any PHP class with a matching name, and if you were genuinely creating your own you wouldn't be able to use the page prefix since Magento already does.
To create a block you only need a <block> tag in the layout file.
To create a block type you need to write a PHP class, give it a namespace and declare it as part of a module.
To add to an existing block is the time when you use a <reference> tag.
There are many fine articles at Magento Knowledge Base including some on Theming & Design.