简体   繁体   中英

Magento - show custom attribute in cart

I was looking for a way to display the "estimated delivery" of a product by using the attribute 'delivery' I've made.

So far I've managed to put together this:

<?php $delivery= Mage::getSingleton('catalog/product')->load($this->getProduct()->getId())->getAttributeText('Delivery'); ?>
            <?php if(isset($delivery)){
                echo $delivery;
                }
                ?>

I have added this piece to:

template/checkout/cart/item/default.phtml - between line 38/39 (Magento Version 1.6.2)

Here is the default.phtml from line 35-49 with the code added to the h2 tag:

   <h2 class="product-name">
    <?php $_item = $this->getItem()?>
        <?// Delivery - Script ?>
        <?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
        <?php $delivery= Mage::getSingleton('catalog/product')->load($this->getProduct()->getId())->getAttributeText('Delivery'); ?>
            <?php if(isset($delivery)){
                echo $delivery;
                }
                ?>      
    <?php if ($this->hasProductUrl()):?>
        <a href="<?php echo $this->getProductUrl() ?>"><?php echo $this->htmlEscape($this->getProductName()) ?></a>
    <?php else: ?>
        <?php echo $this->htmlEscape($this->getProductName()) ?>
    <?php endif; ?>
    </h2>

The problem is, the first product added to cart is being skipped, it's always showing the attribute as not set, but the second, third and rest of the products added to cart, works great, showing their est. delivery date just fine.

From here, I'm unsure how to proceed?

After some Google'ing I found this, which seems to work - But I still cannot see why, and what the big difference is, if anyone would like to clarify it, I would be very happy.

This is the working solution, it's so simple, yet I don't understand:

<?php
$custom = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $custom->getAttributeText('delivery');
?>

This does not skip the first product?

Use of singleton will result in same object being called repeatedly so previous object data will get overwritten.

Once you changed from singleton to getModel you have an instance for each product so no over writing on same instance.

app/design/frontend/{YOURTEMPLATE}/default/template/checkout/cart/item/default.phtml

Past this code:

<?php $_item = $this->getItem(); ?>
<?php $_product = $_item->getProduct()->load(); ?>
<?php $_product->get{YOUR ATTRIBUT HERE}(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM