Two product layout in magento - magento

In magento project, I have four categories and I have 2 template file for products
Like
1) catalog/product/view.phtml (original)
2) catalog/product/newview.phtml (new file)
Now I want to display product layout based on category
like category id 1 and 2, product display base on view.phtml
and category 3 and 4, product display based on newview.phtml

You can set this on each product in the backend in the design tab set :
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/newview.phtml</template></action>
</reference>
Else you can also perform this through an Observer to get all product from category X in one shot.
Create your own module observing the controller_action_layout_generate_blocks_after event with a function like this one :
public function generateBlocksAfter($event)
{
$controller = $event->getAction();
//limit to the product view page
if($controller->getFullActionName() != 'catalog_product_view')
{
return;
}
$layout = $controller->getLayout();
$product_info = $layout->getBlock('product.info');
if(!$product_info)
{
Mage::log('Could not find product.info block');
return;
}
$id = Mage::registry('current_product')->getId();
$prod = Mage::getModel('catalog/product')->load($id);
$category_ids = $prod->getCategoryIds();
if(in_array(3,$category_ids) || in_array(4,$category_ids))
$product_info->setTemplate('catalog/product/newview.phtml');
}

use this code for display according to categories id in local.xml files
<reference name="product_list">
<action method="setTemplate"><name>catalog/product/list_new.phtml</name></action>
</reference>
</CATEGORY_5>

Related

how to view product price in right bar in magento product details page

