Magento 2 - Get custom attribute within .phtml file using MagentoCatalogBlockNavigation class?












2














I am trying to get a custom attribute value within my phtml file but struggling to get this to work? getData() & getCustomAttribute() doesn't seem to work. I believe this is because I am using the MagentoCatalogBlockNavigation class to loop through my categories?



Can anyone advise how I can get my custom attribute value within my loop in my product-categories.phtml?




default.xml within my theme




<block class="MagentoCatalogBlockNavigation" name="product.categories" template="Magento_Theme::html/product-categories.phtml"></block>



product-categories.phtml




<?php
if (!$block->getCategory()) {
return;
}
$_categories = $block->getCategory()->getChildrenCategories();
$_count = is_array($_categories) ? count($_categories) : $_categories->count();
if ($_count) {
?>
<ul class="list-quicklinks row med-gutter">
<?php
foreach ($_categories as $_category) {
if ($_category->getIsActive()) {
?>
<li>
<a href="<?php /* @escapeNotVerified */ echo $block->getCategoryUrl($_category) ?>" title="<?php echo $block->escapeHtml($_category->getName()) ?>"><?php echo $block->escapeHtml($_category->getName()) ?></a>
<?php
//$short_description = $_category->getData('short_category_description');
//$short_description = $_category->getCustomAttribute('short_category_description');
//$short_description = $this->helper('MagentoCatalogHelperOutput')->categoryAttribute($_category, '', 'short_category_description');
?>
<p><?php // echo $short_description; ?></p>
</li>
<?php
}
}
?>
</ul>
<?php
}
?>


UPDATE:



namespace AutosmartCategoryAttributesModel;

class Category extends MagentoCatalogModelCategory
{
public function getShortCategoryDescription()
{
//return "working";
return $this->_getAttribute('category_short_description');
}
}


I've created a modal with custom function getShortCategoryDescription() which I can call by $_category->getShortCategoryDescription() however I am unsure how to get this to return my short description attribute. The above code gives me an error:



1 exception(s):
Exception #0 (MagentoFrameworkExceptionLocalizedException): Invalid method ModuleCategoryAttributesModelCategoryInterceptor::_getAttribute









