3

I need to convert my following Mage::getModel() from Magento1 to Magento2

$cart_api = Mage::getModel('checkout/cart_api');
$quoteId = $cart_api->create($storeObject->getStoreId());
$quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($quoteId);

$product = Mage::getModel('catalog/product');

$customer = Mage::getModel('customer/customer');
$customerAddress = Mage::getModel('customer/address');

$convertQuoteObj = Mage::getModel('sales/convert_quote');

$storeObject = Mage::getModel('core/store')->load(1);

So anyone let me know the correct method for Magento2.

0

For Mage::getModel magento1 you need to use the below code for Magento 2:

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

For Mage::getSingleton you need to use:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->get('\Namespace\Modulename\Model\Modulename');
  • I need to get all the above values in my custom php file placed in magento 2 root. So let me know how to do this for my above code? – Mesk Jul 11 '18 at 9:06
  • How can I use it in my Custom class? – Mesk Jul 11 '18 at 9:33
  • @Selvamesk, Check the link in my answer! – Pallavi Jul 11 '18 at 11:46
  • @Pallavi How can we get customer, product details in my Custom module's Index Controller? – Mesk Jul 12 '18 at 7:01
  • @Selvamesk, why you need controller for that? you can create model or helper for fetching that data. You will get so many examples in this site for fetching customer/product details. Updated My answer with helper example. – Pallavi Jul 12 '18 at 11:33
0

Override constructor in your Model class and add your model class as dependency.

protected $_modelObj;
public function __construct(
    ...
    \Vendor\Example\Model\Model $modelObj,
    ...
) {
      ...
      $this->modelObj = $modelObj;
      ...
}

function someMetod() {
    $model =  $this->modelObj->create();
}

There are thousands of examples you can find in google for same. Also you can check core files how it is implemented.

Adding here one specific answer for your question:

protected $_quoteFactory;

public function __construct(
    ...
    \Magento\Quote\Model\QuoteFactory $quoteFactory
) {
    $this->_quoteFactory = $quoteFactory;
    parent::__construct(...);
}

Then in your class you can do:

$this->_quoteFactory->create()->loadByIdWithoutStore($quoteId);

For using it in your custom class, check this link!

Hope this helps!

<?php

namespace Yourcompany\<yourmodule>\Helper {

/**
 * Customer data helper
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $customerRepository;

    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterfaceFactory $customerRepositoryFactory) {
        $this->customerRepository = $customerRepositoryFactory->create();
    }

    public LoadCustomerById($customerId) {
         $customer = $this->customerRepository->getById($customerId);
         return $customer;
    } 

}

Same way you can add functions for getting product detail and use these in your module controller. Hope this helps!

  • @Sukumar, direct use of $objectManager is not accepted as standard way of implementation. It should be done via class constructor. – Pallavi Jul 11 '18 at 7:40
  • I need to get all the above values in my custom php file placed in magento 2 root. So let me know how to do this for my above code? – Mesk Jul 11 '18 at 9:06
  • @Pallavi Mage::getModel is accepted? I am only saying what are the code for getModel and getSingleton in magento2. So Please read the question carefully as I also know use of objectManager anywhere directly is not good practice. – Sukumar Gorai Jul 11 '18 at 9:10
  • How can I use it in my Custom class? – Mesk Jul 11 '18 at 9:30
  • @SukumarGorai, My comment was not intended to offend you in any ways. However If you felt so, I apologize. My only intention was to tell the correct way of implementation, that's it. hope you understand! – Pallavi Jul 11 '18 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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