【问题标题】:How to display custom attribute in cart (Magento)如何在购物车中显示自定义属性(Magento)
【发布时间】:2011-07-09 22:51:16
【问题描述】:

我尝试了很多东西,但它们都不起作用。我想我可以在产品页面上获取自定义属性,但我想知道:如何在购物车页面中获取它们? (属性只是简单的书面文字)

【问题讨论】:

    标签: php html magento custom-attributes


    【解决方案1】:

    $_item->getProduct()->load() 将重新加载数据库中的所有产品数据。虽然这会起作用,但请记住,每次调用 load() Magento 时都会执行数据库查询。

    同样可以通过将属性与引用项一起加载来获得更好的性能。只需创建一个自定义模块并将其添加到 config.xml

    <global>
        <sales>
            <quote>
                <item>
                    <product_attributes>
                        <one_custom_attribute_code />
                        <another_custom_attribute_code />
                    </product_attributes>
                </item>
            </quote>
        </sales>
    </global>
    

    完成后,您无需额外的数据库查询即可访问您的自定义属性。

    $_item->getProduct()->getAnotherCustomAttributeCode();
    

    这是一篇关于此的文章:https://www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/

    【讨论】:

    • 这是迄今为止最好的答案
    • 我同意拉斐尔的观点。这是最有效的方法,应该是公认的答案。
    • 绝对是最好的方法,尽管有没有办法用这种方法获取属性的选项标签而不是选项 id?
    • 您可以尝试$_item-&gt;getProduct()-&gt;getResource()-&gt;getAttribute('attribute_code')-&gt;getStoreLabel();获取标签。
    • @odd_duck $_item-&gt;getProduct()-&gt;getAttributeText('attribute_code') 应该可以解决问题。
    【解决方案2】:

    你说的是自定义选项还是简单属性?

    简单属性(文字):
    (在你的 default.phtml 中)

    <?php $_item = $this->getItem()?>
    <?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
    <?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
    

    【讨论】:

    • app/design/frontend/your_default/your_default/template/checkout/cart/item/default.phtml
    • 谢谢,效果很好:) 另外,有什么方法可以显示产品是缺货还是缺货。 (例如:如果有货,我应该使用什么代码才能显示“有货”?)
    • 我们正在谈论一个选择框,我不确定这个解决方案是否可行,但您可以尝试使用 $_product->getResource()->getAttribute('avability')->getSource( )->getValue($_product);
    • 感谢您的帮助,但那个没有用。您可以在这里找到我的第二个问题的真正解决方案:magentocommerce.com/boards/viewreply/247851 感谢您的尝试 :)
    • -1 有两个原因:(1) 在循环内调用 load()。因为 default.phtml 被包含在每个购物车项目中(Magento 循环通过),所以页面将缓慢加载大型购物车。 (2) 如果一定要在循环内加载,可以用getAttributeText('code')代替getResource()->getAttribute('code')...等获取标签文本。
    【解决方案3】:

    我用过这个

    (在app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml

    对于我的(文本字段)属性:

    <?php 
    $_item = $this->getItem();
    $_product = $_item->getProduct()->load();
    ?>
    <?php echo $_product->getCustomAttribute(); ?>
    

    【讨论】:

    • $_product = $_item->getProduct()->load($_item->getProductId());作品
    【解决方案4】:

    这不是最好的方法,在你的属性(magento/admin)中你可以设置选项:

    在结帐中可见

    所以属性转到了

    $_options Array ($_options = $this->getOptionList()) (in checkout/cart/item/default.phtml)
    

    您可以像这样使用属性(数组$_option):

    array(4) { ["label"]=> string(10) "Lieferzeit" ["value"]=> string(8) "2-3 Tage" ["print_value"]=> string(8) "2-3 Tage" ["code"]=> string(13) "delivery_time" } 
    

    通过这种方式,您无需再次连接数据库并优化性能。

    【讨论】:

    • 这比使用$_item-&gt;getProduct()-&gt;load(); 好得多。 IMO 每次使用 load 时都应该重新考虑他们在做什么 - 负载可能非常激烈,实际上会降低整体渲染性能。然而,最好的方法是上面 Andreas Riedmüller 指出的方法。
    • 后台的“结帐可见”选项在哪里?编辑属性时我没有看到此选项。我在 1.9.2.4
    【解决方案5】:

    显示从选项列表中选择的属性

    更改:app/design/frontend/base/default/template/checkout/cart/item/default.phtml

    $_customOptions = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
    
    foreach($_customOptions['attributes_info'] as $_option){ echo option['label']; }
    

    【讨论】:

      【解决方案6】:

      一种可能的方法是使用 singleton 设计模式。这是获取属性的方法。

      $_item = $this->getItem();
      $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId());
      echo $attrValue=$_product->getAttributeText('attrCode');
      

      【讨论】:

        【解决方案7】:

        在所有的贡献之后,我得到了第一个答案并想知道围绕 magento。

        我找到了一个无需再次进行 load() 的解决方案。我已经在以下路径上编辑了文件 config.xml,

        app/code/core/Mage/Sales/etc/config.xml
        

        在商品/产品属性上我添加了自定义属性

        <item>
            <product attributes>
                <sku/>
                <type_id/>
                <my_custom_attribute_id/>
        

        然后在我的 cart.phtml 文件中,我可以通过以下方式获取属性:

        $_item->getProduct()->getmy_custom_attribute_id();
        

        我不知道这是最好的还是正确的做法,但它确实解决了问题。

        干杯

        【讨论】:

          【解决方案8】:
          <?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
                          <?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
          

          【讨论】:

          • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
          猜你喜欢
          • 2013-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-01
          • 2012-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多