How to view product price of a product in right side bar,
in Magento product details page?
(my details page is 2column-right)
Any one please help me if know about it and is it possible or not
thank you
First add below code under catalog.xml (app/design/)
<catalog_product_view>
.......
<reference name="right">
<block type="catalog/product_view" name="catalog.product.rightprice" before="-" template="catalog/product/view/rightprice.phtml"/>
</reference>
</catalog_product_view>
Create a phtml(rightprice.phtml) under catalog/product/view
code of rightprice.phtml is
echo Mage::registry('current_product')->getFinalPrice();
Also get More details ,you can try this
if(Mage::registry('current_product')){
$product=Mage::registry('current_product');
$displayMinimalPrice = false;
$idSuffix = '-right';
$type_id = $product->getTypeId();
if (Mage::helper('catalog')->canApplyMsrp($product)) {
$realPriceHtml = $this->_preparePriceRenderer($type_id)
->setProduct($product)
->setDisplayMinimalPrice($displayMinimalPrice)
->setIdSuffix($idSuffix)
->setIsEmulateMode(true)
->toHtml();
$product->setAddToCartUrl($this->getAddToCartUrl($product));
$product->setRealPriceHtml($realPriceHtml);
$type_id = $this->_mapRenderer;
echo $this->_preparePriceRenderer($type_id)
->setProduct($product)
->setDisplayMinimalPrice($displayMinimalPrice)
->setIdSuffix($idSuffix)
->toHtml();
}
If any issue let me know

Magento - Set Category meta title, keywords and description

I have a category for shoes on my website. The meta title,description and keywords that I give under Catalog/Category for that category are not reflected. This is not specific to a category , I'm just quoting shoes as an example.
I dont want to use settitle,setkeyword,setdescription on layout files to set the above. Why is the default magento functionality not working.
It'd be great help if you could point out the area where the default values are applied.
For category layouts in the default magento theme, the area where the meta tags are set is :
//file: app/code/core/Mage/Catalog/Block/Category/view.php
//class: Mage_Catalog_Block_Category_View
//function: protected function _prepareLayout()
//...
$category = $this->getCurrentCategory();
if ($title = $category->getMetaTitle()) {
$headBlock->setTitle($title);
}
if ($description = $category->getMetaDescription()) {
$headBlock->setDescription($description);
}
if ($keywords = $category->getMetaKeywords()) {
$headBlock->setKeywords($keywords);
}
//...
So I think the questions to ask yourself are why is $category->getMetaTitle() falsey or what, later on in the layout, is overwriting $category->getMetaTitle() or does my theme rewrite the function Mage_Catalog_Block_Category_View::_prepareLayout().
You may want to search all your .xml files for code like this:
<reference name="head">
<action method="setTitle" translate="title" module="catalog"><title>Site Map</title></action>
</reference>
Because any module can change the meta tags using XML.

Limit number of products displayed in random products block in Magento

How can the solution provided in Magento limit the number of products shown with in the new products block be applied to a random products block displayed on the home page?
I currently have the code
<reference name="random">
<block type="catalog/product_list_random" name="product_random" template="catalog/product/grid_only.phtml">
<action method="setProductsCount"><count>3</count></action>
</block>
</reference>
in my layout update XML. The grid product view default value seems to be dominating this value which is set in the backend System > configuration > catalog > catalog > frontend > "Products per Page on Grid Default Value".
Take a look #
http://www.magentocommerce.com/boards/viewthread/54496/#t224118
Magento random product
http://dx3webs.com/front/2010/10/how-to-create-a-random-featured-product-list-on-home-page-in-magento/
Also try changing
<action method="setProductsCount"><count>3</count></action>
to
<action method="setData"><key>num_products</key><value>[# to display]</value></action>
or
<action method="setNumProducts"><num_products>[# to display]</num_products></action>
see /app/code/core/Mage/Catalog/Block/Product/List/Random.php
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
You could also try adding this to your cms page
{{block type="catalog/product_list_random" category_id="YOUR_CATEGORY_ID" template="catalog/product/list.phtml" column_count="4" num_products="12"}}
in your grid_only.phtml file you should write following code to get random products, here limit you can pass as you want, here i have giving category wise random product code.
$categoryid = 15;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$products = $category->getProductCollection();
$products->addAttributeToSelect('*');
$products->getSelect()->order('RAND()');
$products->getSelect()->limit(4);
foreach($products as $prod)
{
echo $prod->getName() ."<br>";
$img=$prod->getSmallImageUrl();
echo "<img src='$img'>" ."<br>";
}
Refer these link http://blog.magikcommerce.com/random-products-magento-home-page/
hope this will help you.
You can get latest products in magento with the following code:
<?php
$products = Mage::getModel('catalog/product')->getCollection();
//Magento does not load all attributes by default
//Add as many as you like
$products->addAttributeToSelect('name');
$products->setOrder('created_at', 'desc');
$products->getSelect()->limit(22);
foreach($products as $product) {
echo ''.$product->name.' - ';
}
Taken from here

Magento - How to add Layered Navigation to Advanced Search?

How can I add Layered Navigation to the Advanced Search result pages?
Magento Version 1.7.
The patch below will display the layered navigation in Advanced search result and will work fine with layered navigations.
The layered navigation and search result are displayed based on two separate product collections, one created by catalogsearch/Model/Layer.php and the other by catalogsearch/Model/Advanced.php.
So we need to override few functions of both these models to make layered nav work in Advanced search.
1- In your local.xml under catalogsearch_advanced_result tag add.
<reference name="left">
<block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
Override prepareProductCollection function of catalogsearch/model/Layer.php with
public function prepareProductCollection($collection){
if(Mage::helper('catalogsearch')->getQuery()->getQueryText())//for normal search we get the value from query string q=searchtext
return parent::prepareProductCollection($collection);
else{
$collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());
/**
* make sure you cross check the $_REQUEST with $attributes
*/
$attributes = Mage::getSingleton('catalog/product')->getAttributes();
Mage::log(print_r($_REQUEST,1));
foreach($attributes as $attribute){
$attribute_code = $attribute->getAttributeCode();
//Mage::log("--->>". $attribute_code);
if($attribute_code == "price")//since i am not using price attribute
continue;
if (empty($_REQUEST[$attribute_code])){
//Mage::log("nothing found--> $attribute_code");
continue;
}
if(!empty($_REQUEST[$attribute_code]) && is_array($_REQUEST[$attribute_code]))
$collection->addAttributeToFilter($attribute_code, array('in' => $_REQUEST[$attribute_code]));
else
if(!empty($_REQUEST[$attribute_code]))
$collection->addAttributeToFilter($attribute_code, array('like' => "%" . $_REQUEST[$attribute_code] . "%"));
}
$collection->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite();
//Mage::log($collection->getSelect()->__toString());
Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection($collection);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
}
return $this;
}
Override getProductCollection, getSearchCriterias function of catalogsearch/model/Advanced.php with
public function getProductCollection(){
if (is_null($this->_productCollection)) {
$this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
if(isset($_GET['cat']) && is_numeric($_GET['cat']))
$this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['cat']),true);
}
return $this->_productCollection;
}
public function getSearchCriterias()
{
$search = parent::getSearchCriterias();
/* display category filtering criteria */
if(isset($_GET['cat']) && is_numeric($_GET['cat'])) {
$category = Mage::getModel('catalog/category')->load($_GET['cat']);
$search[] = array('name'=>'Category','value'=>$category->getName());
}
return $search;
}
There is no quick solution for this. The standard search and the advanced search use two different methods to search.
If you compare the layouts in catalogsearch.xml you see that for catalogsearch_advanced_result the block catalogsearch/layer is not included. If you copy the block definition from catalogsearch_result_index and change the root template to 3columns.phtml various errors are thrown.
In my 1.6.2 the layered nav showed up after setting a 0 (Zero) to System -> Configuration -> Catalog -> Catalog Search -> Apply Layered Navigation if Search Results are Less Than
This link goes to Magento website should help. You need to create attributes from Catalogues. Then see the settings under Frontend Properties (Catalogues>Attributes).
Simply adding following line in catalogsearch.xml advance search results left area helped me to get it visible on my EE site, however I haven't checked it in CE version.
<block type="catalogsearch/layer" name="catalogsearch.leftnav" before="-" template="catalog/layer/view.phtml"/>
So my full left area looks like this on advance search area on xml file:
<reference name="left">
<block type="catalog/navigation" name="hello.leftnav" as="hello.leftnav" template="catalog/navigation/hello_left_nav-search.phtml" />
<block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>
</reference>
Hope it helps others.

Display Tiered Pricing on the Cart page

If Im on this page:
http:///checkout/cart/
With products in my cart I would like to display the tiered pricing, the same that is shown on the item page, if available.
My attempt was add
<checkout_cart_index>
<block type="catalog/product_view" name="product.tierprices" as="tierprices" template="catalog/product/view/tierprices.phtml"/>
</checkout_cart_index>
to my xml file and add
<?php echo $this->getChildHtml('tierprices') ?>
to
\app\design\frontend\enterprise\<mytemplate>\template\checkout\cart\item\default.phtml
Doesn’t do anything - any further suggestions?
It seems impossible to easily change layout. You need to modify item renderer and add tier price displaying manually. To fetch list of available tier prices you need to get price model. You can get it from product model
$product->getPriceModel()
or if don't have product model try the following code
Mage::getSingleton('catalog/product_type')->priceFactory($productTypeId)
Quote item contains product type information.
When you have price model just call method getTierPrice() to get all tier prices as array.
$priceModel->getTierPrice()
You could edit the .phtml file and adding the $this->getTierPrices($_product);//or$this->getTierPrices($_item); if you simply want to display the tier prices of products.
Do note that the getTierPrices() only works when being on the product list or product view page, so you would need to copy the getTierPrices() method that can be found inside the List.php to your custom module.
This should give you an idea what needs to be done.
layout file
<?xml version="1.0"?>
<layout version="0.1.0">
<checkout_cart_index>
<reference name="additional.product.info">
<block type="LokeyCoding_Cart/TierPrice" name="additional.product.info.tierprice" />
</reference>
</checkout_cart_index>
</version>
block file
<?php
class LokeyCoding_Cart_Block_TierPrice extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
$parent = $this->getParentBlock();
if ($parent) {
$item = $parent->getItem();
if ($item instanceof Mage_Sales_Model_Quote_Item) {
return $item->getProduct()->getTierPriceHtml();
}
}
return '';
}
}

Resources