简体   繁体   English

Magento - 在购物车中显示自定义属性

[英]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) template / checkout / cart / item / default.phtml - 第38/39行之间(Magento版本1.6.2)

Here is the default.phtml from line 35-49 with the code added to the h2 tag: 这是第35-49行的default.phtml,代码添加到h2标记:

   <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. 一旦你从singleton变为getModel,你就有了每个产品的实例,所以不要在同一个实例上写过。

app/design/frontend/{YOURTEMPLATE}/default/template/checkout/cart/item/default.phtml 应用程序/设计/前端/ {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}(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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