Questions tagged [object-manager]

Denotes question regarding Magento 2 Object Manager

Filter by
Sorted by
Tagged with
1 vote
1 answer
2k views

How is the array $data parameter set in Magento 2 (dataObject constructor)

In https://github.com/SnowdogApps/magento2-menu/blob/master/Block/Menu.php, a plugin that makes it possible to replace the default menu, the constructor of the block looks like below. For all ...
user5198077's user avatar
-2 votes
2 answers
3k views

How to get customer id using object manager in Magento 2 [closed]

How to get customer id using object manager?
Shyam Hajare's user avatar
2 votes
1 answer
3k views

Get plain value from custom variable in php with objectManager

I want to get a plain value from custom variable (defined on admin) in php with objectManager. storeID is in $order->getStore()->getId() Custom Variable Code is 'mycustomvar' why does ...
rico's user avatar
  • 31
8 votes
10 answers
80k views

How to load product collection in Magento 2

How to load product collection in Magento 2 and what is the difference between Magento 1 and Magento 2 load product collection
jignesh prajapati's user avatar
1 vote
2 answers
2k views

Magento2 Passing Arguments to class in Construct

I am trying to instantiate the below class in my custom code \Magento\Solr\Model\Client\Solarium like public function __construct( \Magento\Solr\Model\Client\Solarium $solr ) {....}...
blakcaps's user avatar
  • 1,965
3 votes
2 answers
3k views

Get up-sell product in Magento 2

I am trying to get up-sell product in Magento 2 via objectManager. Current product id and name is returning well but up-sell product return empty. <?php $objectManager = \Magento\Framework\...
sanjay's user avatar
  • 377
-1 votes
2 answers
203 views

Magento 2 : what are the place use object manager. what are the place need to avoid? [duplicate]

i have written code using object manager in Block. i am not sure it is best practice. what are the place use object manager. what are the place we need to avoid Any one suggest please
Rama Chandran M's user avatar
3 votes
2 answers
3k views

How Magento2 creates object of factory classes

I would like to know how magento is using factory class to create an object i.e, when we use $classFactory->create() function then it return object of the class. How from which set of code does ...
arushi's user avatar
  • 790
1 vote
1 answer
674 views

Custom controller: Argument 1 must be instance of Context, instead Objectmanager is given

Created a module which was working for a long time, however after some other edits to the site I got this error: "Recoverable Error: Argument 1 passed to vendor\module\Controller\Cart\Index::...
perrivdb's user avatar
  • 149
3 votes
1 answer
11k views

How to create a productrepository using object manager in Magento 2?

I want to save/update the product in phtml file and I need to create productRepository using object manager. I don't want any controller to be called. Is there any possibility to save product in phtml ...
Mandar Parbate's user avatar
2 votes
2 answers
4k views

Magento2: Checking if a module exists, after being injected via class constructor

I know I can achieve this with object Manager - but just wanted to see if there is a better way. Perhaps this case is a good candidate for using the object Manager; however, I would like to do things ...
BAF's user avatar
  • 709
0 votes
1 answer
1k views

What is the preferred way to get entity_type_id in Magento 2?

