Get category name from Magento - php

I'm using Luxury theme in Magento. I'm trying to display current category name in the catalog/category/view.phtml file.
What I have done so far:
<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
<div class="container">
<div class="col-md-12">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
<?php else: ?>
<strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span>| </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif ?>
</div>
I have taken this code from page/html/breadcrumbs.phtml.
I am totally new to Magento/PHP. It doesn't show any error but it's not displaying the name of the category, while it's visible in breadcrumbs header. What I am doing wrong here?

If you want to show category name on category page. You can achieve this by 2 ways.
<?php echo $this->getCurrentCategory()->getName(); ?>
Or
<?php echo Mage::registry('current_category')->getName() ?>

On our site (magento 1.9) we wanted to show the first parent category of the current product on our product pages and provide a link to it. I achieved this as follows - you should be able to reverse engineer my code to your own ends.
At first I did it by adding the following code directly to the catalog/product/view.phtml but have since migrated it into a custom helper in my own module.
Here's the code, see if it works for you.
//get an array of the IDs of every category to which the product belongs.
$categoryIds = $_product->getCategoryIds();
//set CatID to the second element of the array since the first element
//is the base category of which all others are children.
$_catID = $categoryIds[1];
//load the correct model
$_category = Mage::getModel('catalog/category')->load($_catID);
//get the level of the current category
$level = $_category->getLevel();
//This if statement prevents the function from trying to run on products
//which are not assigned to any category.
if($_category->getName()){
// we want the second level category, since the first is the base category.
// ie if we call the default category 'base' and the product is in category
//base->foo->bar->baz we want to return the link to foo.
// while current category is deeper than 2 we ask it for it's parent
//category until we reach the one we want
while ($level > 2){
$parent = $_category->getParentId();
$_category =Mage::getModel('catalog/category')->load($parent);
$level = $_category->getLevel();
}
//Now we can build the string and echo it out to the page
$caturl = $_category->getUrl_key();
$_linkstring = 'http://www.yourwebsite.com/' . $caturl . '.html';
echo 'in category:';
echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
echo ' '. $_category->getName();
echo '</a>';
}

Get the category name, image, description from category id in magento
$category = Mage::getModel('catalog/category')->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
Please put the category id in the load function

Related

How to show only the children in WordPress Categories?

I have this code for WordPress and this code only displays the entire category list. I want to make it only show the categories' children category (not display "all").
Right now I have:
Category Page 1
-child1
-child2
Category Page 2
-child1
-child2
For example that is what I want:
Category Page 1
-child1
Category Page 2
-child2
<!-- Gallery filters -->
<div id="gallery_filter">
<a href="<?php echo esc_url(get_page_link(df_get_page("gallery-1", false)));?>"<?php if(!$catSlug) { ?> class="active"<?php } ?>><?php esc_html_e("All", THEME_NAME);?></a>
<?php foreach ($categories as $category) { ?>
<?php if(isset($category->term_id)) { ?>
<a href="<?php echo esc_url(get_term_link((int)$category->term_id,DF_POST_GALLERY.'-cat'));?>"<?php if($catSlug==$category->slug) { ?> class="active"<?php } ?>><?php echo esc_html__($category->name);?></a>
<?php } ?>
<?php } ?>
</div>

How to get product category URL Key?

How can I get the product category's URL Key?
I can get the category name by $category->getName() but it does not work if I use this $category->getURLKey(),
$categories = $_product->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categories as $category) {
$productCategoryName = $category->getName();
var_dump($category->getURLKey());
}
Return null
Any ideas?
I'm not entirely sure what your goal is, but you're not going to be able to get category information from $_product. Feel free to ask questions based upon my code below. But my code below will grab the active categories children and their info. However, this should be a good enough base for what you're looking to do regardless.
<!-- Use this section if you're trying to grab children categories and their info -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name Feel Free to remove the img src section to remove the image -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- End -->
Any questions feel free to ask! Sorry, I just stole an example from a project so the HTML may not line up for a direct copy.
foreach($categories as $category) {
// for category URL key
var_dump($category->getUrlPath());
// for category URL
var_dump($category->getUrl());
}

Get Subcategory in Magento

