Show product attributes in list.phtml - Magento - php

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(); ?>

Related

How to group posts by category using PHP & MySql?

I’ve tried using GROUP BY in my query but that only returns one post per category. This is an example of what is returned with my current setup. What I need is for all posts with the same category to be grouped together, can someone point me in the right direction? Thanks for your time.
Like suggested by Charlotte, you could group your posts in an associative array, with the categories as keys.
<?php
// Connecting to database
$categories = array(); // Create an empty array
while ($post = mysqli_fetch_array($result)) {
if(!array_key_exists($post['category'], $categories)) { // Check if the category is already present
$categories[$post['category']] = array(); // Create an array indexed with the category name
}
$categories[$post['category']][] = $post; // Add the post to the category
}
?>
Now you have to iterate twice : to display the categories and to display each post in the category.
When I have to deal with nested foreach to render html, I preferably use inline foreach.
The rendering of the posts should look like :
<?php foreach ($categories as $category => $posts) : ?>
<h2><?php echo $category; ?></h2>
<?php foreach ($posts as $post) : ?>
<a href='article.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a>
<p>Posted on <?php echo date('d-m-y h:i:s',strtotime( $post['posted'] ) );?> In <a href='category5.php?id=<?php echo $post['category_id']; ?>' ><?php echo $post['category']; ?></a>
</p>
<?php endforeach; ?>
<hr/>
<?php endforeach; ?>
Edit: I ometted the opening and closing php tags in the first block of code.
If you assemble the two blocks of code in your php file, it should work.

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 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
}

Change foreach to show only first/last added image

I have following code construction. It gives out an image of the product, which was even added to the shopping card. Images appear in the adding order: the image on top is the image, which was added last.
How can i change this code so, that i get showing only one image, from recently added product (but not all images from all added products)? I guess, i need change something in foreach ($items as $item) so, that the image is showing not for each $item, but only for one $item, but don't know how to modify this code exactly...
Thanks for your help and advises!
Here is the code:
<table>
<?php $i=0; $k=0; $subtotal = 0;?>
<?php foreach ($items as $item) : ?>
<?php
$link = K2StoreItem::getK2Link($item->product_id);
$link = JRoute::_($link);
$image_path = K2StoreItem::getK2Image($item->product_id, $this->params);
?>
<tr class="row<?php echo $k; ?>">
<?php if($this->params->get('show_thumb_cart')) : ?>
<td class="warkorb2">
<?php if(!empty($image_path)) : ?>
<img src="<?php echo $image_path; ?>" class="itemImg<?php echo $this->params->get('cartimage_size','small') ?>" />
<?php endif;?>
</td>
<?php endif; ?>
</tr>
<?php ++$i; $k = (1 - $k); ?>
<?php endforeach; ?>
<table>
You can use a Query with a limit or do something like this
<?php $lastItem = end($items) ?> <!-- return the last item in the $items array -->
<?php
$link = K2StoreItem::getK2Link($lastItem->product_id);
$link = JRoute::_($link);
$image_path = K2StoreItem::getK2Image($lastItem->product_id, $this->params);
?>
in the query that you did not post you can do a LIMIT 0,1 and also add in a SORT BY .. be it id, time etc..

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.

Categories