1

I am new with Magento. Now I'm using magento 1.9 So I add new attribute for my products via backend magento admin. I already assigned that attribute to my product's Attribute sets. the attribute code is full_bertahap_price It appeared at edit product page. And the value can saved. No problem at all enter image description here

But when I try to get the attribute's value in Frontend, it returned NULL. this is my code

<?php
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $cart = $quote->getAllVisibleItems();   
?>
<div class="row">
    <?php foreach($cart as $_item): ?>

        <?php  
             $the_item = $_item;
        ?>

    <?php endforeach ?>
    </div>
    <?php 

            $value = Mage::getModel('catalog/product')->load($the_item->getProductId());

            echo $value->getFullBertahapPrice();    ?>

I try to use

Zend_Debug::dump($value->getFullBertahapPrice());

And the result is

NULL

Can someone help me to solve this? Where I went wrong? In the admin backend that attribute has value but in Frontend its NULL? Please help !

Edit : My cache is disabled and I also already Reindex Data but nothing work.

  • just print this $the_item->getProductId() check and add result. – chirag Nov 28 '16 at 7:56
0

You have to write your code inside the foreach loop, It may be multiple item in your cart.

<?php
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $cart = $quote->getAllVisibleItems();   
?>
<div class="row">
<?php 
foreach($cart as $_item): 
    $the_item = $_item;
    $value = Mage::getModel('catalog/product')->load($the_item->getProductId());
    echo $value->getFullBertahapPrice();
endforeach 
?>
</div>
| improve this answer | |
  • Thank you for the suggestion, Ashish, but unfortunately it's not working. – user2290559 Nov 28 '16 at 9:36
  • @user2290559, If its not working then you have to check first its available in product or not with print_r($value->getData()). If Not available then update product in admin again and do Reindex. – Ashish Jagnani Nov 28 '16 at 9:40
  • It can be found inside other array but it can't be found on the 1st dimension like price or sku attribute. In the admin full_bertahap_price has the value that I input earlier (like in the pic that I post) and I also already reindex data also clear all cache via magento although I already disabled it and manually delete var/cache. But it still not shown on front end. – user2290559 Nov 28 '16 at 9:54
0

You can also try this,

$value->getData('full_bertahap_price');

or use this

$the_item->getProduct()->getId()

instead of

$the_item->getProductId()

try this also

$attribute = $value->getResource()->getAttribute('full_bertahap_price');
if ($attribute)
{
    echo $attribute_value = $attribute ->getFrontend()->getValue($value);
}

May be it will help.

| improve this answer | |
  • Thank you for your suggestion, but it still not working. Still got NULL for the result. and I try print getProductId() just like you said and it show the ID of the product. I don't get it, why backend can get the value but frontend can't? Does backend used different database or different approach? – user2290559 Nov 28 '16 at 8:28
  • try to print $value->getData() and check your attribute is there or not? – chirag Nov 28 '16 at 8:32
  • I try to use Zend_Debug again and it show arrays. I try to find it with ctrl+f and it shown 15 matches of full_bertahap_price. I try to track it and one of them shown array ["full_bertahap_price"] => object(Mage_Catalog_Model_Resource_Eav_Attribute)#249 (22) it inside an array tho. It's on the deeper array not like other attributes like price that on the first dimension of array. So it's detected or not? – user2290559 Nov 28 '16 at 9:06
  • try this $attribute = $value->getResource()->getAttribute('full_bertahap_price'); if ($attribute) { echo $attribute_value = $attribute ->getFrontend()->getValue($value); } – chirag Nov 28 '16 at 9:13
  • nope. nothing showing. so the attribute is not there? – user2290559 Nov 28 '16 at 9:23
0

Finally it works !! I don't really know how I done it actually. I turn ON the Use Flat Catalog Product in Setting Configuration Then I Reindex Flat Data and then I use this code

Mage::getModel('catalog/product')->load($the_item->getProduct()->getId())->getFullBertahapPrice();

and it works !

| improve this answer | |
0

I would like to give you a better solution. Here it is :

<?php
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $cart  = $quote->getAllVisibleItems();   
?>
<div class="row">
    <?php 
        foreach ($cart as $_item) :  
            echo $_item->getProduct()->getFullBertahapPrice();
        endforeach; 
    ?>
</div>

Why should use this?

  1. You need to echo your attribute value within foreach loop. This is because, it is possible to have more than one product available in cart
  2. Better use getProduct() call on quote item. This may avoid forceful model load in most of the case.
| improve this answer | |
  • Thank you for your answer Rajeev. Really useful information – user2290559 Nov 29 '16 at 10:53

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.