I need help. I use this code to get the category link under products info on a few pages in Magento:
<?php $categories = $_product->getCategoryIds(); ?>
<span>In </span>
<?php $i=1; foreach($categories as $k => $_category_id): ?>
<?php if($i>1) {break;} ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a class="in-category" href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>
<?php $i++; endforeach; ?>
You can see it here: http://192.241.178.130/new_arrivals
Problem I'm having is that I want the script to display the category closest to the product but it's instead displaying the root category (Default category of the site)
M
Thanks.
Try something like this:
<?php
$categoryIds = $_product->getCategoryIds();
$categories = Mage::getModel('catalog/category')
->addAttributeToSelect('url_key')//add url key to select
->addAttributeToSelect('name')//add name to select
->getCollection() //get categories as collection
->addAttributeToFilter('entity_id', $categoryIds)//filter only by selected ids
->addAttributeToFilter('is_active', 1)//get only active categories
->addAttributeToFilter('level', array('gte'=>2))//ignore root category and 'root of roots'
->setOrder('level', 'desc');//sort by level descending
$mainCategory = $categories->getFirstItem();//get only the category lowest in the tree
if ($mainCategory->getId()) : ?>
<a class="in-category" href="<?php echo $mainCategory->getUrl() ?>"><?php echo $mainCategory->getName() ?></a>
<?php endif;?>
Instead of using a foreach to walk one time through the array you can use array_pop.
Anyhow the function getCategoryIds() will return an array of all categories the product is in. This also include parent categories and are in logical order. The category with the lowest id will show up first.
Perhaps something like this will work for you:
<?php $_category_id = array_pop($_product->getCategoryIds()); ?>
<span>In </span>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a class="in-category" href="<?php echo $_category->getUrl() ?>">
<?php echo $_category->getName() ?>
</a>

Obtain the value from a custom category attribute outside category page

On my Magento homepage I try to list the subcategories from a certain category with some values from custom category attributes in it. The list is working but I can't obtain the values from the custom category attributes.
The output is blank. What I am doing wrong? The relevant attributes are category_subtitle and category_slidertext.
<?php
$_helper = Mage::helper('catalog/category');
$productsChildren = Mage::getModel('catalog/category')->getCategories(3);
foreach ($productsChildren as $productCat) {
?>
<li>
<div class="content-wrapper">
<div class="content">
<h2><?php $_category_subtitle = $productCat->getData('category_subtitle'); if($_category_subtitle): ?><span><?php echo $_category_subtitle; ?></span><?php endif; ?></h2>
<?php $_category_slidertext = $productCat->getData('category_slidertext'); if($_category_slidertext): ?><h3><?php echo $_category_slidertext; ?></h3><?php endif; ?>
<?php echo $this->__('View our products') ?> >
</div>
</div>
</li>
<?php } ?>
Try:
$productsChildren = Mage::getModel('catalog/category')->getCategories(3,0,false,true,false);
$productsChildren->addAttributeToSelect('category_subtitle')
->addAttributeToSelect('category_slidertext');
That will work if $productsChildren is an instance of the category collection.

Magento get product special price not working on homepage

I'm editing a premium magento template right now (Hellodisplay) which has a featured product section on it's homepage. That section works by calling a specific category defined in a static block. My problem with this section is I couldn't show the special price of the products in any way. Althought the special product showed up on the product detail page. It also runs normal on the default template.
I've tried both function getSpecialProduct and getFinalProduct. GetSpecialProduct return nothing and GetFinalProduct return the normal price. I've also tried to use default theme price child html (price.phtml). It also doesn't works.
Then I check the print_r() output of $_product variable both on the homepage and also on the product page.I noticed the differences. Special array value exist only on the product pages's $_product variable. So how can I make this special price value appear on the homepage too?
This is my featured.phtml code
<?php
/**
* Product list template
*
* #see Mage_Catalog_Block_Product_List
*/
?>
<?php
$product_limit = 3;
$i = 1;
$_productCollection=$this->getLoadedProductCollection();
$cat_id = $this->category_id;
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'status'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));
?>
<?php if(!$_productCollection->count()): ?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection. Please provide a category ID.') ?>
</div>
<?php else: ?>
<ul class="frontgrid">
<?php $_collectionSize = $_productCollection->count() ?>
<?php foreach ($_productCollection as $_product): ?>
<?php if($i >= $product_limit+1){
break;
} else{
$a = $i % 3;
$i++;
} ?>
<li class="<?php echo "col".$a; ?>">
<a class="imglink" href="<?php echo $_product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200, 200); ?>" width="200" height="200" alt="<?php echo $this->htmlEscape($_product->getName()); ?>" />
</a>
<h4><?php echo $this->htmlEscape($_product->getName()); ?></h4>
<div class="boxbar">
<span class="oldprice">
<?php if($this->htmlEscape($_product->getSpecialPrice())){ ?>
€ <?php echo number_format($this->htmlEscape($_product->getPrice()), 2) ?>
<? } ?>
</span>
<span class="price">
<?php if($this->htmlEscape($_product->getSpecialPrice())){ ?>
€ <?php echo number_format($this->htmlEscape($_product->getSpecialPrice()), 2) ?><br/>
<? } else { ?>
€ <?php echo number_format($this->htmlEscape($_product->getPrice()), 2) ?>
<? } ?>
</span>
<a class="moreinfo" href="<?php echo $_product->getProductUrl() ?>">Meer Info »</a>
</div>
</li>
<?php endforeach ?>
</ul>
<?php endif; ?>
Thanks before :)
You have to add "special_price", "special_from_date" and "special_to_date" on addAttributeToSelect array.

Categories