Magento: Getting the category description in a layout file? - magento

I have created my own template file for category pages (like 2columns-left.phtml for example). In that file I want to display the description of the category I am in at some other place. How can I get the current categories description? I know its included in
$this->getChildHtml('content')
But its hard to get it from there. Is there another way?
Thanks!

There's always
$category = Mage::registry('current_category');
if ($category) {
$category->getDescription();
}
Ideally this would be encapsulated in a proper PHP method rather than used directly in the template.

Related

How to filter product based on specific arrribute in magento and call via cms page as well from controller

I need some tips to use product list functionality in my cms pages.
like in my cms page named "manufacture = apple" should show all products of apple manufacture .
same for other attribute like color, size etc .....
Thanks.
You need to create small module. I can't share complete code here, but sharing you a helpful link which I have earlier used and working fine.
Link:https://www.atwix.com/magento/products-list-cms/
Hope it will helpful!
create a template (test.phtml) under yourtheme/catalog/product and add the following code to your test.phtml
<?php
$arrParams = array_slice($this->getRequest()->getParams(),1);
$attribute = each($arrParams);
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter($attribute[0],$attribute[1]);
echo '<pre>';
print_r($collection->getData());
exit;
?>
than call this template to your cms page. Add the code below in content area of your cms page.
{{block type="catalog/product" template="catalog/product/test.phtml"}}
once you have the array of product than display it as per your requirement.

Joomla 2.5 Show subcategory image on blog view

I am having trouble showing the image of a subcategory on my blogview.
Furthest I've gotten is showing the image of the parent category with this line:
$this->category->getParams()->get('image');
This line shows the title of the subcategory though
$this->escape($this->item->category_title);
So I've tried adding item in that top line but it didn't take. My knowledge of Joomla and php isn't enough to figure out how to change this to show the subcategory image instead.
Any help would be much appreciated.
Edit:
To elaborate, the blog view I am using is a view of all the articles in the category 'Trends'. The category Trends however does not include any articles but all of it's subcategories do.
So the structure is as follows:
-Trends
--Trend1
---Article1
----Article2
---Trend2
---Article1
---Article2
With this blog I want to show the image of the subcategory that the article is related to. In the Joomla parameters I set the option to show the category image, however this only shows the parent category image not the subcategory image.
Solution 1:
There's no need to change the code for that.
When you are seeing "$this->category->getParams()->get('image');" in the code, it means that the template is reading parameters to whether show the image or not.
You can change the global parameters of com_content with Option button in top-right of your administrator interface, or you can change a specific category's parameters in Edit Category mode.
I can see a Category Image parameter there!
Solution 2:
Use print_r to see all the attributes of item, like this: print_r($this->item), then you will see if the item has something like category_image or not. If it contains image, use it!
I made it by creating a new instance of JCategories by catid. And then I took an image from it by getParams().
$options = array();
$categories = JCategories::getInstance('Content', $options);
$category = $categories->get($this->item->catid);
<img src="<?php echo $category->getParams()->get('image'); ?>"/>

How to change the product display order in magento

How can I change the product display order in the front end (grid or list) by setting some preferences from back-end? I guess it should be other than best value and name from the default Magento display order property.
I tried by creating a new attribute called display_order, and each product holds a value based on its value the product needs to shown in front end. However, it is not working. Please help me fix this.
You'll need to extend the Mage_Catalog_Block_Product_List block to provide your own functionality for the getProductCollection() method. Probably something along the lines of:
class ... extends Mage_Catalog_Block_Product_List {
function getProductCollection() {
parent::getProductCollection()->addAttributeToSort('display_order', 'ASC')
}
}
Then, of course, you'll have to update you layout xml file on your, presumably, custom controller (unless you want all of the product listing screens to act like this) to use your new block instead of the Magento default of catalog/product_list.
Why don't you use the Magento sorting thing ?
In your category, under Category Product you have the possibility to choose the sorting order in the last column. To do it through php, just make a custom script that you'll need to launch once.
$collection = 'Your product collection';
$result = array();
foreach ($collection as $product) {
$sort = 'Your way of calculating the desired sorting';
$result[$product->getId()]=$sort;
}
Mage::getModel('catalog/category')->load('your category id')->setPostedProducts($result)->save();
And that's it :)
To change the display order , first you need to set the default sort by option to position.This can be done from the magento admin configuration.After that you need to set position for all the products starting with the value 1.
I come across the following module which will make this task very easy, just by drag and drop the products from the manage categories itself.Please check the following extension
http://www.blackqubers.com/extensions/product-sorting-drag-and-drop.html
Hope this will helps you

Managing Magento Footer Content

I have a portion of page in footer.phtml whose content I want to change on each new page, according to different categories. How can I get this done, or can I access product attribute outside of the product page? Is this possible?
regards
You can do this by defining your own block, though the specifics depend on more detail about the requirement. Define a block that will output the footer content you want whenever you are on a page with the current category defined:
$category = Mage::registry("current_category");
if($category) {
// write your output here
}
$product = Mage::registry("current_product");
if($product) {
// write your output here
}
Note that it is generally bad practice to have your global footer change based on each page. At that point, it isn't a global footer any longer.
Thanks,
Joe

Magento: How to tell if you're on a category page, or product page in .phtml file(s)

I am trying to program into my .phtml files an if statement if the guest is on a category list page, or on a product page.
For example this code:
<?= Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>
Returns "catalog" whenever l'm on a page other than a CMS page.
Is there a way l can use a similar method to know if the user is looking at a root category, sub category or an individual product page?
Any help would be greatly appreciated!
It's been a while since I've dealt with frontend catalog pages, but give this a try.
Current versions of Magento register certain global variables (not PHP globals, but things global to the Magento system) on certain pages.
Calling the following
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
$product = Mage::registry('product');
will either return null if the objects haven't been set (i.e. you're on a page without a category or a product), or return category and product objects.
If a product object is returned you're on a product page.
If no product object is returned but a category object is, you're on a category page. Category objects have a method to getting the parent id
$category->getParentId()
A category without a parent id should be a top level category, categories with parent ids should be sub-categories.
That should give you what you need to identify where the current request is.
UPDATE: Coming back to this almost a decade later -- I likely would not rely on the contents of the registry alone to determine the page I'm on. Instead I'd use the full action name in combination with looking for the above the objects.
While Alan's answer will work, there is a more direct option, and you were actually on the right track with your code snippet... you just need to inspect the controller name rather than the module name:
<?php Mage::app()->getFrontController()->getRequest()->getControllerName(); ?>
That will return category or product based on their controllers being CategoryController.php and ProductController.php respectively.
This does assume that you've not installed any third-party modules that rewrite those controllers with their own.
I am not a big fan of checking if the current_category registry exists, because basically any controller could do this and it wouldn't necessarily mean it's a category. My way of doing it is a bit more robust:
$fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ($fullActionName == 'catalog_category_view') {
... //Category
}
elseif ($fullActionName == 'catalog_product_view') {
... //Product
}
I am afraid you are trying to do it the wrong way. I might be wrong, because you have not explained what is it exactly that you want to achieve, but I would use the layout xml to include your block on a product page with a parameter (say product-page="1") and similiarly on a category page (category-page="1").
Then you would be able to tell if you are on a product page or category page by examining those parameters inside your block:
if($this->getProductPage()) {
//this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
//this is a category page, do some stuff
}
Differentiating between main and subcategory pages might be more difficult, the first thing that comes to mind is analysis of the request variables, but that is certainly not the best approach.

Resources