Display selected attributes on category page - php

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>

Related

Get category name from Magento

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

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>

Magento static Blocks is not displaying properly.

I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.
If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}

Magento display subcategories name on product page

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.

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