Custom Magento View.phtml file called via xml - magento

What is the code to pull in a custom view.phtml file via custom layout update (in admin > catalog > manage products > specific product) with magento? I would really like to do this in local.xml for specific products.
I found this:
<PRODUCT_ATTRIBUTE_SET_shirts>
<reference name="product.info">
<action method="setTemplate"><template>my/custom/product/view.phtml</template></action>
</reference>
But this code is for attributes.

Well, I don't think you can do that in local.xml. But Custom Layout Updates were made for situation like that. Just write there your XML:
<reference name="product.info">
<action method="setTemplate"><template>my/custom/product/view.phtml</template></action>
</reference>
Or if you need to apply the same view.phtml for several products, you can create a new theme containing just one catalog/prodcut/view.phtml and make it extend your current theme. Then apply this theme only for products you need.

I am not entirely sure what you are asking for, but I am guessing you want to use your own view.phtml file and don't know how to have the xml layout file point to that new file.
first look for the file:
app/design/frontend/YOUR_THEME/default/layout/catalog.xml
This file essentially controls what blocks will be called within the catalog of products. If you look for the line:
<reference name="content">
<block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
You can change the template= portion to point to your custom view.phtml file. Just remember that the file structure starts at the template file:
app/design/frontend/YOUR_THEME/default/template
So you will need to store your view file within that template file if you use this method.
I hope this helps!

Related

Magento - Change Product Page Layout for only one Product in the define Layout Section

This is for sure a quicky, but nothing i found so far is working.
I would like to set a different product view for a choosen Product only. Not for a category. I tried to add this to the custom layout tab without any luck so far.
I tried any variantions of this:
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/newview.phtml</template></action>
</reference>
But Magento will not read it!
Is there any easy solution to this ? Or maybe how i can get the template added to the layout dropdown in the Design Tab of the Product ? I mean the Tab where you can choose between 1-2 or 3 Columns Layout.
Thank you very much for your help. I am using Magento 1.9.1.
In your local.xml file put the following,
<layout>
...
<PRODUCT_86>
<reference name="root">
<action method="setTemplate"><template>catalog/product/newview.phtml</template></action>
</reference>
</PRODUCT_86>
...
</layout>
Here 86 is a product_id. Flush the cache and then check.
The Solution is quite easy: Make the new File readable by the Webserver.
For what its worth, maybe it helps someone.

Magento custom head code

How can I add a custom code i.e. Facebook retargetting or conversion pixel, etc. to a section of a SPECIFIC page? (not global)
I am using the latest magento version.
Thanks
You can accomplish this cleanly using Magento’s layout system. Create or modify the local.xml file in your current theme’s layout subdirectory. Add a new layout update like this:
<!-- Add Facebook retargeting pixel on success page. -->
<checkout_onepage_success>
<reference name="before_body_end">
<block type="core/template" name="fb_retargeting" template="tracking/fb_retargeting.phtml"/>
</reference>
</checkout_onepage_success>
Notice that this layout update is targeting the <checkout_onepage_success> handle, which corresponds to the /checkout/onepage/success page. If you need to target a different page, you have to figure out the layout handle for that page. The handle is created by combining the route name, controller name, and controller method into a single underscore-separated string.
Now you just need to create your template file in your current design’s template subdirectory. In my above example, the template being referenced should be created at: app/design/frontend/.../template/tracking/fb_retargeting.phtml. You would just put the markup for your tracking pixel in that file.
1) If its going to be a CMS pages you can add in the design tab Layout Update XML and add custom xml code, for example
<reference name="head">
<block type="module/block" name="module" template="module/view.phtml" ></block>
</reference>
2) If this need to be in other pages, you can add same code in local.xml of your theme file
go to app/design/frontend/base/your theme/template/page/html/head.phtml
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
if($path == your specic page url){
your code.
}

Adding a block to magento category page using local.xml

Before posting this, i have looked in to the following, but all of them did not helped me and i was not able to add the block.
magento - adding and positioning a block using local.xml
Magento - Add phtml file to layout block
Magento 1.7:Show category image on category page in full width in a 2 column template
I want to add the category image on top of the page. Currently it is displayed under the product list view, and i want it to be above the left navigation, so that it takes full page width.
I created a template file in mytheme/template/catalog/category/image.phtml and i just added some text "I am here".
After that i add the following xml to my local.xml file under catalog_category_default
<reference name="content">
<block type="catalog/category_view" name="category.image" template="catalog/category/image.phtml"></block>
</reference>
But it is still not working and the text is not displayed there. When this text is displayed, then i will display the category image, but for now i just want to make the block working and display it on top of the page.
Thank you
Maybe your category does not load the layout handle catalog_category_default. There are 2 handles for categories _default and _layered.
To make sure you cover both cases and so you won't duplicate markup try this approach.
define your custom handle.
<my_awsome_category_handle>
<reference name="content">
<block type="catalog/category_view" name="category.image" template="catalog/category/image.phtml"></block>
</reference>
</my_awsome_category_handle>
Then include that handle in both category handles.
<catalog_category_default>
<update handle="my_awsome_category_handle" />
</catalog_category_default>
<catalog_category_layered>
<update handle="my_awsome_category_handle" />
</catalog_category_layered>

