Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Product attrubute call in checkout cart? 
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

Hi

I have been working with magento for a while now, and one thing I really dont get, is why getting the standard product details has to be difficult and far from straight forward???

Anyway, I need the base image from all the product added to the shopping cart.

I found this piece of code:

<?php
    
    $mageFilename 
'app/Mage.php';
    require_once 
$mageFilename;
    
    
umask(0);
    
Mage::app();

    
Mage::getSingleton('core/session', array('name'=>'frontend'));

    
$session Mage::getSingleton('checkout/session');
    
    
$output "";
    
    foreach (
$session->getQuote()->getAllItems() as $item{
        $output 
.= "Name: " $item->getName() . "<br>";
        
$output .= "Item ID: " $item->getSku() . "<br>";
        
$output .= "Description: " $item->getDescription() . "<br>";
        
$output .= "Image: " $item->getImage() . "<br>"// does not work?? 
        
$output .= "Price: " $item->getBaseCalculationPrice() . "<br>"// 
        
$output .= "<br>";
    
}
    
    
print $output;
        
    
?>

But I cant figure it out how to access the out of the box magento product attributes. Need help guys.

To be totally honest, I believe that the magento team needs to take a look at this in the future. If I need to make multi lines of code to access something as simple as attributes, I proprobly will not create a lot of extensions myselves.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

To get the product image, you need to access the Mage_Catalog_Product_Model, see docs.magentocommerce.com. You can load the product with:

// by sku
$product Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);  

// or by the product id
$product Mage::getModel('catalog/product')->load($id);

then you can access all the product attributes:

$img $product->getImageUrl ();
$imgs $product->getMediaGalleryImages () ;
$small $product->getSmallImageUrl ();
//refer to docs.magentocommerce.com for more
 
Magento Community Magento Community
Magento Community
Magento Community
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

Sweet, I will try it out smile
thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

It works! Great

But I got one more question.. If my product have more than one product image. How does it get that?

I cant get this working:
$product->getMediaGalleryImages () ;

Do I need to apply a attribute like:
->addAttributeToSelect(’gallery’)

Or how do I access all the images?

 
Magento Community Magento Community
Magento Community
Magento Community
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

Hmm.. didnt understand that..What does getYourOtherAttribute(); do?

I have this inside /template/checkout/cart.phtml

<?php
$session 
Mage::getSingleton('checkout/session');
$output "";
    foreach(
$session->getQuote()->getAllItems() as $item)
    
{
        
// getting Mage_Catalog_Product_Model
        
$productId $item->getProduct()->getId();
        
$productList Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('image')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('description')
            ->
addAttributeToSelect('price')
            ->
addIdFilter($productId);
        
        foreach(
$productList as $product)
        
{            
            $output 
.= "Name: " .  $product->getName() . "<br>";
            
$output .= "Item SKU: " .  $product->getSku() . "<br>";
            
$output .= "Description: " .  $product->getDescription() . "<br>";
            
$output .= "Price: " .  $product->getPrice() . "<br>";
            
$output .= "Image: " .  $product->getImageUrl() . "<br>";
            
$output .= "<br><br>";

        
}
    
    }
    
print $output
 ?>

In addition, I just figured out that the image isn’t the full base image but the 256x256 image. How do I get the full image?

 
Magento Community Magento Community
Magento Community
Magento Community
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

Image path is:
media/catalog/product/cache/5/image/265x/5e06319eda06f020e43594a9c230972d/…

But I need to have:
media/catalog/product/cache/5/image/5e06319eda06f020e43594a9c230972d/…

 
Magento Community Magento Community
Magento Community
Magento Community
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

thanks for your time dd_gi

foreach($product->getMediaGalleryImages() as $galleryImage){
                $output 
.= "galleryImage: $galleryImage";
            
}

That didnt work. Invalid argument :S

 
Magento Community Magento Community
Magento Community
Magento Community
 
madsm
Member
 
Total Posts:  34
Joined:  2008-09-26
Copenhagen, Denmark
 

I found this code. Where you I put it? It could solve my problem?

function addMediaGalleryAttributeToCollection(Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $_productCollection{
    $_mediaGalleryAttributeId 
Mage::getSingleton('eav/config')->getAttribute('catalog_product''media_gallery')->getAttributeId();
    
$_read Mage::getSingleton('core/resource')->getConnection('catalog_read');
    
    
$_mediaGalleryData $_read->fetchAll('
        SELECT
            main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
            `value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
            `default_value`.`position` AS `position_default`,
            `default_value`.`disabled` AS `disabled_default`
        FROM `catalog_product_entity_media_gallery` AS `main`
            LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value`
                ON main.value_id=value.value_id AND value.store_id=' 
Mage::app()->getStore()->getId() . '
            LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value`
                ON main.value_id=default_value.value_id AND default_value.store_id=0
        WHERE (
            main.attribute_id = ' 
$_read->quote($_mediaGalleryAttributeId) . ') 
            AND (main.entity_id IN (' 
$_read->quote($_productCollection->getAllIds()) . '))
        ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC    
    '
);
    
    
    
$_mediaGalleryByProductId = array();
    foreach (
$_mediaGalleryData as $_galleryImage{
        $k 
$_galleryImage['entity_id'];
        unset(
$_galleryImage['entity_id']);
        if (!isset(
$_mediaGalleryByProductId[$k])) {
            $_mediaGalleryByProductId[$k] 
= array();
        
}
        $_mediaGalleryByProductId[$k][] 
$_galleryImage;
    
}
    
unset($_mediaGalleryData);
    foreach (
$_productCollection as &$_product{
        $_productId 
$_product->getData('entity_id');
        if (isset(
$_mediaGalleryByProductId[$_productId])) {
            $_product
->setData('media_gallery', array('images' => $_mediaGalleryByProductId[$_productId]));
        
}
    }
    
unset($_mediaGalleryByProductId);
    
    return 
$_productCollection;
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top