2

I am trying to get custom attribute value in frontend

I am trying below code in view.phtml , its working fine.

when i used the same code in other .phtml file its giving error :

Fatal error: Call to a member function getId() on a non-object in

<?php 

$product_id = Mage::registry('current_product')->getId();
$_product=Mage::getModel('catalog/product')->load($product_id);
$attribute = $_product->getResource()->getAttribute('cod_available');
if ($attribute)
{
    echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}
?>
13
  • this question is not clear add more information and Mage::registry('current_product') is not work out side the view.phtml file. Commented Dec 9, 2015 at 7:00
  • as you said , i replaced $product_id = $this->getProduct()->getId(); than we gave attributes value as "yes", but its displaying "NO" Commented Dec 9, 2015 at 7:02
  • it is yes and no dropdown? Commented Dec 9, 2015 at 7:05
  • yes, you are right... Commented Dec 9, 2015 at 7:05
  • there values are 1 and zero if($_product->getData('cod_available')) { echo 'available'; } this will be your condition. Commented Dec 9, 2015 at 7:09

4 Answers 4

3

Replace your code from:

<?php 

$product_id = Mage::registry('current_product')->getId();
$_product=Mage::getModel('catalog/product')->load($product_id);
$attribute = $_product->getResource()->getAttribute('cod_available');
if ($attribute)
{
    echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}
?>

To:

view.phtml

<?php 
$currentProduct = Mage::registry('current_product');
if($currentProduct) {
    $attribute = $currentProduct->getResource()->getAttribute('cod_available');
    if ($attribute) {
        echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
    }
}
?>

other .phtml (But must be available getproduct() function in block file)

<?php 

$product = $this->getProduct();
if($product->getId()) {
    $attribute = $product->getResource()->getAttribute('cod_available');
    if ($attribute) {
        echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
    }
}
?>
14
  • its giving blank .... yes or no both are not displaying.... Commented Dec 9, 2015 at 7:42
  • updated ans pls check now
    – Abdul
    Commented Dec 9, 2015 at 7:47
  • but other .phtml file use which block? are you added code in child block or main block?
    – Abdul
    Commented Dec 9, 2015 at 7:48
  • i am displying quantity, price in same page. that means getproduct() function in block file is present? Commented Dec 9, 2015 at 7:49
  • try this code :<?php $product = $this->getProduct(); if($product->getId()) { $attribute = $product->getResource()->getAttribute('cod_available'); if ($attribute) { echo $attribute_value = $attribute ->getFrontend()->getValue($_product); } } ?>
    – Abdul
    Commented Dec 9, 2015 at 7:50
1

If you using this code for product details page, please try:

$product_id = $this->getRequest()->getParam('id'); $_product=Mage::getModel('catalog/product')->load($product_id);

1
  • Product view page, its working fine. i need for other pages Commented Dec 9, 2015 at 7:02
1

when i used the same code in other .phtml file its giving error :

Fatal error: Call to a member function getId() on a non-object in

Mage::registry('current_product') 

Give Product object only on product page.your have used same code in other non-product page that's why it gives error.

8
  • what code i have to replace now... Commented Dec 9, 2015 at 7:03
  • on which page you want this ? do you get product id on that page ? Commented Dec 9, 2015 at 7:03
  • if you are getting product id then use Mage::getModel('catalog/product')->load($product_id); Commented Dec 9, 2015 at 7:04
  • this code worked for price : <input type = "text" id = "specialprice_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" "name = "price" value = "<?php echo $products->getSpecialPrice(); ?>" style = "display:none"/> Commented Dec 9, 2015 at 7:05
  • i am trying the same code..... Commented Dec 9, 2015 at 7:06
1

use this

 $product_id = $this->getProduct()->getId();
     $_product=Mage::getModel('catalog/product')->load($product_id);
$attribute = $_product->getResource()->getAttribute('cod_available');
if($_product->getData('cod_available')) { echo 'available'; }
?>
1
  • @Qaisar Satti Thanks for your support boss as always.... , +1 from me. Commented Dec 9, 2015 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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