Get eCommerce attribute value by handle

Permalink 4 users found helpful
What is the best way to get an ecommerce product attribute value by handle?

jgarcia
 
jgarcia replied on at Permalink Best Answer Reply
jgarcia
In case anyone is wondering, here was the solution:

Loader::model('attribute/categories/core_commerce_product','core_commerce');
$myAttributeKey = CoreCommerceProductAttributeKey::getByHandle('myAttributeHandle');
$myAttributeValueObject = $product->getAttributeValueObject($myAttributeKey);
$attributeValue = $myAttributeValueObject->getValue();
mrnoisy replied on at Permalink Reply
mrnoisy
I get this error, even though the product attribute exists and the handle is correct:

Fatal error: Call to a member function getAttributeValueObject() on a non-object
Ricalsin replied on at Permalink Reply
Ricalsin
....yes, because the 3rd line:
$myAttributeValueObject = $product->getAttributeValueObject($myAttribute);

is incorrect - or, doesn't show the variable "$product" as being an instance of the CoreCommerceProductAttributeKey class (which might have happened early in jgarcia's code). So (your) PHP doesn't know where to look for the getAttributeValueObject method. Solve that problem and you'll be good to go.
qntndlttr replied on at Permalink Reply
qntndlttr
@Ricasin,

I'm stuck with the same problem, how do you solve that please ?


Thank you very much
enlil replied on at Permalink Reply
enlil
I was able to achieve setting $product like this within a block controller on_page_view():

$page = Page::getCurrentPage();
$pageid = $page->getCollectionID();
$db = Loader::db();
Loader::model('product/model', 'core_commerce');
$uh = Loader::helper('urls', 'core_commerce');
$productid = $db->GetOne('select productID from CoreCommerceProducts where cID = ?', array($pageid));
if ($productid > 0) {
$product = CoreCommerceProduct::getByID($productid);
}
Qurea replied on at Permalink Reply
Thanks for this sollution! I was really struggling on this :)
kirkroberts replied on at Permalink Reply
kirkroberts
Thank you! This answer helped me get a regular CollectionAttribute value. I didn't know about CollectionAttributeValueObject.