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; ?>

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');
8
  • i am getting error on search page?
    – imtiazau
    Commented Sep 12, 2019 at 7:05
  • Call to a member function getData() on null
    – imtiazau
    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?
    – imtiazau
    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

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.