Change template page layout on cart page in Magento

Please help me on this. I want to change template page layout of cart page in magento.
The following is the URL : http://www.wildgoosetrading.com/index.php/checkout/cart/
I want this to look like other pages of category.
Thanks in advance.
You'd set the template for the cart in your checkout.xml layout file.
In the checkout_cart_index section (straight after <default>) look for the following block and change the template the cart page is using;
<!-- Mage_Checkout -->
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
EDIT
I know this is the accepted answer but soipo's answer is the better way. Apply the change to local.xml, don't modify the core layout files, override them using local.xml in your theme's layout. See http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout
In your local.xml file you can just add the following:
<checkout_cart_index translate="label">
<reference name="root">
<action method="setTemplate"><template>page/3columns.phtml</template ></action >
</reference>
Just change the template to the one you need.
By using Magento's common setTemplate method, the inherent granularity introduced by Magento's own app/design/frontend/base/default/layout/checkout.xml is lost.
Magento deliberately created two methods for doing this: setCartTemplate and setEmptyTemplate. They handle two separate conditions.
One condition is when the cart has items, the other is when the cart is empty. By calling setTemplate, that granularity is lost. That means a cart with items and a cart with zero items will both display the same template, which might be not desired.
More on this can be found here: https://stackoverflow.com/a/33875491/2973534

How to add class to sidebar div in Magento layout?

How do you add a class to a div in a Magento layout? I want to change it on only one of my pages. By default, I have:
<div class="col-left sidebar">
I want:
<div class="col-left sidebar my-class">
I can't change this in 2-columns-left because it will change on all pages of Magento. Is it possible?
If you just want to change one page, you could try copying the 2-columns-left template, renaming and editing it, then editing the page to use the new template.
Rename and edit the 2-columns-left.phtml file. This is found in /app/design/frontend/default/YOUR_THEME/template/page . At around line 50 you'll see the <div class="col-left sidebar"> line.
Edit the config.xml file so that the page uses the new template. Config.xml is in /app/code/core/Mage/Page/etc . About halfway down you'll see code referring to two_columns_left; copy this code, and edit it to point to the new page.
Finally, edit the page through the backend > CMS > Pages to use the new template. You can now add styles through the CSS in your theme.
More instructions here.
Method 1 - layout.xml :
a. For files you want to include on every page
For css or js files you want to add to every page, you edit the page.xml files located in your layout folder (app/design/frontend/default/your_theme/layout/page.xml). Within this file, at the top you will find the <default> area with the **<block name="root" … >**. This block has a child named head which contains the included css and js elements.
<block type="page/html_head" name="head" as="head">
<action method="addJs"><script>prototype/prototype.js</script></action>
<action method="addJs" ifconfig="dev/js/deprecation"><script>prototype/deprecation.js</script></action>
<action method="addJs"><script>prototype/validation.js</script></action>
<action method="addJs"><script>scriptaculous/builder.js</script></action>
...
</block>
Here you can add your javascript and css. Note that any Js files you add are relative to the “js” folder in your root directory. The css files are included from the skin files of the current and default templates (skin/frontend/default/your_template(& default)/css).
b. Specific areas
If you want to include css or js on only certain areas (such as the checkout process or catalog pages), you can do that also. For instance, if you want it in a single product view (product view vs product list), you can open up catalog.xml, find <catalog_product_view> area (near line 168 in vs 1.2.1).  Add your code in there – notice that we are using the <reference> tag rather than <block> tags. We use the “reference” tag to reference other blocks that are in other areas of the template.
<reference name="head">
<action method="addJs"><script>varien/product.js</script></action>
<action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/lang/calendar-en.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
</reference>
The use of can also be used in your layout XML areas in the admin backend (CMS pages, category and product designs). This can accomplish the same thing, as well as adding or removing other blocks.
Method 2 - Block Code :
We can accomplish all of this in code as well. These functions are defined within Mage_Page_Block_Html_Head. So, we can use this code with in a block class (not a .phtml file!):
$headBlock = $this->getLayout()->getBlock('head');
$headBlock->addJs('somefolder/yay.js');
I suggest looking over the page.xml files as long as finding the removeItem syntax ($type, $name for the method, for the xml), which will be very handy for you for adding or removing assets as and when you need them!
<action method="removeItem"><type>js</type><name>calendar/calendar.js</name</action>
$this->getLayout->getBlock('head')->removeItem('js', 'calendar/calendar.js');
The article was published : http://www.exploremagento.com/magento/adding-and-remove-js-css.php

Resources