【发布时间】:2011-08-03 09:21:26
【问题描述】:
如果我知道产品ID而不加载整个产品,如何获取特定的产品属性值?
【问题讨论】:
-
不是一个很好的答案,但您可以创建一个直接查询相应表的模型 :)
标签: magento attributes product
如果我知道产品ID而不加载整个产品,如何获取特定的产品属性值?
【问题讨论】:
标签: magento attributes product
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
【讨论】:
我知道的一种方式:
$product->getResource()->getAttribute($attribute_code)
->getFrontend()->getValue($product)
【讨论】:
$product 两次?
你可以使用
<?php echo $product->getAttributeText('attr_id') ?>
【讨论】:
$product->getData('my_name')
请参阅 Daniel Kocherga 的 answer,因为它在大多数情况下都适用。
除了获取属性值的方法之外,有时您可能还想获取select 或multiselect 的标签。在这种情况下,我创建了这个方法,并将其存储在一个辅助类中:
/**
* @param int $entityId
* @param int|string|array $attribute atrribute's ids or codes
* @param null|int|Mage_Core_Model_Store $store
*
* @return bool|null|string
* @throws Mage_Core_Exception
*/
public function getAttributeRawLabel($entityId, $attribute, $store=null) {
if (!$store) {
$store = Mage::app()->getStore();
}
$value = (string)Mage::getResourceModel('catalog/product')->getAttributeRawValue($entityId, $attribute, $store);
if (!empty($value)) {
return Mage::getModel('catalog/product')->getResource()->getAttribute($attribute)->getSource()->getOptionText($value);
}
return null;
}
【讨论】:
不加载产品模型似乎不可能获得价值。如果你看一下文件 app/code/core/Mage/Eav/Model/Entity/Attribute/Frontend/Abstract.php 你会看到方法
public function getValue(Varien_Object $object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
if (in_array($this->getConfigField('input'), array('select','boolean'))) {
$valueOption = $this->getOption($value);
if (!$valueOption) {
$opt = new Mage_Eav_Model_Entity_Attribute_Source_Boolean();
if ($options = $opt->getAllOptions()) {
foreach ($options as $option) {
if ($option['value'] == $value) {
$valueOption = $option['label'];
}
}
}
}
$value = $valueOption;
}
elseif ($this->getConfigField('input')=='multiselect') {
$value = $this->getOption($value);
if (is_array($value)) {
$value = implode(', ', $value);
}
}
return $value;
}
如您所见,此方法需要加载对象才能从中获取数据(第 3 行)。
【讨论】:
首先我们必须确保加载了所需的属性,然后将其输出。使用这个:
$product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>'));
$attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product);
【讨论】:
试试这个
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}
【讨论】:
您不必加载整个产品。 Magentos 集合非常强大和智能。
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('entity_id', $product->getId());
$collection->addAttributeToSelect('manufacturer');
$product = $collection->getFirstItem();
$manufacturer = $product->getAttributeText('manufacturer');
在您调用 getFirstItem() 的那一刻,查询将被执行并且结果产品非常小:
[status] => 1
[entity_id] => 38901
[type_id] => configurable
[attribute_set_id] => 9
[manufacturer] => 492
[manufacturer_value] => JETTE
[is_salable] => 1
[stock_item (Varien_Object)] => Array
(
[is_in_stock] => 1
)
【讨论】:
这个有效-
echo $_product->getData('ATTRIBUTE_NAME_HERE');
【讨论】:
您可以通过以下方式获取属性值
$model = Mage::getResourceModel('catalog/product');
$attribute_value = $model->getAttributeRawValue($productId, 'attribute_code', $storeId);
【讨论】:
$orderId = 1; // YOUR ORDER ID
$items = $block->getOrderItems($orderId);
foreach ($items as $item) {
$options = $item->getProductOptions();
if (isset($options['options']) && !empty($options['options'])) {
foreach ($options['options'] as $option) {
echo 'Title: ' . $option['label'] . '<br />';
echo 'ID: ' . $option['option_id'] . '<br />';
echo 'Type: ' . $option['option_type'] . '<br />';
echo 'Value: ' . $option['option_value'] . '<br />' . '<br />';
}
}
}
您将用于在 Magento 2 中检索价值产品自定义选项购物车订单的所有内容:https://www.mageplaza.com/how-get-value-product-custom-option-cart-order-magento-2.html
【讨论】:
我想你可以编写一个直接通过 sql 执行的方法。
看起来像这样:
变量:
$store_id = 1;
$product_id = 1234;
$attribute_code = 'manufacturer';
查询:
SELECT value FROM eav_attribute_option_value WHERE option_id IN (
SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET(
option_id,
(SELECT value FROM catalog_product_entity_varchar WHERE
entity_id = '$product_id' AND
attribute_id = (SELECT attribute_id FROM eav_attribute WHERE
attribute_code='$attribute_code')
)
) > 0) AND
store_id='$store_id';
您必须根据属性的 backend_type(eav_attribute 中的字段)从正确的表中获取值,因此至少需要 1 个额外的查询。
【讨论】:
如果您有一个名为 my_attr 的 text/textarea 属性,您可以通过以下方式获取它:
product->getMyAttr();
【讨论】: