我尝试了很多stuf,但没有一个工作。我想我可以在产品页面上获取自定义属性,但我想知道:如何在购物车页面中获取它们? (该属性只是一个简单的书面文字)
答案 0 :(得分:47)
$_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/
答案 1 :(得分:25)
您是在谈论自定义选项还是简单属性?
简单属性(文本):
(在你的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); ?>
答案 2 :(得分:7)
我用过这个
(在app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml
)
表示我的(textfield)属性:
<?php
$_item = $this->getItem();
$_product = $_item->getProduct()->load();
?>
<?php echo $_product->getCustomAttribute(); ?>
答案 3 :(得分:1)
这不是最好的方法,在你的属性(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" }
通过这种方式,您无需再次连接数据库并优化性能。
答案 4 :(得分:1)
从选项列表中显示所选属性:
更改: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']; }
答案 5 :(得分:0)
一种可能的方法是使用 singleton 设计模式。以下是获取属性的方法。
$_item = $this->getItem();
$_product= Mage::getSingleton('catalog/product')->load($_item->getProductId());
echo $attrValue=$_product->getAttributeText('attrCode');
答案 6 :(得分:0)
您可以执行以下操作以显示自定义产品属性,在本例中为产品视图页面上的保修产品属性。
第1步:在STORES->Attributes->Product
下的Magento后端创建新属性'保修'。
在创建属性以在“产品列表”和“查看页面”中显示属性时,在“前端产品视图页面上的可见”和“店面属性”下的“用于产品列表”选项中将值设置为“是”。
第2步:将新属性“保修”分配给STORES->Attributes->Attribute
集中设置的默认属性。
第3步:现在,您将在创建产品时看到该属性。你可以提供你想要的任何价值。
第4步:编辑catalog_product_view.xml
文件并更新以下内容。您可以在product.info.main
容器中添加此部分。您可以在product.info.overview
块旁边添加块。因此它将显示在简短描述旁边。
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.demo" template="product/view/demo.phtml" after="-">
<arguments>
<argument name="at_call" xsi:type="string">getWarranty</argument>
<argument name="at_code" xsi:type="string">warranty</argument>
<argument name="css_class" xsi:type="string">warranty </argument>
<argument name="at_label" xsi:type="string">warranty </argument>
<argument name="add_attribute" xsi:type="string">itemprop="warranty "</argument>
</arguments>
</block>
步骤5:在mageno2root / vendor / magento / module-catalog / view / frontend / templates / product / view下创建一个新文件warranty.phtml
,内容如下。
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue): ?>
<div class="product attibute <?php echo $_className?>">
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>
答案 7 :(得分:0)
在完成所有贡献之后,我采取了第一个答案,并想知道maroundo。
我找到了一个解决方案,我没有再次进行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 :(得分:0)
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>