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?

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

$myAttributeKey = CoreCommerceProductAttributeKey::getByHandle('myAttributeHandle');
$myAttributeValueObject = $product->getAttributeValueObject($myAttributeKey);
$attributeValue = $myAttributeValueObject->getValue();
fig replied on at Permalink Reply
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
....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

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

Thank you very much
enlil replied on at Permalink Reply
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
Thank you! This answer helped me get a regular CollectionAttribute value. I didn't know about CollectionAttributeValueObject.