share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    2














    I am trying to get a custom attribute value within my phtml file but struggling to get this to work? getData() & getCustomAttribute() doesn't seem to work. I believe this is because I am using the MagentoCatalogBlockNavigation class to loop through my categories?



    Can anyone advise how I can get my custom attribute value within my loop in my product-categories.phtml?




    default.xml within my theme




    <block class="MagentoCatalogBlockNavigation" name="product.categories" template="Magento_Theme::html/product-categories.phtml"></block>



    product-categories.phtml




    <?php
    if (!$block->getCategory()) {
    return;
    }
    $_categories = $block->getCategory()->getChildrenCategories();
    $_count = is_array($_categories) ? count($_categories) : $_categories->count();
    if ($_count) {
    ?>
    <ul class="list-quicklinks row med-gutter">
    <?php
    foreach ($_categories as $_category) {
    if ($_category->getIsActive()) {
    ?>
    <li>
    <a href="<?php /* @escapeNotVerified */ echo $block->getCategoryUrl($_category) ?>" title="<?php echo $block->escapeHtml($_category->getName()) ?>"><?php echo $block->escapeHtml($_category->getName()) ?></a>
    <?php
    //$short_description = $_category->getData('short_category_description');
    //$short_description = $_category->getCustomAttribute('short_category_description');
    //$short_description = $this->helper('MagentoCatalogHelperOutput')->categoryAttribute($_category, '', 'short_category_description');
    ?>
    <p><?php // echo $short_description; ?></p>
    </li>
    <?php
    }
    }
    ?>
    </ul>
    <?php
    }
    ?>


    UPDATE:



    namespace AutosmartCategoryAttributesModel;

    class Category extends MagentoCatalogModelCategory
    {
    public function getShortCategoryDescription()
    {
    //return "working";
    return $this->_getAttribute('category_short_description');
    }
    }


    I've created a modal with custom function getShortCategoryDescription() which I can call by $_category->getShortCategoryDescription() however I am unsure how to get this to return my short description attribute. The above code gives me an error:



    1 exception(s):
    Exception #0 (MagentoFrameworkExceptionLocalizedException): Invalid method ModuleCategoryAttributesModelCategoryInterceptor::_getAttribute









    share|improve this question
















    bumped to the homepage by Community yesterday


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      2












      2








      2







      I am trying to get a custom attribute value within my phtml file but struggling to get this to work? getData() & getCustomAttribute() doesn't seem to work. I believe this is because I am using the MagentoCatalogBlockNavigation class to loop through my categories?



      Can anyone advise how I can get my custom attribute value within my loop in my product-categories.phtml?




      default.xml within my theme




      <block class="MagentoCatalogBlockNavigation" name="product.categories" template="Magento_Theme::html/product-categories.phtml"></block>



      product-categories.phtml




      <?php
      if (!$block->getCategory()) {
      return;
      }
      $_categories = $block->getCategory()->getChildrenCategories();
      $_count = is_array($_categories) ? count($_categories) : $_categories->count();
      if ($_count) {
      ?>
      <ul class="list-quicklinks row med-gutter">
      <?php
      foreach ($_categories as $_category) {
      if ($_category->getIsActive()) {
      ?>
      <li>
      <a href="<?php /* @escapeNotVerified */ echo $block->getCategoryUrl($_category) ?>" title="<?php echo $block->escapeHtml($_category->getName()) ?>"><?php echo $block->escapeHtml($_category->getName()) ?></a>
      <?php
      //$short_description = $_category->getData('short_category_description');
      //$short_description = $_category->getCustomAttribute('short_category_description');
      //$short_description = $this->helper('MagentoCatalogHelperOutput')->categoryAttribute($_category, '', 'short_category_description');
      ?>
      <p><?php // echo $short_description; ?></p>
      </li>
      <?php
      }
      }
      ?>
      </ul>
      <?php
      }
      ?>


      UPDATE:



      namespace AutosmartCategoryAttributesModel;

      class Category extends MagentoCatalogModelCategory
      {
      public function getShortCategoryDescription()
      {
      //return "working";
      return $this->_getAttribute('category_short_description');
      }
      }


      I've created a modal with custom function getShortCategoryDescription() which I can call by $_category->getShortCategoryDescription() however I am unsure how to get this to return my short description attribute. The above code gives me an error:



      1 exception(s):
      Exception #0 (MagentoFrameworkExceptionLocalizedException): Invalid method ModuleCategoryAttributesModelCategoryInterceptor::_getAttribute









      share|improve this question















      I am trying to get a custom attribute value within my phtml file but struggling to get this to work? getData() & getCustomAttribute() doesn't seem to work. I believe this is because I am using the MagentoCatalogBlockNavigation class to loop through my categories?



      Can anyone advise how I can get my custom attribute value within my loop in my product-categories.phtml?




      default.xml within my theme




      <block class="MagentoCatalogBlockNavigation" name="product.categories" template="Magento_Theme::html/product-categories.phtml"></block>



      product-categories.phtml




      <?php
      if (!$block->getCategory()) {
      return;
      }
      $_categories = $block->getCategory()->getChildrenCategories();
      $_count = is_array($_categories) ? count($_categories) : $_categories->count();
      if ($_count) {
      ?>
      <ul class="list-quicklinks row med-gutter">
      <?php
      foreach ($_categories as $_category) {
      if ($_category->getIsActive()) {
      ?>
      <li>
      <a href="<?php /* @escapeNotVerified */ echo $block->getCategoryUrl($_category) ?>" title="<?php echo $block->escapeHtml($_category->getName()) ?>"><?php echo $block->escapeHtml($_category->getName()) ?></a>
      <?php
      //$short_description = $_category->getData('short_category_description');
      //$short_description = $_category->getCustomAttribute('short_category_description');
      //$short_description = $this->helper('MagentoCatalogHelperOutput')->categoryAttribute($_category, '', 'short_category_description');
      ?>
      <p><?php // echo $short_description; ?></p>
      </li>
      <?php
      }
      }
      ?>
      </ul>
      <?php
      }
      ?>


      UPDATE:



      namespace AutosmartCategoryAttributesModel;

      class Category extends MagentoCatalogModelCategory
      {
      public function getShortCategoryDescription()
      {
      //return "working";
      return $this->_getAttribute('category_short_description');
      }
      }


      I've created a modal with custom function getShortCategoryDescription() which I can call by $_category->getShortCategoryDescription() however I am unsure how to get this to return my short description attribute. The above code gives me an error:



      1 exception(s):
      Exception #0 (MagentoFrameworkExceptionLocalizedException): Invalid method ModuleCategoryAttributesModelCategoryInterceptor::_getAttribute






      magento2 attributes phtml custom-attributes category-attribute






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 28 '17 at 12:13

























      asked Jun 28 '17 at 9:54









      heady12

      357422




      357422





      bumped to the homepage by Community yesterday


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community yesterday


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          We can get the custom attribute value directly calling the name by <?php echo $_category->getShortCategoryDescription(); ?>



          Still you have any doubts please check the below link
          http://www.dckap.com/blog/display-custom-attribute-value-product-viewlist-page-magento-2/






          share|improve this answer























          • '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
            – heady12
            Jun 28 '17 at 11:12










          • could you please try $_category->getShortCategoryDescription();
            – Kavitha mano
            Jun 28 '17 at 11:17










          • getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
            – heady12
            Jun 28 '17 at 11:21










          • getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
            – Kavitha mano
            Jun 28 '17 at 11:58












          • Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
            – heady12
            Jun 28 '17 at 12:16



















          0














          In your custom template try this to get the custom attribute value.



           $block->getCategory()->getChildrenCategories()->getAttribute([your_custom_attribute_code_name]);


          Hope this will help you.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f181087%2fmagento-2-get-custom-attribute-within-phtml-file-using-magento-catalog-block%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            We can get the custom attribute value directly calling the name by <?php echo $_category->getShortCategoryDescription(); ?>



            Still you have any doubts please check the below link
            http://www.dckap.com/blog/display-custom-attribute-value-product-viewlist-page-magento-2/






            share|improve this answer























            • '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
              – heady12
              Jun 28 '17 at 11:12










            • could you please try $_category->getShortCategoryDescription();
              – Kavitha mano
              Jun 28 '17 at 11:17










            • getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
              – heady12
              Jun 28 '17 at 11:21










            • getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
              – Kavitha mano
              Jun 28 '17 at 11:58












            • Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
              – heady12
              Jun 28 '17 at 12:16
















            0














            We can get the custom attribute value directly calling the name by <?php echo $_category->getShortCategoryDescription(); ?>



            Still you have any doubts please check the below link
            http://www.dckap.com/blog/display-custom-attribute-value-product-viewlist-page-magento-2/






            share|improve this answer























            • '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
              – heady12
              Jun 28 '17 at 11:12










            • could you please try $_category->getShortCategoryDescription();
              – Kavitha mano
              Jun 28 '17 at 11:17










            • getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
              – heady12
              Jun 28 '17 at 11:21










            • getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
              – Kavitha mano
              Jun 28 '17 at 11:58












            • Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
              – heady12
              Jun 28 '17 at 12:16














            0












            0








            0






            We can get the custom attribute value directly calling the name by <?php echo $_category->getShortCategoryDescription(); ?>



            Still you have any doubts please check the below link
            http://www.dckap.com/blog/display-custom-attribute-value-product-viewlist-page-magento-2/






            share|improve this answer














            We can get the custom attribute value directly calling the name by <?php echo $_category->getShortCategoryDescription(); ?>



            Still you have any doubts please check the below link
            http://www.dckap.com/blog/display-custom-attribute-value-product-viewlist-page-magento-2/







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 28 '17 at 11:16

























            answered Jun 28 '17 at 11:08









            Kavitha mano

            11610




            11610












            • '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
              – heady12
              Jun 28 '17 at 11:12










            • could you please try $_category->getShortCategoryDescription();
              – Kavitha mano
              Jun 28 '17 at 11:17










            • getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
              – heady12
              Jun 28 '17 at 11:21










            • getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
              – Kavitha mano
              Jun 28 '17 at 11:58












            • Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
              – heady12
              Jun 28 '17 at 12:16


















            • '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
              – heady12
              Jun 28 '17 at 11:12










            • could you please try $_category->getShortCategoryDescription();
              – Kavitha mano
              Jun 28 '17 at 11:17










            • getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
              – heady12
              Jun 28 '17 at 11:21










            • getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
              – Kavitha mano
              Jun 28 '17 at 11:58












            • Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
              – heady12
              Jun 28 '17 at 12:16
















            '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
            – heady12
            Jun 28 '17 at 11:12




            '$short_description = $_category->getCustomAttributeName('category_short_description');' This doesn't seem to work and the link provided is for product attributes which isn't what I am trying to retrieve? I need to get the custom category attribute that I have created through a custom module.
            – heady12
            Jun 28 '17 at 11:12












            could you please try $_category->getShortCategoryDescription();
            – Kavitha mano
            Jun 28 '17 at 11:17




            could you please try $_category->getShortCategoryDescription();
            – Kavitha mano
            Jun 28 '17 at 11:17












            getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
            – heady12
            Jun 28 '17 at 11:21




            getShortCategoryDescription() isn't a function? I can create a model for my module and create a class which would have this as a public function, however I am unsure how I would reference this in my phtml as I need to use the MagentoCatalogBlockNavigation as the block when adding the phtml file to the page to be able to look through my categories?
            – heady12
            Jun 28 '17 at 11:21












            getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
            – Kavitha mano
            Jun 28 '17 at 11:58






            getShortCategoryDescription() is not a function but can get values from the instance created in xml. while creating the custom attribute need to define in xml files with specific names like in this link gielberkers.com/adding-custom-category-attributes-magento-2-1
            – Kavitha mano
            Jun 28 '17 at 11:58














            Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
            – heady12
            Jun 28 '17 at 12:16




            Im really sorry but I am unsure what you mean? getShortCategoryDescription() was just returning nothing? I've updated my question to show my updated code. I'm unsure how I can get the custom category attribute within my public function for getShortCategoryDescription() ?
            – heady12
            Jun 28 '17 at 12:16













            0














            In your custom template try this to get the custom attribute value.



             $block->getCategory()->getChildrenCategories()->getAttribute([your_custom_attribute_code_name]);


            Hope this will help you.






            share|improve this answer


























              0














              In your custom template try this to get the custom attribute value.



               $block->getCategory()->getChildrenCategories()->getAttribute([your_custom_attribute_code_name]);


              Hope this will help you.






              share|improve this answer
























                0












                0








                0






                In your custom template try this to get the custom attribute value.



                 $block->getCategory()->getChildrenCategories()->getAttribute([your_custom_attribute_code_name]);


                Hope this will help you.






                share|improve this answer












                In your custom template try this to get the custom attribute value.



                 $block->getCategory()->getChildrenCategories()->getAttribute([your_custom_attribute_code_name]);


                Hope this will help you.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 20 '18 at 7:03









                Renu Mishra

                214




                214






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f181087%2fmagento-2-get-custom-attribute-within-phtml-file-using-magento-catalog-block%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1300-talet

                    1300-talet

                    Display a custom attribute below product name in the front-end Magento 1.9.3.8