0

I created multi-select attribute to display the accessories in product collateral tab in details page. I added the accessories block like below:

 <block type="catalog/product_view" name="catalog.product.accessories" template="catalog/product/accessories.phtml"> <action method="addToParentGroup"><group>detailed_info</group></action> <action method="setTitle" translate="value"><value>Accessories</value></action> </block> 

Content in the accessories.phtml file to display the image of the accessories selected is below:

 <?php $_helper = $this->helper('catalog/output'); ?> <?php $_product = $this->getProduct(); ?> <script type="text/javascript"> var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); </script> <div class="feature"> <span> <?php $Feature = explode(",",$_product->getResource() ->getAttribute('standard_accessories')->getFrontend() ->getValue($_product)); foreach($Feature as $key => $value): ?> <a href="<?php echo Mage::getBaseUrl().$value ?>" ?> <?php echo "<img src='".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."wysiwyg/".trim($value).".png'/>"; ?> </a> <?php /*echo $value; */?> <?php endforeach; ?> </span> </div> 

Now when no accessories are selected for particular product the accessories tab should not be displayed.but it is displayed with broken image like below:

enter image description here

Someone kindly help me with this issue like Where and how should the condition be given to hide the tab when it is empty?

2 Answers 2

2

You need create Accessories.php in app/code/core/Mage/Catalog/Block/Product/View

class Mage_Catalog_Block_Product_View_Accessories extends Mage_Core_Block_Template { protected $_product = null; function getProduct() { if (!$this->_product) { $this->_product = Mage::registry('product'); } return $this->_product; } } 

and then add xml in your theme catalog.xml

<block type="catalog/product_view_accessories" name="product.accessories" as="accessories" template="catalog/product/view/accessories.phtml"> <action method="addToParentGroup"><group>detailed_info</group></action> <action method="setTitle" translate="value"><value>Accessories</value></action> </block> 

and add create accessories.phtml in your theme template in path catalog/product/view and put code

<?php $_accessories = $this->getProduct()->getAccessories(); ?> <?php if ($_accessories): ?> <div class="std"> <?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_accessories, 'accessories') ?> </div> <?php endif; ?> 

and this will tab only display if accessories has content.

here is updated code for your accessories.phtml

<?php $_helper = $this->helper('catalog/output'); ?> <?php $_product = $this->getProduct(); ?> <?php $Feature = explode(",",$_product->getResource()->getAttribute('standard_accessories')->getFrontend()->getValue($_product)); ?> <?php if(count(array_filter($Feature, 'strlen')) > 0): ?> <script type="text/javascript"> var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); </script> <div class="feature"> <span> <?php foreach($Feature as $key => $value): ?> <a href="<?php echo Mage::getBaseUrl().$value ?>" ?> <?php echo "<img src='".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."wysiwyg/".trim($value).".png'/>"; ?> </a> <?php /*echo $value; */?> <?php endforeach; ?> </span> </div> <?php endif; ?> 
11
  • I followed your code.But nothing is displayed inspite of value selected. Commented Aug 6, 2016 at 9:07
  • are u added all files ? becuase i test this in my local pc. and your code in accessories.phtml that's worked ? Commented Aug 6, 2016 at 9:15
  • as my accessories.phtml is for textarea attribute so please use your code in catalog/product/view/accessories.phtml Commented Aug 6, 2016 at 9:19
  • Yea I used your php and my phtml. Now when the accessories tab is getting displayed with broken image even when value is not selected. Commented Aug 6, 2016 at 9:32
  • 1
    @Ramya i edit your file code copy and paste that will work perfect Commented Aug 6, 2016 at 11:19
0

This is of course very confusing for customers,
so the better option would be to hide attribute value’s that are empty.
This can be done with a small piece of code. Find and open the attributes.phtml file. This file can be found here:

/app/design/front end/[theme name]/[package name]/template/catalog/product/view/attribute.phtml

Open the file and search for the following lines:

 htmlEscape($this->__($_data['label'])) ?> productAttribute($_product, $_data['value'], $_data['code']) ?> 

Replace the entire foreach loop with the following lines of code:

 getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?> htmlEscape($this->__($_data['label'])) ?> productAttribute($_product, $_data['value'], $_data['code']) ?> getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?> htmlEscape($this->__($_data['label'])) ?> productAttribute($_product, $_data['value'], $_data['code']) ?> 
1
  • should I give it within the for loop or should I replace the for loop?. Because I replaced for loop and am getting error like Parse error: syntax error, unexpected 'endif' (T_ENDIF) Commented Aug 5, 2016 at 13:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.