When looking around the M2 source trying to figure out how to get the entity_type_id I see a lot of this : $this->_entityTypeId = $this->_objectManager->create( 'Magento\Eav\Model\...
AreDubya's user avatar
  • 1,344
3 votes
2 answers
3k views

Magento 2: What is Difference between Create & Get ObjectManager?

I have below code. Which give same result either I use create or get $orderId = 1; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->orderPayment = $_objectManager->...
Jackson's user avatar
  • 9,849
2 votes
1 answer
1k views

Magento 2 - Inject arguments into constructors of fabricated objects

When we are using a SDK third party library of any service we can not change it and when this service requires a parameter as argument. Example: /** * @param string $apiKey * @param int|null $...
Rafael Ortega Bueno's user avatar
1 vote
1 answer
602 views

How to check if user exists in Unit Test ? in Magento2 [duplicate]

This my the code, and it's working normally, but in test unit, it is not working ? he said that he doesn't have the create method. $objectManager = \Magento\Framework\App\ObjectManager::...
user3092453's user avatar
1 vote
1 answer
979 views

How to store value in object Magento 2

How do you change price in the frontend without affecting database. I have found the following code for it, but now I want to set my custom price in object. Magento\Framework\Pricing\Amount\Base ...
Ajay Patel's user avatar
  • 1,513
23 votes
2 answers
2k views

What is the proper way for unit testing PHP7 code with PHPUnit 4.1 in Magento 2?

When I'm writing my modules, I'm trying to supply them with unit-tests for the most critical parts of the application. However, there are at the moment (Magento 2.1.3) several ways on how to write ...
Giel Berkers's user avatar
  • 12.2k
1 vote
1 answer
85 views

Object instantiations in compiled mode

I was poking around for academic reasons on how the objects are instantiated. I figured out most of the things and the differences between factories (dev, compiled, production) but something scared me....
Marius's user avatar
  • 198k
0 votes
1 answer
1k views

Magento2 getting - Error filtering template error after add custom attribute value programmatically

Getting this error when reload page after adding value programmatically in a custom attribute. And when i delete that value which i added programmatically then its working fine. Error filtering ...
user3443146's user avatar
3 votes
1 answer
2k views

how pass parameter to object manager get

I have a ProductFactory , it's construction must manual pass a parameter, But I don't know how to pass it. I use $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objectManager-...
user1989041's user avatar
1 vote
1 answer
140 views

Magento 2: How to avoid objectManager in Model?

I have achieved Magento 2: How to change Weight field for DHL Shipping Method [SOLVED] I tried to put protected $_product; public function __construct(\Magento\Catalog\Model\Product $product) { ...
Jackson's user avatar
  • 9,849
3 votes
6 answers
4k views

How to get currency Symbol in magento 2?

I want currency Symbol using currency code in magento 2. so any one help me? I have tried below code but I got error Class Magento\Directory\Model\CurrencyFact‌​ory does not exist $_objectManager =...
Susanta Kumar Das's user avatar
2 votes
1 answer
2k views

Dynamically instanciate class with arguments

I am actually trying to instanciate a class dynamically with these arguments. Here is the code I use in class A: /** @var string $source */ $source = '\Foo\Bar\My\Class'; // Dynamic value that I ...
Matthéo Geoffray's user avatar
3 votes
1 answer
68 views

How do I know when I should use ObjectManager's create() or get()?

I understand that I can create an object using the ObjectManager through either its get() or create() method. From the docs, create() will always create a new instance whereas get() retrieves a ...
xenon's user avatar
  • 875
6 votes
3 answers
2k views

How to debug Uncaught TypeError: Argument must be class A, class B given

First off, very importantly: This error ONLY happens in production mode. In developer mode it seems to be fine. So those of you who will tell me to clear var/generation: That would break production ...
peedee's user avatar
  • 713
-3 votes
1 answer
553 views

Is it recommended to use object manager by passing into constructor as a dependency? [duplicate]

As of my observation, core team used the object manager as added Example flow taken from module-cms public function __construct( //..... \Magento\Framework\...
Krishna ijjada's user avatar
1 vote
0 answers
36 views

Proxy object - when should I use them in fact

I have yesterday read great tutorial about Proxy objects: http://alanstorm.com/magento_2_object_manager_proxy_objects/ I understand that I should use them to lazy load object which have can be slowly ...
Bartosz Kubicki's user avatar
0 votes
1 answer
8k views

Magento 2 : How to use objectManager class object? [closed]

I am little bit confuse, how to use object manager. I have checked this answer but not getting satisfied answer. Always we are recommending that never use objectManager like \Magento\Framework\App\...
Ashish Jagnani's user avatar
3 votes
1 answer
99 views

How to learn about Magento objects

I recently started developing on magento2 and I'm finding a habitual problem always. When I start creating new extension or something, it's normal I will need M2 objects on my controller's ...
Marc Pont's user avatar
  • 1,453
1 vote
1 answer
689 views

Can you help me better understand the object manager to avoid errors when using it?

I'm working on learning Magento 2, and I'm having issues with the automatic constructor dependency injection in my resource model collection. Here is my code: namespace TylerSchade\Contacts\Model\...
tjons's user avatar
  • 1,831
6 votes
1 answer
1k views

How can we identify whether the class is injectable or not in Magento 2

How can we identify whether the class is injectable or not in Magento 2 as per the comment. In this answer get is used by object manager, can any one tell me how to convert below class into ...
Krishna ijjada's user avatar
0 votes
1 answer
691 views

How Magento\CatalogInventory\Model\Stock\Item can be replaced in Constructor (DI)

I want to use below code in Magento 2 constructor. $stockItem = $objectManager->create('Magento\CatalogInventory\Model\Stock\Item'); Can any one tell me the right way of calling above create ...
Krishna ijjada's user avatar
33 votes
6 answers
71k views

Fatal error: Cannot instantiate interface in Magento 2?

As the part of creating bundle product programmatically in Magento 2. I am getting below error. Even i removed var/cache and generation. This may be a duplicate question but none of the answer is ...
Krishna ijjada's user avatar
9 votes
2 answers
2k views

Magento 2 : When to use Object Manager in Unit tests?

So I recently stumbled upon a problem while writing a unit test in Magento 2. You can read more about the problem in question here. Vinai told me it is generally bad practice to use the object ...
Giel Berkers's user avatar
  • 12.2k
3 votes
1 answer
1k views

Is it possible to get an object if exist by object manager in magento 2?

I wanna get some object in magento to like $this->objectmanager->create(`'path\to\some\class\in\another\module'`) but i wanna get 'path\to\some\class\in\another\module' object just if the ...
ali gh's user avatar
  • 731
1 vote
1 answer
273 views

Magento 2: Guide for All Collections

For now, I'm not able to find any guide for this. If I need to get all collection say for Example all Orders List so I will go to Vendor Magento Folder, Particular Module's di.xml file & checking ...
Jackson's user avatar
  • 9,849
1 vote
2 answers
3k views

How to create Tax rule programmatically in controller magento 2

I have try with this code make tax rule programmatically. but it's not working infact model not loaded. you can see this code : $objectManagers = \Magento\TestFramework\Helper\Bootstrap::...
Kartik Asodariya's user avatar
0 votes
1 answer
1k views

M2.1: Loading Products with Magento\Catalog\Block\Product\View

I want to load another Product on the catalog product view page using the the default Magento\Catalog\Block\Product\View Block and trying to load the product via SKU in an custom template file <...
juhanix's user avatar
  • 671
1 vote
1 answer
5k views

how to get customer session id in html page magento 2

I am using one custom payment module and i want to do code in html file. Path is app/code/Custom/Payment/view/frontend/web/template/payment/custompayment.html and my code is $om = \Magento\...
Kartik Asodariya's user avatar
12 votes
3 answers
25k views

Magento2: Load Model Data By Another Field

I have below table named "messages". id | posts_id | message_description | created_at ----------------------------------------------------------------------------- 1 1 test1 ...
Jackson's user avatar
  • 9,849
4 votes
2 answers
416 views

Difference when to declare an object via Magento\Framework\ObjectManagerInterface and inject via the constructor

I'm initializing an object \Magento\Framework\Registry via a function create() of Magento\Framework\ObjectManagerInterface in Controller. After, I try to register a value to Registry Class. $registry ...
LinoPham's user avatar
  • 3,748
26 votes
7 answers
58k views

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

How to Get Media URL in Template file ? All the solution i found is calling Object Manager directly. My another concern, Can you ever call object manager directly as best practice ? (because in most ...
Vishwas Bhatnagar's user avatar
1 vote
2 answers
3k views

How to create an object with constructor parameter in controller file

I am trying to create an object of third-party library class in controller file, I am facing issue while passing argument, below is a code I have written to create an object. Sample_controller.php ...
Aniket Shinde's user avatar
3 votes
0 answers
1k views

Working with Models and Databases in Magento 2

So I've been following along with these questions : https://stackoverflow.com/questions/31983546/in-magento-2-what-is-the-correct-way-for-getmodel/31984198 https://stackoverflow.com/questions/...
Sebastian-Laurenţiu Plesciuc's user avatar
1 vote
1 answer
2k views

How to load model without $objectManager and without inject dependencies?

I am reading that I should never use $objectManager directly to load model $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $subscriber = $objectManager->get('Magento\...
JohnyFree's user avatar
  • 1,529
0 votes
1 answer
783 views

Magento 2 : Can not add method/class in __construct()

I have a collection.php file. I am adding some function into that which calls other models.As per Magento 2 first we have to add object managerger interface. And then we can call other model.like ...
Krupali's user avatar
  • 1,140
1 vote
1 answer
124 views

How to program with the Object manager through PHPStorm Magento 2 [closed]

I'm fairly new to the programming part of Magento 2, I've been using rest calls with PHP, but I want to change method as I'm opening up a second shop. Now I would like to know if it is possible to ...
TanguyB's user avatar
  • 213
8 votes
2 answers
7k views

How to get ScopeConfigInterface through the object manager of unit tests in magento 2?

I'm trying to read a row in my unit test from core_config_table in magento 2 database. I know that to accomplish this job as I have read this link. I have to use: \Magento\Framework\App\Config\...
ali gh's user avatar
  • 731
160 votes
10 answers
183k views

To use or not to use the ObjectManager directly?

Ok, so yesterday we had a big talk with other people from the Magento community regarding the direct use of the ObjectManager in classes/templates. I'm already aware of the reasons why we shouldn't ...
Raphael at Digital Pianism's user avatar
3 votes
1 answer
2k views

product image url not valid - magento 2.0.1

I have created a widget. I am trying to show product image in widget. I have successfully got product name, product url and image url using object manager. But the Image is not loaded (404 - not found)...
Bilal Usean's user avatar
  • 9,917