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
  • 1
    This is very broad question. Please narrow down what exactly u want to achieve like Customer, Product, etc.. – Jackson Feb 1 '17 at 8:56
  • Particularly, I need information about product (product name, attributes) and order total amount, order items. – Vladimir Despotovic Feb 1 '17 at 9:00
  • I edited the question. My problem is that I can't get reference to the object manager. – Vladimir Despotovic Feb 1 '17 at 9:02
  • 1
    Use di for get factories You need. Eg for products: magento.stackexchange.com/questions/130185/… – Konrad Siamro Feb 1 '17 at 9:02
  • I need to get object manager. – Vladimir Despotovic Feb 1 '17 at 9:03

This is not valid class

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

Use instead

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  • ;; ? Meaning. It's not valid right? – Jackson Feb 1 '17 at 9:10
  • @AnkitShah ;; typo also that not valid i check that now. thanks for pointing out – Qaisar Satti Feb 1 '17 at 9:13

You can use below code for call model anywhere

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\Namespace\Modulename\Model\Modulename');

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