i have created custom category attribute now i want to get the attribute value on list.phtml page i can i achieve this in magento2.
<?php if($category->getColThreeCategory()): ?>
<ol class="grid-product-type products list items product-items col-3-wizard">
<?php else: ?>
<ol class="grid-product-type products list items product-items">
<?php endif; ?>
Savan Patel
2,4541 gold badge19 silver badges43 bronze badges
1 Answer 1
First you need to create category custom attribute with used_in_product_listing and visible_on_front set to ture
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'col_three_category',
[
// ...
'used_in_product_listing' => true, // for category pages
'visible_on_front' => true, // for frontend??
'is_used_in_grid' => true, // for category pages
'is_visible_in_grid' => true // for category pages
]
);
Then you can get this custom attribute value like this.
$category->getColThreeCategory()
// or
$category->getData('col_three_category');
answered Sep 12, 2019 at 6:57
Narayan Jat
4652 silver badges7 bronze badges
-
i am getting error on search page?imtiazau– imtiazau2019年09月12日 07:05:39 +00:00Commented Sep 12, 2019 at 7:05
-
Call to a member function getData() on nullimtiazau– imtiazau2019年09月12日 07:10:15 +00:00Commented Sep 12, 2019 at 7:10
-
on search page it will not work becuase search page is not current category page so $category object will not work you need to add condtion like $currentCategory = $registry->registry('current_category'); if ($currentCategory) { // Custom code }Narayan Jat– Narayan Jat2019年09月12日 07:17:23 +00:00Commented Sep 12, 2019 at 7:17
-
for magento2 this code?imtiazau– imtiazau2019年09月12日 07:46:27 +00:00Commented Sep 12, 2019 at 7:46
-
how you getting this $category->getColThreeCategory() the $category object in your code ?Narayan Jat– Narayan Jat2019年09月12日 07:57:12 +00:00Commented Sep 12, 2019 at 7:57
Explore related questions
See similar questions with these tags.
default