cancel
Showing results for 
Search instead for 
Did you mean: 

get value of product attribute

get value of product attribute

I am trying to retrieve the value of a product attribute. I have a custom product attribute that is a text box. I would like to retrieve that value from the current product. I tried to setup a function 

public function getCurrentProductStyle()
    {
        $result = false;
        try {
            $product = $this->coreRegistry->registry('current_product');
            if (isset($product) && $product != '') {
                $result = $product->getData('acu_in_stylegroup');
            }
        } catch (\Exception $e) {
            $this->emarsysLogs->addErrorLog(
                $e->getMessage(),
                $this->storeManager->getStore()->getId(),
                'getCurrentProductSku()'
            );
        }

        return $result;
    }

After this function I then pass this function to a variable. 

$productStyleCheck = $block->getCurrentProductStyle();

Then I print the variable with a simple echo

<?php echo $productStyleCheck; ?>

This always prints nothing. I cannot retrieve the value that is entered into the text box in the admin. Any help on what I might be doing wrong would be helpful. I do not have anything that jumps out at me as incorrect with my code.

5 REPLIES 5

Re: get value of product attribute

Hello @ty_thompson1 

 

Didn't this help?

https://magento.stackexchange.com/questions/157584/magento-2-get-custom-attribute-value-of-product

 

Was my answer helpful? You can accept it as a solution.
175+ Professional Extensions for M1 & M2
Need a developer?Just visit Contact Us Now

Re: get value of product attribute

I found this post referenced post and actually tried to implement using their example. I still am unable to retrieve any value. Trying to troubleshoot to see what is preventing this.

Re: get value of product attribute

So what does 

$product->getData();

return exactly? Is your attribute there? 

If you want, you could wrap all the code into a module and put a download link? Then I could take a look. 

Re: get value of product attribute

my understanding was product->getData('') returned the attribute value of whatever attribute you placed in the single quotes.

Re: get value of product attribute

Yes, it should. But when it isn't something is not going as intended. Mostly it is a bug in your own code, but it could be caused by something else. 

dumping $product->getData() returns all the attributes, instead of just one you've put in as an argument. So when it is not in the data $product->getData(), that is a sign something is up.