0

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
asked Sep 12, 2019 at 5:25

1 Answer 1

0

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
8
  • i am getting error on search page? Commented Sep 12, 2019 at 7:05
  • Call to a member function getData() on null Commented 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 } Commented Sep 12, 2019 at 7:17
  • for magento2 this code? Commented Sep 12, 2019 at 7:46
  • how you getting this $category->getColThreeCategory() the $category object in your code ? Commented Sep 12, 2019 at 7:57

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.