0

I am trying to get categories in helper file I am using Mage::() to get categories in my helper file but it is throwing error. Here is the code to my helper file.

<?php
namespace Pulsestorm\HelloWorldMVVM\Helper;
use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

public function getCategoriesDropdown(){
    $categories=Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')->addAttributeToSort('path', 'asc')->addFieldToFilter('is_active', array('eq'=>'1'));
    $first = array();
    $children = array();
    foreach ($categories->getItems() as $cat) {
        if ($cat->getLevel() == 2) {
            $first[$cat->getId()] = $cat;
        } else if ($cat->getParentId()) {
            $children[$cat->getParentId()][] = $cat->getData();
        }
    }
    return array('first' => $first, 'children' => $children);
}
}

Now i am getting this error upon executing my code.

Fatal error: Uncaught Error: Class 'Pulsestorm\HelloWorldMVVM\Helper\Mage' not found in D:\wamp\www\alo_digi_quickstart\app\code\Pulsestorm\HelloWorldMVVM\Helper\Data.php:9 Stack trace: #0

I have to use this result in my phtml file which is here.

  <?php $tree=$this->helper('Pulsestorm\HelloWorldMVVM\Helper\Data')->getCategoriesDropdown();?>
    <script type="text/javascript">
        var children = $H(<?php echo json_encode($tree['children']) ?>);

        function showCat(obj, level) {
            var catId = obj.value;
            level += 1;
            if ($('cat_container_' + level)) {
                $('cat_container_' + level).remove();
            }
            if (children.get(catId)) {
                var options = children.get(catId);
                var html = '<select id="cat_' + catId + '" onchange="showCat(this, ' + level + ')">';
                for (var i = 0; i < options.length; i++) {
                    html += '<option value="' + options[i].entity_id + '">' + options[i].name + '</option>';
                }
                html += '</select>';
                html = '<div id="cat_container_' + level + '">' + html + '</div>';

                $('sub_cat').insert(html);
            }
        }
    </script>
    <select id="first_cat" onchange="showCat(this, 2)">
        <?php foreach ($tree['first'] as $cat): ?>
            <option value="<?php echo $cat->getId() ?>"><?php echo $cat->getName() ?></option>
        <?php endforeach ?>
    </select>
    <div id="sub_cat"></div>

can you help me resolve this problem enter image description here

1
  • There is no more Mage class in magento2 Jul 29, 2019 at 13:48

2 Answers 2

0

The Mage class is no longer available in Magento 2.

Furthermore, Magento 2 would automatically reference the missing class path to the current package where it was declared.

The reason why your getting this

Fatal error: Uncaught Error: Class 'Pulsestorm\HelloWorldMVVM\Helper\Mage' not found 

is because you didn't import the correct path to the Mage class. I can tell it by looking at your "use" statements.

Please follow this link to get the category collection in Magento 2.

Remember not to use Object Manager.

2
0

I was trying to make my module from magaplaza hello world tutorial, but something went wrong. I imported code of this module https://github.com/astorm/magento2-hello-world from github and it worked. Whit help of this hello world module i created this functionality as you can see in this picture. enter image description here

This categories subcategories ajax select drop downs Module. you can check its code here https://github.com/sanaullahAhmad/Magento2_cat_subcat_ajax_select_dropdowns you can access it after installing it in aap/code directory of your magento 2 installation by following URL.. http://www.example.com/hello_mvvm/hello/world

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.