0

I am a complete Magento 2 noob. I know a lot of things changed, one of which is how to get the Singleton Mage:: instance and its methods. I know it is moved to vendor/ directory from Mage/Code/Core, but I still don't know how to call Mage:: and invoke the methods for getting the model and other things, like order total amount, and order items. I need this in order to implement a module, but also independently of the module, in a pure PHP page. How can I access Magento methods in Magento 2? I tried:

namespace Vendor\Module_Name\Block\Product;
$objectManager = Magento\Core\Model\ObjectManager::getInstance();

but it didn't work, I got the error message:

Class 'Vendor\Module_Name\Block\Product\Magento\Core\Model\ObjectManager' not found in /Applications/MAMP/htdocs/magento2/retrieveTotalOrders.php on line 8
Amit Bera
  • 77,456
  • 20
  • 123
  • 237

2 Answers2

1

This is not valid class

$objectManager = Magento\Core\Model\ObjectManager::getInstance(); 

Use instead

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
0

You can use below code for call model anywhere

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\Namespace\Modulename\Model\Modulename');
BornCoder
  • 1,498
  • 5
  • 20
  • 46