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?

asked Sep 2, 2020 at 13:59
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();

answered Sep 2, 2020 at 15:04
2
  • A custom attribute needs getCustomAttribute()->getValue() Commented May 14, 2024 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"); Commented May 16, 2024 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)
}
answered May 14, 2024 at 22:17
-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')

answered Sep 2, 2020 at 14:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.