php - How to display newly added attributes in magento?

244

I added a new attribute inMagento.

 Attribute Label -> Weight Available 
 Attribute code -> weight_multi
 input type -> multiple select 
 values are -> 1,1.5,2,2.5,3,3.5,4,4.5,5

Now I want toecho theWeight Available attribute in page.

To displayName of the product we can useecho $_product->getName();

How to displayWeight Available attribute?

$_product object contain all product details.

But when I try

var_dump($_product->weight_multi); This gives

string(26) "30,29,28,27,26,25,24,23,22"

This is strange the expected value is1,1.5,2,2.5,3,3.5,4,4.5,5. How can I resolve this?? And how to display the newly added attribute in page by code.

I triedecho $_product->getWeight_multi();. But no luck..

I am using Magento 1.9.0.1.

Please help me...

512

Answer

Solution:

You can print value like this :

print_r($_product->getWeightMulti());

if you want to display attribute in product page. the go to admin panel -> catalog -> attributes ->manage attributes -> select your attribute to edit

there you will find "Visible on Product View Page on Front-end" set to yes then it will be displayed in product page.

check also in admin panel if there is any warning then refresh index and refresh cache.

132

Answer

Solution:

You have to either add the attribute to your collection filter or load each product so you have access to all of its attributes:

require_once( 'app/Mage.php' );

umask(0);
Mage::app('default');

$sCustomerId = 1;
$oQuotes = Mage::getModel( 'sales/quote' )->getCollection();
$oQuotes->addFieldToFilter( 'customer_id', $sCustomerId );
foreach( $oQuotes as $oQuote )
{
    var_dump( $oQuote->getId() );
    var_dump( $oQuote->getData( 'customer_email' ) );
    var_dump( $oQuote->getData( 'customer_id' ) );

    $oItems = Mage::getModel( 'sales/quote_item' )
        ->getCollection()
        ->setQuote( $oQuote );
    foreach( $oItems as $oItem )
    {
        $oProduct = $oItem->getProduct();
        $oProductModel = Mage::getModel( 'catalog/product' )->load( $oProduct->getId() );
        $sWeight = $oProductModel->getData( 'weight_multi' );
        var_dump( $sWeight );
        $sFormat  = $oProductModel->getAttributeText( 'weight_multi' );
        var_dump( $sFormat );
    }
}

People are also looking for solutions to the problem: php - Should I place function within foreach loop brackets?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.