I need to show a single attribute out of many attributes on product page. For example, I have author, publisher and format attributes for a product; and want to show only author in product page. Following code is showing all attributes in tabular format:
<?php foreach ($attribute_groups as $attribute_group) { ?>
<tbody>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<div>
<?php echo "<strong>",$attribute['name'], ":\n</strong>"; ?>
<?php echo $attribute['text']; ?>
</div>
<?php } ?>
A Better way to show single attribute for a product is to go to your product edit panel and remove all the attributes except the one you want to show
If you still want to do it in code which is not recommended then you can use and if your desired attribute
<?php foreach ($attribute_group['attribute'] as $attribute) {
if($attribute['name']=="Clockspeed") {
/* your attribute name for e.g i have used Clockspeed */
?>
<tr>
<td><?php echo $attribute['name']; ?></td>
<td><?php echo $attribute['text']; ?></td>
</tr>
<?php } } ?>
Related
I have a section of extra information for the product. In the config you can add a link. This is showing the text of the link, but we want to have a word as a link and not the url to be seen.
For the product additional information there is now 1 working link. (a word that is the link instead of showing the url)
Now for a second field (second link), I want to do the same. When this field is filled with an url, the word "second link" must be the link, but not showing the url. The word "second link" is the link
I tried to change the attributes.phtml but I get lost changing the php code.
<?php foreach ($_additional as $_data): ?>
<tr class="<?php if ($_data['value'] == "No" or $_data['value']== "Nee" or $_data['value'] == "N/A" or $_data['value'] == "Nvt" ){?>no-data-value<?php } ?>">
<th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
<td class="data">
<?php if($_data['code'] == 'link'){?>
<?php echo $this->__('Product page manufacturer')?>
<?php }else{?>
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
<?php }?>
</td>
</tr>
<?php endforeach; ?>
I am trying to do the same for the second field. How do I change this in the php? I tried several things but ends up in a loop showing it twice or i get an error in the syntax.
You may try the code below:
<?php
$links = array('link' => 'Product page manufacturer',
'YOUR_CODE_OF_SECOND_LINK' => 'TEXT_FOR_SECOND_LINK');
foreach ($_additional as $_data): ?>
<tr class="<?php if ($_data['value'] == "No" or $_data['value']== "Nee" or $_data['value'] == "N/A" or $_data['value'] == "Nvt" ){?>no-data-value<?php } ?>">
<th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
<td class="data">
<?php if(array_key_exists($_data['code'], $links)){
$code = $_data['code']; ?>
<?php echo $this->__($links[$code])?>
<?php }else{?>
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
<?php }?>
</td>
</tr>
<?php endforeach; ?>
You will be able to add links' codes and texts to the $links array. It's not very good to edit template files by this way, but it should work.
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.
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>
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..
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(); ?>