14

Take the following "car" product: Volkswagen Golf.

It has a multiselect attribute car_options with the following possible options and their ids:

  • Airco (id=123) selected
  • Radio (id=124) selected
  • Bluetooth (id=125)
  • Navigation (id=126)

Radio and Airco are selected for this product. How can I get those two values (radio, airco) and display them? $_product is given.

19

Try this:

echo $_product->getResource()->getAttribute('car_options')->getFrontend()->getValue($_product);
  • I get no output! This is not a dropdown, but a multiselect attribute! – SPRBRN Jun 27 '14 at 11:48
  • It should still work with a multiselect, I just tried it myself. Try var_dump($_product->getData('car_options'); and check the output. It should be a comma separated string, if it's not then I can only imagine a 3rd party module is interfering. – Mayers Jun 27 '14 at 12:41
  • Still no output. We use several modules, but I don't know of any that messes with the attributes. – SPRBRN Jun 27 '14 at 13:07
  • If $_product->getData('car_options') returns NULL, it means it's not added to your product collection. We will need to know more about where you are trying to access this attribute – Mayers Jun 27 '14 at 13:36
  • I've found a solution - see my answer. – SPRBRN Jun 27 '14 at 13:48
1

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

$attributevalues= $product->getResource()->getAttributeRawValue($product_id,'my_custom_attribute_code',$storeid);

it works.. Hope it help you..

  • Awesome...It works like a charm!!!!! – Sneha Panchal Oct 31 '18 at 5:48
  • glad to know :) @SnehaPanchal – Sameer Bhayani Oct 31 '18 at 5:50
0

The following code

  1. returns the value ids of the selected options for this product: 123,124
  2. turns that into an array: array(123,124)
  3. then finds the label of those options: 123=>Airco and 124=>Radio
  4. and returns the values in a text string: Airco, Radio
$_attribute_code = 'car_options';
$car_options_csv = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, $_attribute_code, $storeId); // returns: 123,124
$car_options = explode(',', $car_options_csv);
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product',$_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();

$res = '';
foreach($attributeOptions as $a)
{
    $l = $a['label'];
    $m = $a['value'];
    if (strlen(trim($l))>0 && in_array($m, $car_options))
    {
        $res .= trim($l) . ', ';
    }
}
echo substr($res, 0, -2);;
  • Ouch - you don't usually wanna go down that road. Especially since Mayer's answer above works fine. I'm also not sure if you're not bypassing Magento's translation system this way - haven't checked though. – workflow Oct 1 '14 at 21:46
0

Thanks @Mayers, his solution is too good not to override the native getAttributeText:

 public function getAttributeText($attributeCode)  
 {
    return $this->getResource()
        ->getAttribute($attributeCode)
        ->getFrontend()
        ->getValue($this);
 }

and to add to the customer model as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.