Magento display subcategories name on product page - php

I am looking at displaying the names of the sub-categories that have been selected within the admin area of Magento on the single product page.
I have the template open, but I just need to call the relevant code, any ideas?

Try this
<?php
$onCatalog = false;
if(Mage::registry('current_product')) {
$onCatalog = true;
}

Try this (assuming you are inside the product view template, view.phtml):
<?php foreach($_product->getCategoryCollection() as $_cat): ?>
<?php echo $_cat->getName() ?><br />
<?php endofreach ?>
That should get you started and get a list of the categories the product has been assigned to.
If you would rather thave the IDs:
<?php $categoryIds = $_product->getCategoryIds() // an array ?>

You could use this.
<h2>This product is in the following categories</h2>
<ul>
<?php
$categories = $_product->getCategoryCollection();
$categories->addAttributeToSelect(array('name', 'url'));
foreach ($categories as $category){
if ($category->getName() == 'Default Category' || $category->getName() == 'Categories') {
continue;
}
?>
<li><?php echo $category->getName() ?></li>
<?php } ?>
</ul>

OK, if your're in catalog>product>view.phtml or catalog>product>list.phtml
<?php foreach($_product->getCategoryCollection() as $_cat): ?>
<?php echo $_cat->getName() ?><br />
<?php endforeach ?>
Otherwise, first line should get your the product:
$_product = Mage::registry('current_product');
Would give your currently selected product.
While:
$_product = Mage::helper('catalog/product')->load(35)
Would get you product 35.

Related

echo product description in magento if the products belongs to specific category

I want to show a detailed product description after a short description on the product listing page.
I'm doing this:
<?php $_product = Mage::getModel('catalog/product')->load($productId); ?>
<?php if(in_array(428, $_product->getCategoryIds())): ?>
<span><?php echo $_product->getDescription(); ?></span>
<?php endif ?>
It doesn't echo the product description though, any ideas what's wrong?
Are you sure $_product->getCategoryIds() contains 428?
If yes I just can recomend to check you to try replace 428 with '428'. I'm not sure it will help, but...
I got it working like this in the end, in case it helps anybody else:
<?php if (Mage::registry('current_category') && Mage::registry('current_category')->getId() == 428) { ?>
<?php
$my_product = Mage::getModel('catalog/product')->load($_product->getId());
echo $my_product->getDescription();
?>
<?php } ?>

Display selected attributes on category page

I am using OpenCart v1.5.4. I want to display product attribute also on the category page. Have searched but cannot find a module or solution for this.
I am trying to add a certain attribute (one attribute group from 3, not all attributes) to category page and I am getting this error :
Notice: Undefined variable: attribute_groups in /catalog/view/theme/bigshop/template/product/category.tpl on line 79
Can anyone help?
This is the code I used on my product page and it works great but not on category page.
<?php if ($attribute_groups) { ?>
<?php foreach ($attribute_groups as $attribute_group) { ?>
<?php if ($attribute_group['name'] == 'Product Info') { ?>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<span><?php echo $attribute['name']; ?></span> <?php echo html_entity_decode($attribute['text']); ?><br />
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<!-- End Additional Info -->
</div>

Magento get CMS pages based on layout

So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route'){ ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')

Only active product count on Magento Category

I am trying to display the number of products in each category, using this code to I want to display subcategories of category id:3 . It is showing but it included disabled and invisible products.
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<?php echo $category->getName() ?>(<?php echo $category->getProductCount(); ?>)
</li>
<?php endforeach; ?>
</ul>
Is there any good solution so that I can get the exact count of categories that are Enabled and active.
you need check active filter acondition and for this you can use below code
$products = Mage::getModel('catalog/category')->load($category->getId())
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
echo $products->count();
Without any regard to Magento programming conventions, that piece of code should look like this:
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
$collection = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
?>
<ul>
<?php foreach($cats as $category): ?>
<?php $count = $collection->addCategoryFilter($category)->getSize(); ?>
<li>
<?php echo $category->getName() ?>(<?php echo $count ?>)
</li>
<?php endforeach; ?>
</ul>
But do the world a favor and organize the code properly.
Loading collection and using multiple attribute filter is not a proper way and this won't filter associated product and it's stock levels.
This piece of code with do all together,
$categoryId = 32; // Replace with your category
$category = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load($categoryId);
Mage::register('current_category', $category);
$products = Mage::getSingleton('catalog/layer')->getProductCollection();
echo $products->getSize();

Show product attributes in list.phtml - Magento

Hello I have read many posts about this, and while it works its not complete.
For example; Attribute 1= shoesize and attribute 2 = shoe color.
Both are in a dropdown and I would like to list all of the possible attribute colors per product within the category pages.
Problem: When I test the code it will only display the first shoe color, instead of all posibilites. What am I doing wrong here?
Here are 3 examples of what I have. All code work, but only shows the first attribute color.
Example 1:
<!-- Find the following loop -->
<?php foreach ($_productCollection as $_product): ?>
<!-- Inside it insert one of the following codes as needed -->
<!-- Use this for regular text attributes -->
<?php echo $_product->getMyAttribute() ?>
<?php echo $_product->getAnotherCustomAttribute() ?>
<!-- Use this for dropdown attributes -->
<?php echo $_product->getAttributeText('shoecolor') ?>
<?php endforeach?>
<!-- ... -->
Example 2
<?php echo $_product->getResource()->getAttribute('shoecolor')->getFrontend()->getValue($_product) ?>
Example 3
<?php $type = "simple"; $p = "0" ?>
<?php foreach ($_productCollection as $_product): ?>
<?php $custom = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); ?>
<?php $col = $custom->getUsedProductCollection()->addAttributeToSelect('shoecolor')->addFilterByRequiredOptions(); ?>
<?php foreach($col as $simple_product) { $p=$simple_product->getId(); $type="configurable"; } ?>
<?php if($type == "configurable"): ?>
<h5><?php echo $_product->load($p)->getAttributeText('shoecolor'); ?><?php $type="simple" ?></h5>
<?php endif; ?>
you can just config it in attribute edit page
Used in Product Listing -> Yes
Another way
$_product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
If you create an attribute like yours "shoesize" then you can access by following code.
If your attribute type Text Field ( $_product should loaded ) :
<?php
echo $_product->getShoesize();
// if your arribute was shoe_size then
echo $_product->getShoeSize();
?>
If your attribute type Multiple Select or Dropdown, to get all attribute value :
<?php
echo $_product->getAttributeText('shoesize');
?>
Here is the code get attribute name and value that that doesn't belongs to any product
$attributeCode = 'YOUR_ATTRIBUTE_CODE';
$product = Mage::getModel('catalog/product');
$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeCode);
$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes
$options = $attribute->getSource()->getAllOptions(false);
print_r($options); // print out attribute options
Try this:
$_pp2 = Mage::getModel('catalog/product')->load( $_product->getId() );
echo $_pp2->getdm();
in:
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
Attribute Code:dm
Type: Text area
in view.phtml
echo $_product->get>getdm(); ?>

Categories