Linking to category magento - php

I'm looking to get a product on a home page slider in magento to link to the category it is in... so far i have:
<?php
$allIds = $product->getCategoryIds();
foreach($allIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
?>
<?php echo$category->getName() ?><br/>
<?php
}
?>
(this runs within a foreach item) This provides me with the categories (which is great), however the:
<?php echo $category->getCategoryUrl() ?>
Does not seem to link to the correct place (it actually doesn't give me anything). Can anyone help on the matter?

if you want to show only one category link, you don't need to load categories in the loop:
$category = $product->getCategory();
$url = $category->getUrl();
Update: I just realized that the 1st line may not work on the homepage. But you still do not need the loop:
$category = $product->getCategoryCollection()->getFirstItem();

try this
<?php echo Mage::helper('catalog/category')->getCategoryUrl($category);?>

You will find everything related to displaying categories and subcategories here Display Categories and SubCategories in Magento. Hope this will be helpfull..

Related

Display categories list in Wordpress without subcategories

I am attempting to show categories in a list, but without displaying subcatgories. My current code is:
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul>";
wp_list_categories('orderby=id&show_count=0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
echo "</ul>";
}
}?>
Which displays the categories nicely
but when I added a sub-category it looked like this:
Any ideas? Thanks!
Use get_categories() instead.
https://developer.wordpress.org/reference/functions/get_categories/#Get_only_top_level_categories
It has "parent" parameter which you can set to 0 and get desired result.
looks like a css problem to me : you probably want to hide the subcategory item until it's clicked or hovered

Magento: List product categories EXCEPT a select few

I am looking to list on the product page the categories that a particular product belongs to; HOWEVER, I would like to be able to say do NOT list some specific categories.
A solution to outputting the list of categories a product belonged to was posted by a fellow stackoverflow user here: https://stackoverflow.com/a/9720480/99112 and it works great to output the results. How could the above code be modified to get what we are looking for?
Just to draw up an example of what I mean:
Say a Product A is a member of Category IDs: 4, 7, 9, 14, 92
On the product page, I want to output the names of the above categories MINUS category IDs: 7, 92
So, the output would only be Category ID names for: 4, 9, 14
The categories we would want to exempt would apply to all products. So in the above example and looking at a Product B, it would also output Category ID names EXCEPT for the ones we don't want (i.e. 7, 92).
Here is the code in question from the above thread (thanks to user "Sarath Tomy"):
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php echo $_category->getName() ?>
<?php endforeach; ?>
How would we modify this to check a list of Category IDs that we do NOT want outputted, please?
Many thanks.
Here's a solution that worked (no idea if it's the most efficient way of doing it or not), but here it is:
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php if (!in_array($_category->getId(), array(XX,YY))) : ?>
<?php echo $_category->getName() ?>
<?php endif; ?>
<?php endforeach; ?>
where you put in whatever Category IDs you want to exclude in place of XX,YY above.
You could make a System - Configuration setting that contains a multiple select with all the categories.
The categories selected would be the ones excluded.
Now, the best practice would be to extend Mage_Catalog_Model_Product, and add another method getVisibleCategoryIds()
Here's how it would look (not tested):
public function getVisibleCategoryIds() {
return array_diff($this->getCategoryIds(), Mage::getStoreConfig('section_name/group/field'));
}
Or you can extend getCategoryIds() directly and save the hassle of modifying through templates. This would look like this:
public function getCategoryIds() {
return array_diff(parent::getCategoryIds(), Mage::getStoreConfig('section_name/group/field'));
}

Getting the last sub-category of a product in magento

I having been searching for days to get the last subcategory of a product in magento.
Actually what i have to do is display the last subcategory the product is placed in. for example i have plastic and glass as products. I want to display the last subcategory i.e cups or plates.
|Party
--|boys
----|batman
--------|cups
-----------|plastic
-----------|glass
--------|plates
----|Superman
i have edited the list.phtml file, i can get the category id's and name from the array but they are all mixed up. So there is no way to figure out which one is the last category.
Is there any default functionality in magento? or someone be kind enough to help me out?
Thanks in advance.
Edit
Okay, from your description it sounds like you want to get the child categories from the current category you're in. (I.E. Get cups and plates while in batman category view).
The following should be just about as little as you need to get the current children.
<?php
$_helper = Mage::helper('catalog/category');
$children = Mage::registry( 'current_category' )->getChildrenCategories();
?>
<ul>
<?php foreach( $children as $child ): ?>
<li><?php echo $child->getName() ?></li>
<?php endforeach; ?>
</ul>
Previous Answer(s)
It's a little roundabout, but this can get you the parent category id from a product object.
//If you don't have a product to start with, load by ID.
$_product = Mage::getModel( 'catalog/product' )->load($id);
//Assign Category Model
$categoryModel = Mage::getModel( 'catalog/category' );
//Get Array of Category Id's with Last as First (Reversed)
$_categories = array_reverse( $_product->getCategoryIds() );
//Get Parent Category Id
$_parentId = $categoryModel->load($_categories[0])->getParentId();
//Load Parent Category
$_category = $categoryModel->load($_parentId);
//Do something with $_category
echo $_category->getName();
It works better when the product only has one category Id assigned to it. You may not get the category you want if multiple categories have been assigned to that one product.
Also, you can get it a slightly quicker way, but this method doesn't work if the product was searched for:
$_category = Mage::getModel( 'catalog/category' )->load( Mage::registry( 'current_category' )->getParentId() );
I didn't test the line above, but it should work. Only if the product was reached by browsing through the categories though.

Manufacturer of a product Magento

I started off with magento, and got stuck at a very odd problem,
I have one root category, two child catgories, each having subcategories and products, i want manufacturer list from each of the two child categories which are under the root category.
i started of with giving root category id as 3.
please look at the code below..
$root=3;
$rootcategories=Mage::getModel('catalog/category')->getCategories($root,1,false,true,false);
foreach($rootcategories as $c=>$Cat){
$product = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($Cat);
foreach($product as $pro){
$pId=$pr['entity_id'];
$_product=Mage::getModel('catalog/product')->load($pId);
$manufacturers[$c][]=$_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product);
}
}
echo '<pre>';
print_r($manufacturers);
die;
i'm supposed to get list of manufacturers from this code, but i get a 'NO' for each product in each of the category.
you don't make use of your $pro variable in your foreach loop :)
foreach($product as $pro) {
echo $pro->getManufacturers;
}
Try this. Work everywhere.
$manufacturer = Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('manufacturer');
Why don't you simply do
$_product->getData('manufacturer');
??
Try
$root=3;
$categories = Mage::getModel('catalog/category')->load($root)->getChildrenCategories();
foreach($categories AS $cat)
{
$productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cat);
foreach($productCollection AS $product)
{
echo $product->getAttributeText('manufacturer').'<br/>';
}
}
Something like that. Hope this helps.

Change sort order of sub categories in magento

I have some code that calls the sub categories of a main category and I need to be able to change the sort order of teh sub categories on the front end of the site.
I have tried adding the attribute to sort tag but this isnt doing anything. Can anyone help point me in the right direction to get this working. Many Thanks:
->addAttributeToSort(’position’, ‘asc’)
This is isnt having any affect on the order. The code i'm using is below:
<?php
//get the current category
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();
//get the children of the current category
$subCats = Mage::getModel('catalog/category')->load($currentCat->getId())->getChildren();
//get sub category ids
$subCatIds = explode(',',$subCats);
?>
<?php if (count($subCatIds) > 1): ?>
<?php foreach($subCatIds as $subCatId): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?>
<?php if($subCat->getIsActive()): ?>
#Jason Millward
Please do not call load() to every object. It will affect site performance in the nearest future ;)
I created an example for you.
$currentCategory = Mage::getModel('catalog/category')->load(3);
$collection = $currentCategory->getCollection();
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addIdFilter($currentCategory->getChildren())
->setOrder('position', Varien_Db_Select::SQL_ASC)
->load();
category entity already have position attribute that is managed from admin.
Just use it to order categories.
You can try this:
you can write the code after $subCats
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $subCats))
->addAttributeToSelect('entity_id');
if($collection)
{
$subCatIds = $collection->setOrder('position', 'asc');
}
after doing foreach $subCatIds you can go with the id. Hope this will help ...
If you want to load the sub categories of specific category with ordering by position. Please try this:
/** #var Mage_Catalog_Model_Resource_Category_Flat_Collection $storeCategories */
$storeCategories = Mage::getModel('catalog/category')->getCategories(
$currentCategory->getId(), 0, false, true, false
);
$storeCategories->unshiftOrder('position', Varien_Db_Select::SQL_ASC);
Notify that we must use unshiftOrder instead of setOrder. Otherwise, before loading the collection, it will be set ordering by name first.
I was reference to the top menu to see how it work. Hope this will help somebody!

Categories