Add custom category templates to magento - magento

I am building a webshop that contains the products for over 25 different shops. For every shop we have a similar but slightly different layout for the homepage. I want to add additional templates to use for every shops sub homepage. I found that i can add templates to local.xml . However I can only get it to work for one template.. What is the correct way to add multiple templates to local.xml ?
<page>
<layouts>
<homepage>
<label>Home Page</label>
<template>page/1column-home.phtml</template>
</homepage>
</layouts>
</page>
Many thx,

You have 2 Solutions :-
To add Layout pages in the select menu as you have to add the XML in Module ( config.xml ) not in theme layout ( local.xml )
So to achieve this create Custom Module and in config.xml
<global>
.......
<page>
<layouts>
<custom_layout_page >
<label>custom_layout_page</label>
<template>page/custom_layout_page.phtml</template>
<layout_handle>custom_layout_page</layout_handle>
</custom_layout_page>
</layouts>
</page>
.......
</global>
See Screen below :
OR:
You can create the template phtml file in your theme, for example in folder
page/category_custom_template.phtml
Then you can use the layout update for the category you want to assign this file as layout page or template root
Like the screen shot below :

Related

How can I show custom attribute value as menu item label in magento 2?

I have created a custom attribute for menu title on category add/edit page in my magento 2 setup. I want to show the value of the attribute saved in database instead of the category title on main menu. How can I do this. Please help
I was searching online and didn't find any exact solution to my problem.
You need to code in the frontend theme and also in your module to display your display custom attribute on the placement of the category title.
Custom/Module/view/frontend/layout/catalog_category_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="category.view.container">
<block class="Magento\Catalog\Block\Category\View" name="category.heading" template="Example_CategoryHeading::category/heading.phtml" before="category.description"/>
</referenceContainer>
<referenceBlock name="page.main.title" remove="true"/>
</body>
</page>
Turn on the Template path hints and find the phtml file, where you can find the file location attribute to display in the menu title. Remember to override your custom theme or module.
For Template path hints:
On the Admin sidebar, go to Stores > Settings > Configuration.
In the left panel, expand Advanced and choose Developer.
Expand Expansion selector in the Debug section and do the following:
To get the category attribute:
$categoryId = 3;
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
echo $category->getName();
echo $category->getData('<attribute_code>');
Remember, object manager is not recommended.
In the app/design/frontend/theme vendor/themename/Magento_Catalog/layout/ folder, the file catalog_category_view.xml is responsible for displaying the title. In this xml file, the setPageTitle action method is used, and you can override the category default title according to your requirement.

How to add new layout via local.xml?

How can I add a new layout in magento using local.xml? I want it to appear in layouts list when I make new CMS page.
Layout in magento using local.xml
please go and follow this code.
http://www.codeboss.in/web-funda/2009/07/07/create-new-layout-in-magento/
Look at the very last block of code on this page. You can go into app/code/core/Mage/Page/etc/config.xml and add lines like this:
<TEMPLATE_LABEL module="page" translate="label">
<label>TEMPLATE NAME</label>
<template>page/TEMPLATE-FILENAME.phtml</template>
<layout_handle>page_HANDLE_NAME</layout_handle>
</TEMPLATE_LABEL>
replacing the capitalized words, e.g.
<home_format module="page" translate="label">
<label>Home Format</label>
<template>page/home-page.phtml</template>
<layout_handle>page_home_format</layout_handle>
</home_format>
Right before the end of the <page> ... </page> block of code.
Note: if you do it this way, you may not be able to upgrade Magento without losing your custom layout, as config.xml is a core file.

How to remove 'add to cart' button from one cms page only?

