【问题标题】:Magento Product Attribute Get ValueMagento 产品属性获取价值
【发布时间】:2011-08-03 09:21:26
【问题描述】:

如果我知道产品ID而不加载整个产品,如何获取特定的产品属性值?

【问题讨论】:

  • 不是一个很好的答案,但您可以创建一个直接查询相应表的模型 :)

标签: magento attributes product


【解决方案1】:
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);

【讨论】:

  • 如何获取这个的选项文本?有人知道吗?
  • 对于重要的人:此方法仅在 1.6+ 中可用
  • 要从中获取选项文本,请参阅下面的答案:stackoverflow.com/a/30519730/327862
【解决方案2】:

我知道的一种方式:

$product->getResource()->getAttribute($attribute_code)
        ->getFrontend()->getValue($product)

【讨论】:

  • 谢谢。这就是我要找的东西。
  • 我是 magento 新手.. 为什么要使用 $product 两次?
  • @Mr_Green 因为这是 Magento。没有“为什么”得到合理的答案。
  • @Mr_Green,这并不是说要使用两次:第二行是第一行的延续。您正在对 getAttribute() 返回的对象调用 getFrontend()。
【解决方案3】:

你可以使用

<?php echo $product->getAttributeText('attr_id')  ?> 

【讨论】:

  • 这段代码运行良好。我搜索了很多博客,但除此之外没有一个代码对我有用。真不错。
  • 此代码仅在产品型号包含属性值时有效,当您只知道产品ID时它将不起作用
  • 这适用于下拉列表等属性,但如果属性是简单的文本字段,则需要不同的功能。假设属性是 my_name 然后代码变为 $product->getMyName()
  • @Ken,如果您需要从产品中获取动态属性,那么您可以使用$product-&gt;getData('my_name')
  • 我们如何获得选项 ID? $product->getAttributeId('attr_id') ???
【解决方案4】:

请参阅 Daniel Kocherga 的 answer,因为它在大多数情况下都适用。

除了获取属性值的方法之外,有时您可能还想获取selectmultiselect 的标签。在这种情况下,我创建了这个方法,并将其存储在一个辅助类中:

/**
 * @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;
}

【讨论】:

    【解决方案5】:

    不加载产品模型似乎不可能获得价值。如果你看一下文件 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 行)。

    【讨论】:

      【解决方案6】:

      首先我们必须确保加载了所需的属性,然后将其输出。使用这个:

      $product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>'));
      $attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product);
      

      【讨论】:

        【解决方案7】:

        试试这个

         $attribute = $_product->getResource()->getAttribute('custom_attribute_code');
            if ($attribute)
            {
                echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
            }
        

        【讨论】:

          【解决方案8】:

          您不必加载整个产品。 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
              )
          

          【讨论】:

            【解决方案9】:

            这个有效-

            echo $_product->getData('ATTRIBUTE_NAME_HERE');
            

            【讨论】:

            • 问题是“不加载整个产品”。
            【解决方案10】:

            您可以通过以下方式获取属性值

            $model = Mage::getResourceModel('catalog/product');
            $attribute_value = $model->getAttributeRawValue($productId, 'attribute_code', $storeId);
            

            【讨论】:

              【解决方案11】:

              $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

              【讨论】:

                【解决方案12】:

                我想你可以编写一个直接通过 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 个额外的查询。

                【讨论】:

                • 谢谢!看起来不错,但并非所有属性都是 varchar 并且 3 选择包含并不好。也许使用 join 更好?
                • 我可能错了,但据我所知,select 和 join 之间没有太大区别,因为它们是由查询处理器优化的。但是关于不是所有的属性都是varchar,你首先需要获取你想要获取的属性的bac​​kend_type,然后使用表sprintf('catalog_product_entity_%s', $backend_type) ^^。但其他类型也无法通过使用 FIND_IN_SET 获得,因此您也必须为此找到一些东西。祝你好运!
                【解决方案13】:

                如果您有一个名为 my_attr 的 text/textarea 属性,您可以通过以下方式获取它: product-&gt;getMyAttr();

                【讨论】:

                • 我问过“不加载整个产品”
                猜你喜欢
                • 2018-03-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多