Magento Product Attribute Get Value

后端 未结 13 1594
暗喜
暗喜 2020-12-02 05:32

How to get specific product attribute value if i know product ID without loading whole product?

相关标签:
13条回答
  • 2020-12-02 05:56

    you can use

    <?php echo $product->getAttributeText('attr_id')  ?> 
    
    0 讨论(0)
  • 2020-12-02 05:57

    $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 />';
            }
        }
    }

    all things you will use to retrieve value product custom option cart order in Magento 2: https://www.mageplaza.com/how-get-value-product-custom-option-cart-order-magento-2.html

    0 讨论(0)
  • 2020-12-02 05:58
    Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
    
    0 讨论(0)
  • 2020-12-02 06:00

    A way that I know of:

    $product->getResource()->getAttribute($attribute_code)
            ->getFrontend()->getValue($product)
    
    0 讨论(0)
  • 2020-12-02 06:01

    Please see Daniel Kocherga's answer, as it'll work for you in most cases.

    In addition to that method to get the attribute's value, you may sometimes want to get the label of a select or multiselect. In that case, I have created this method which I store in a helper class:

    /**
     * @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;
    }
    
    0 讨论(0)
  • 2020-12-02 06:02

    Try this

     $attribute = $_product->getResource()->getAttribute('custom_attribute_code');
        if ($attribute)
        {
            echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
        }
    
    0 讨论(0)
提交回复
热议问题