0

i want to add a attribute to products in the cart of my magento-shop. The problem is not all of the products have the attribute color. If a product has no attribute "color" the value of the previous product will be displayed - so f.e. the first product is white, the second has no color. Now there will be displayed PRODUCT 1 Color: white and PRODUCT 2 Color: white (actually this isn't right, it has no color at all).

Help would be appreciated!

The code i use in default.phtml:

<?php $_item = $this->getItem() ?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo 'Color: '.$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product).'<br>'; ?>
0

You can use model instead singleton or add one check as attribute exist in the product or not. Below both codes should work.

Try code 1:

<?php $_item = $this->getItem() ?>
<?php $_product= Mage::getModel('catalog/product')->load($_item->getProductId()) ?>
<?php echo 'Color: '.$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product).'<br>'; ?>

Try code 2 (Recommended):

<?php $_item = $this->getItem() ?>
<?php $_product= Mage::getModel('catalog/product')->load($_item->getProductId()) ?>

<?php $eavConfig = Mage::getModel('eav/config');
$attributes = $eavConfig->getEntityAttributeCodes(
Mage_Catalog_Model_Product::ENTITY,$_product);
if (in_array('color',$attributes)) {?>

<?php echo 'Color: '.$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product).'<br>'; ?>

<?php } ?>

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.