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?
3 Answers 3
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();
-
A custom attribute needs getCustomAttribute()->getValue()Gustavo Ulyssea– Gustavo Ulyssea2024年05月14日 22:17:43 +00:00Commented 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");Shoaib Munir– Shoaib Munir2024年05月16日 06:56:53 +00:00Commented May 16, 2024 at 6:56
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)
}
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')
Explore related questions
See similar questions with these tags.