Questions tagged [object-manager]
Denotes question regarding Magento 2 Object Manager
161
questions
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 ...
-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?
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 ...
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
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
)
{....}...
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\...
-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
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 ...
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::...
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 ...
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 ...
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\...
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->...
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 $...
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::...
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 ...
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 ...
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....
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 ...
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-...
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) {
...
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\CurrencyFactory does not exist
$_objectManager =...
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 ...
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 ...
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 ...
-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\...
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 ...
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\...
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 ...
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\...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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::...
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
<...
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\...
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 ...
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 ...
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 ...
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
...
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/...
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\...
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 ...
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 ...
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\...
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 ...
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)...