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

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; ?>
| improve this answer | |
  • I followed your code.But nothing is displayed inspite of value selected. – Ramya Aug 6 '16 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 ? – Prashant Barot Aug 6 '16 at 9:15
  • as my accessories.phtml is for textarea attribute so please use your code in catalog/product/view/accessories.phtml – Prashant Barot Aug 6 '16 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. – Ramya Aug 6 '16 at 9:32
  • 1
    @Ramya i edit your file code copy and paste that will work perfect – Prashant Barot Aug 6 '16 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']) ?>
    
    
 
| improve this answer | |
  • 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) – Ramya Aug 5 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.