1

i have created one product custom attribute name -> 'is-buynowbutton-show' and i want to get that attribute value in product list page to show buy now based on product setting.

Does anyone suggest me how i can get product custom attributes value in product list page?

0

3 Answers 3

1

First open attribute on admin

Store -> Attributes -> Products

Then open your desired attribute is-buynowbutton-show, open tab Storefront Properties

In this tab you will find option "Used in Product Listing" make sure it is set to Yes

Now in your list phtml file you can use attribute like this

$_product->getData("is-buynowbutton-show");

Note: A piece of advice, do not use dash (-) always use underscore (_) in attribute code. so that you can be able to use it like this $_product->getIsBuynowbuttonShow();

2
  • A custom attribute needs getCustomAttribute()->getValue() May 14 at 22:17
  • getCustomAttribute()->getValue() will work on custom attributes. Like if you create attribute in customers and address attribute etc. If you create an attribute for product you can just directly get the attribute with $_product->getIsBuynowbuttonShow() or $_product->getData("is-buynowbutton-show"); May 16 at 6:56
0

ATTENTION do not use $product->getData(...)

The right way to get a custom attribute value is to run getCustomAttribute('attributecode') then getValue() on it.

Check the following code:

if ($product->getCustomAttribute('attributecode')) {
    $attributeValue = $product->getCustomAttribute('attributecode')->getValue();
    (your logic here)
}
-1

In product list page you will get the foreach loop with product colection. Each product of this loop is product object as $_product.

The you will get this value of this product custom attribute as $_product->getData('is-buynowbutton-show')

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.