1

i have tried so many times to get a custom attribute called 'c_color' from a product collection

here is my product collection code:

$searchstring = 'test';
$productCollection = Mage::getModel('catalog/product')
                        ->getCollection() 
                        ->addAttributeToSelect('*')
                        ->addAttributeToSelect('name')
                        ->addAttributeToSelect('c_color')
                        ->addAttributeToFilter('type_id', array('configurable','simple'))
                        ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
                        ->joinField('qty',
                 'cataloginventory/stock_item',
                 'qty',
                 'product_id=entity_id',
                 '{{table}}.stock_id=1',
                 'left');

foreach ($productCollection as $_product){

        echo 'Color: '.$_product->getResource()->getAttribute('c_color')->getFrontend()->getValue($_product).'</br>';
        echo 'Color: '.$_product->getData('c_color').'</br>';
        echo 'Color: '.$_product->C_color().'</br>';

}

$_product->getResource()->getAttribute('c_color')->getFrontend()->getValue($_product) and getData, and getC_color() are also able to get the attributes, any idea? Is the problem related to the visibility of the c_color?

update 1

  ["_data":protected] => array(35) {
    ["entity_id"] => string(6) "172358"
    ["entity_type_id"] => string(1) "4"
    ["attribute_set_id"] => string(2) "80"
    ["type_id"] => string(6) "simple"
    ["sku"] => string(14) "skuuu"
    ["has_options"] => string(1) "0"
    ["required_options"] => string(1) "0"
    ["created_at"] => string(19) "2016-08-23 06:20:54"
    ["updated_at"] => string(19) "2016-08-23 06:20:54"
    ["ordered"] => string(1) "0"
    ["viewed"] => string(1) "0"
    ["parent_id"] => string(6) "172256"
    ["c_color"] => string(4) "1478"
    ["size"] => string(3) "183"
    ["name"] => string(49) "asfsafafsafa"
    ["image"] => string(35) "abc.jpg"
    ["small_image"] => string(35) "abc.jpg"
    ["thumbnail"] => string(35) "abc.jpg"
    ["url_key"] => string(15) "yikatiyi-at0831"
    ["style"] => string(3) "904"
    ["price"] => string(8) "199.0000"
    ["cost"] => string(7) "89.7000"
    ["price_retail"] => string(8) "199.0000"
    ["brand"] => string(4) "2008"

i can getData('brand'), but i can't get getData('c_color'), any ideas?

  • Try this way ->addFieldToFilter(array(array('attribute'=>'c_color','eq'=>'label'),)) – Magento 2 Aug 24 '16 at 4:17
  • not work............ – hkguile Aug 24 '16 at 4:24
3

Your custom attribute is not visible to product listing collection, set the property 'used_in_product_listing' => true or retrieve the attribute with getAttributeRawValue() example:

$_product->getResource()->getAttributeRawValue($_product->getId(),'c_color',Mage::app()->getStore()->getStoreId());

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.