I have created a web store with custom CMS page which has custom layout too. This page will show all the products of the store.
On that CMS page all the product is displaying but
I want to remove 'add to cart' button, 'add to wishlist' link and 'add to compare' link from products
I want to remove these links from that specific CMS page only.
I tried
<remove name="" />
in Layout Update XML of that CMS page but I can't get specific names to remove and while searching I tried every possible name but no success.
Any suggestions?
If you have used a custom layout and you are displaying all products on that page then you must have used block type to display all products in content of that page.
Something like
{{block type="catalog/product_list_random" name="product" template="catalog/product/list.phtml"}}
Simply make a new file and save it on same loction where list.phtml is saved (obviously with different name say new.phtml
Put that new.phtml instead of list.phtml
And delete whatever you don't want to display on that page from new.phtml
You only can remove whole blocks with layout updates like that, but those links and buttons are not separate blocks but get rendered within the Mage_Catalog_Block_Product_List block. You will have to replace the template of this block with your own:
copy /app/design/frontend/base/default/template/catalog/product/list.phtml to /app/design/frontend/YOUR/THEME/template/WHATEVER/catalog/product/list.phtml (use your theme directory and a directory name under template that refers to your CMS page)
edit this copy and remove the links and buttons
in the layout update handle for your CMS page, add:
XML:
<reference name="product_list">
<action method="setTemplate" template="WHATEVER/catalog/product/list.phtml" />
</reference>
(assuming that the product list block is called product_list)
Then, depending on what you want to remove, you can put between the "default" tags the following lines:
<remove name="cart_sidebar" /> "
<remove name="catalog.compare.sidebar" /> "
http://www.hostknox.com/tutorials/magento/remove-default-blocks/manual-removal

Magento changes layout dynamically via system variable

Is there a way we could changes the layout of a Magento page (let's say a product category page) dynamically by using system variable which have been set on our own module? I want to be able to set my category page's default layout via my own module admin config panel. So that I don't have to deal with those confusing XML layout file each time I want to change my default layout for a certain magento page.
I know, on a phtml file, we could simply call our own module's system variable by calling Mage::getStoreConfig('module/scope/...') to use that system variable. but what if we want to use that system variable to change the whole layout which is set on the XML layout file by default.
I don't see any ways to pull that system variable value on the XML Layout file.
But I'm pretty sure there must be a right way to do that. So far, this is the closest clue that I've got
Magento - xml layouts, specify value for ifconfig?
But, still, I couldn't find any direct answer for what I really want to achieve
this is the content of my config.xml
<config>
<modules>
<Prem_Spectra>
<version>0.1.0</version>
</Prem_Spectra>
</modules>
<global>
<models>
<spectra>
<class>Prem_Spectra_Model</class>
</spectra>
</models>
<helpers>
<prem_spectra>
<class>Prem_Spectra_Helper</class>
</prem_spectra>
</helpers>
</global>
</config>
This can be very easily achieved using layout xml and a simple method in your helper. I don't see any requirement for an observer here or anything else overly elaborate.
So, based on your requirements to change all category page layouts from your own modules store config value you will require the following in your layout xml:
<catalog_category_view>
<reference name="root">
<action method="setTemplate">
<template helper="yourmodule/switchTemplate" />
</action>
</reference>
</catalog_category_view>
And the following in your modules default helper:
public function switchTemplate()
{
$template = Mage::getStoreConfig('path_to/yourmodule/config');
return $template;
}
we are talking about the template of the root-element, so 3columns, 2columns, etc? correct?
Implement an observer, listen to the event controller_action_layout_generate_blocks_before and then get the block in the observer and set the template
Mage::app()->getLayout()->getBlock('root')->setTemplate($myFancyTemplatePath);
This should do it.
Other idea, try the event controller_action_layout_load_before, but I think this is too early.
In addition to Fabian's answer:
You could perhaps extend the functionality of the category 'display modes'.
Using the controller_action_layout_load_before event and then retrieve the display mode of the category and create a XML update handle for it.
$category = Mage::registry('current_category');
$handle = 'category_displaymode_' . strtolower($category->getDisplayMode());
$layout = $observer->getEvent()->getLayout();
$layout->getUpdate()->addHandle($handle);
This way you can pre-define all kinds of layouts in your local.xml and easily switch between them by adjusting the 'display mode' dropdown on the category edit page in the admin.
With some tweaking in the admin you can add additional display modes to the dropdown to make more types of custom display mode xml update handles available.

How to change the page layout in Magento?

I want to change my category page layout to 2columns-left.phtml. The default is a 3 column layout. I only want to set the category page layout to 2columns-left.phtml. The rest should keep the default page layout (3 columns). How can I do this?
You can do this for categories individually under the Design tab. You can do this for all categories by adding a local.xml to your custom theme's layout folder which contains the following:
<?xml version="1.0"?>
<layout>
<my_category_root_template>
<reference name="root">
<tpl>page/2columns-left.phtml</tpl>
</reference>
<my_category_root_template>
<catalog_category_layered>
<update handle="my_category_root_template" />
</catalog_category_layered>
<catalog_category_layered_nochildren>
<update handle="my_category_root_template" />
</catalog_category_layered_nochildren>
<catalog_category_default>
<update handle="my_category_root_template" />
</catalog_category_default>
<!-- EDIT: forgot about search results page -->
<catalogsearch_advanced_result>
<update handle="my_category_root_template" />
</catalogsearch_advanced_result>
<layout>
For further info, Magebase has a great Layout XML primer, and Alan Storm wrote the book on it.
I don't have much experience with magneto but i think it may work.
Go to admin->Catalog->Categories->Manage Categories
Then go to ->Custom Design
You will find Page Layout option there change that to whatever you like.
First to set up the categories: in the admin panel, select the Catalog menu. Then on the left, select the category that you wish to edit. On the right, the Custom Design tab contains the Page Layout property. If you want all categories to have the same layout, set the root category's Page Layout accordingly, and then the child categories' "Use Parent Category Settings" property to True.
The layout for all other pages will be determined by the CMS->Pages page. Click the page to modify, then on the left, select Design, and then on the right, the Layout property.
If you are wondering how to make sure that all subsequently created pages in the CMS admin area by default use a particular layout, so that you don't have to change it each time you create a new content page, I am not certain, as mine seems to default to 1column, even though the XML seems to configure the page to use 3-column (I'm referring to what is in /app/design/frontend///layout/page.xml).. I may misunderstand how that is linked to this, though, perhaps someone else can comment. My understanding is that the CMS pages have their layout settings and custom layout information stored in the database. Therefore, perhaps there is another setting in the database that choose the default when creating a new CMS page.

Resources