0

I created a new drop-down custom product attribute and placed it in an attribute set. and I'm trying to retrieve its value programmatically in Magento 2. The name of said attribute is "ee_qty_custom" but for some reason, I always get either 1 or an empty array when I use any and all functions including:

  1. getattribute('ee_qty_custom')
  2. getattributetext('ee_qty_custom')
  3. getData('ee_qty_custom')

Here is the code for some of what I tried enter image description here

5
  • did you tried getEeCustomQty() ?
    – Pawan
    Commented Dec 10, 2021 at 11:44
  • @Pawan This method doesnt show up in my compiler. And the name you put doesn't match the name OR label of my product attribute.
    – Zyzzx
    Commented Dec 10, 2021 at 11:51
  • it is standard way to get attribute..just used name which you put in question.
    – Pawan
    Commented Dec 10, 2021 at 11:57
  • @Pawan if you say so, then please provide a coding answer and ill test it and if it works ill mark your answer as the correct one
    – Zyzzx
    Commented Dec 10, 2021 at 12:10
  • I have added my comment as an answer..make sure Used in Product Listing set to yes in Storefront Properties of attribute!
    – Pawan
    Commented Dec 10, 2021 at 12:33

1 Answer 1

1

You can get the atrribute value by:

<?php echo $_product->getEeCustomQty();?>
<?php echo $_product->getattributetext('ee_custom_qty');?>

First code will give you the value of option and second one will give you text. Both code should work.

Tip: If your code is not showing on front, make sure you set Used in Product Listing to yes in Storefront Properties of attribute!

3
  • Ive already tried the 2nd line and you can see in the screenshot that it didn't return ANY value. But I'm now trying the first and i wanted to ask something, given that my attribute is called "ee_qty_custom" is the function supposed to be $this->logger->debug('ok last chance: ' . $this->product->getEeQtyCustom()); or $this->logger->debug('ok last chance: ' . $this->product->getEe_Qty_Custom()); or $this->logger->debug('ok last chance: ' . $this->product->getee_qty_custom()); Please also note that im using events and observers NOT template files.
    – Zyzzx
    Commented Dec 10, 2021 at 13:47
  • are you able to get default attribute values ? i.e. geSku(). the correct one is getEeQtyCustom() and getattributetext('ee_custom_qty')
    – Pawan
    Commented Dec 10, 2021 at 14:06
  • Actually, and as i said, i was using events and observers. The event I was using only returned the accurate ORDER object not the product object. So Now, it does work but I had to do this "$product->Load($order->getItemId())" IN order to get the object of the product I wanted which in turn gave me the values I needed. So, while your answer isn't correct, its the closest one to it. Thanks for the help!
    – Zyzzx
    Commented Dec 12, 2021 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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