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:
- getattribute('ee_qty_custom')
- getattributetext('ee_qty_custom')
- getData('ee_qty_custom')
Here is the code for some of what I tried enter image description here
1 Answer 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!
-
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– Zyzzx2021年12月10日 13:47:09 +00:00Commented Dec 10, 2021 at 13:47
-
are you able to get default attribute values ? i.e.
geSku(). the correct one isgetEeQtyCustom()andgetattributetext('ee_custom_qty')Pawan– Pawan2021年12月10日 14:06:15 +00:00Commented 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– Zyzzx2021年12月12日 12:02:11 +00:00Commented Dec 12, 2021 at 12:02
getEeCustomQty()?Used in Product Listingset toyesinStorefront Propertiesof attribute!