Linked Questions

11 votes
1 answer
3k views

why i should not use object manager directly in magento2? [duplicate]

Magento 2 uses dependency injection means when ever client required any services we have to pass in constructor. So magento take care like automatically create objects recursively.. but there is a ...
Sivakumar K's user avatar
  • 2,013
0 votes
1 answer
4k views

Magento 2.2 Can we use Object Manager in Controller or Block? [duplicate]

To get product data, I am using object manager in my controller, but I read in docs that we should not use it directly. <?php pubic function execute() { $objectManager = \Magento\Framework\...
temper's user avatar
  • 994
0 votes
2 answers
1k views

Magento2 performance load : Constructor initialize or use Object manager Directly? [duplicate]

There seems to be multiple different ways to call the helper or model classes. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objectManager->create('Magento\Framework\...
jpk's user avatar
  • 21
-1 votes
2 answers
208 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
1 answer
555 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
2 votes
1 answer
169 views

ObjectManager in category init [duplicate]

Why Magento is using $this->_objectManager->create() directly in function _initCategory in file vendor/magento/module-catalog/Controller/Adminhtml/Category.php? protected function _initCategory(...
Anshu Mishra's user avatar
  • 8,960
80 votes
22 answers
137k views

How to check if customer is logged in or not?

How to find if a customer is logged in or not in Magento 2. If the customer is logged in then how to get customer data from a session?
Rakesh Jesadiya's user avatar
19 votes
6 answers
17k views

How to get store phone number in magento 2

I want to display phone number saved in magento admin in frontend in magento 2. Like in magento 1.9 its like $storePhone = Mage::getStoreConfig('general/store_information/phone');
Paras Arora's user avatar
13 votes
3 answers
29k views

Magento 2 - What is the use of formkey

I see this line of code inside the login form. <?php echo $block->getBlockHtml('formkey'); ?> What is the use of it? Is it more secure? Is it a must for form post?
TFS's user avatar
  • 4,319
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,909
11 votes
4 answers
23k views

Magento 2 Get Cart Quote Total in minicart.phtml

I'm trying to get the cart quote total in minicart.phtml, but I'm having no luck. I'm injecting Magento\Checkout\Model\Cart. Here's my code: $this->cart = $cart; $cartQuote= $this->cart->...
Paul's user avatar
  • 1,501
5 votes
3 answers
19k views

how to get main product image url in phtml file?

I want to get image url of product showing ,on product description page in phtml file. any help ?
Learner's user avatar
  • 833
12 votes
4 answers
8k views

Magento 2 Helper instance

When I think I got my head wrapped around the DI system from Magento 2 something comes up and un-wraps it. I see in the core code different ways to access a helper. For example in Magento\Catalog\...
Marius's user avatar
  • 198k
7 votes
4 answers
20k views

Magento 2: how to get Model in Block

I want to use an instance of a Model into a Block. in Magento 1 they uses: $exple = Mage::getModel('exple/standard'); How can I do this with Magento 2?
Sabri Tahri's user avatar
8 votes
3 answers
11k views

Magento 2: How to redirect customer to login page

How can I check customer is logged in or not? If customer is not logged in then how to redirect him to the login page? I want to do this from .phtml file. So please help me as per that.
Krupali's user avatar
  • 1,140
9 votes
5 answers
16k views

How to delete a product in Magento-2 programmatically?

I am trying to use below code but it is not working, $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('\Magento\Catalog\Model\Product'); $...
Ranjeet Singh's user avatar
10 votes
2 answers
13k views

Magento 2: how to delete image or file

how to delete file or image in magento 2. I know using unlink('full file path'); will delete the file but I want to do magento 2 way. condition when User checked the delete checkbox.
Qaisar Satti's user avatar
  • 32.5k
5 votes
5 answers
18k views

Get customer name from customer id using object manager

I try to use object manager to retrieve customer first name and last name using customer id like this: $customer = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface')->...
Idham Choudry's user avatar
7 votes
5 answers
25k views

How to add tracking url in shipment email for customer in Magento 2?

When I ship any order with a UPS label and add the tracking number. it shows Track Order in the backend. I want to send this URL to the customer's email also. Currently, it sends the tracking number ...
Abhishek's user avatar
  • 939
5 votes
2 answers
19k views

Magento 2 phtml get store view being viewed

In Magento 2 I have a phtml file I am trying to get what store view is currently being seen by the website visitor. However it does not seem to work. I dont want to have to create a whole module or ...
Jayreis's user avatar
  • 705
5 votes
3 answers
26k views

Getting all available category, subcategory list in magento 2

I want the replacement of the following code or similar to this code to get all category and subcategory in Magento 2. $categories = Mage::getModel('catalog/category') ->getCollection() -&...
Manish's user avatar
  • 3,106
4 votes
3 answers
14k views

Uncaught Error: Call to a member function getData() on null

we are using Magento 2.3.1 and Porto theme when I click on product its showing below mentioned error - https://offineeds.co.in/4-in-1-touch-led-lamp.html Fatal error: Uncaught Error: Call to a ...
Divya's user avatar
  • 41
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
5 votes
2 answers
12k views

Update Customer Data in Magento2 with custom attribute

I want to update the firstname of a customer like this : $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerFactory = $objectManager->create('\Magento\Customer\Model\...
Me7z's user avatar
  • 109
7 votes
2 answers
10k views

Update custom column data in quote table Magento 2

I inserted a custom column in quote table like this: if (version_compare($context->getVersion(), '1.0.1', '<')) { $setup->getConnection()->addColumn( $setup->...
Hunter's user avatar
  • 1,484
5 votes
4 answers
5k views

Magento 2 : Alternate Logo for Page Layout

I'm developing Magento 2.1 application with two logos: Light logo on the home page. Dark logo for all other pages. By default, theme uses dark logo. I've created a custom page layout for home page. ...
MParsons's user avatar
  • 105
2 votes
2 answers
15k views

Get product collection based on custom attribute (drop down attribute value)

I created a custom drop down attribute in Magento2 from admin. I tried to collect all associated products by using filters. I am able to filter product collection based on text field attributes, for ...
Suresh C's user avatar
3 votes
5 answers
3k views

Magento 2 : Displaying the database table values in frontend

In Magento 1.9 i used this code to get the database table values. $model = Mage::getModel('task_creation/details'); $collections = $model->getCollection(); foreach($collections as $collection)...
Selva Kumar's user avatar
3 votes
3 answers
11k views

Class Mage not found in Magento 2

I'm new to Magento 2. Fatal error: Class 'Mage' not found in \vendor\magento\module-theme\view\base\templates\root.phtml on line 23 Can anybody tell me how to solve this?
Jackson's user avatar
  • 9,909
1 vote
3 answers
10k views

Magento 2 : How to write logs from a phtml file in a theme

How to write logs in var/log/whatever_file from a phtml file which belongs to my custom theme?
DevonDahon's user avatar
1 vote
2 answers
6k views

Magento 2 : How to add login link on home page

I want to add the login page geturl link when I click on the login link. What is the role of customer_account_login.xml in this and how to load the login page after clicking on the link?
Devendra'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
10 votes
2 answers
3k views

Magento 2 Doesn't Support Dependency Injection in Traits?

Do traits actually work with dependency injection in Magento? Consider the following code: Trait Class namespace Frame\Slick\Block; use Frame\Slider\Slick\Block\Data as Helper trait Slick { ...
André Ferraz's user avatar
0 votes
3 answers
10k views

How to get product by id and products filtered by attribute in magento 2

I have the following code in my Block/Main.php: <?php namespace Foo\TireSearch\Block; class Main extends \Magento\Framework\View\Element\Template { protected $productCollectionFactory; ...
b_pcakes's user avatar
  • 493
1 vote
3 answers
6k views

magento 2 get child product image thumbnail

How to get child product image thumbnail in sales order view in Back-end ? my Magento is 2.1.6 This code working but i need to get order current image not configurable image all the time if i ...
MagEGY's user avatar
  • 348
3 votes
3 answers
4k views

How to get current product in Magento 2.3.1 without deprecations

Not a duplicate question, as the referenced original question uses deprecated classes. I want to get the current product in a Block / Model in Magento 2.3.1. I cannot directly use / inject / extend ...
Adarsh M's user avatar
  • 584
2 votes
3 answers
3k views

what is the best way to create a new instance for class?

What is the difference or best way for creation instance of new class? Object manager? or Define in to constructor?
Bhavik's user avatar
  • 1,240
3 votes
3 answers
5k views

Magento 2: Get all category products by category name

anyone knows how to load from my custom block.php all the products of a specific category, I would like to get them by the name of the category and not by the id. I tried to change the standard ...
3tech - Alessandro Staffolani's user avatar
1 vote
2 answers
2k views

Magento 2.1.1 - Display order subtotal in header

I have added the following to minicart.phtml for my theme and it doesn't work. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\...
user5762246's user avatar
2 votes
2 answers
3k views

How to Hide Header(Primary) Menu before login (for Visitors)?

Magento 2 Version - Magento 2.1.4 CE I am trying to create Custom Module for Compulsory login. When user hits the website url it must needs to redirect on login screen and after logged in user can ...
Manthan Dave's user avatar
  • 9,816
0 votes
4 answers
4k views

How to get only configurable products as a bestseller in Magento 2?

My site contain only configurable products and I want to display bestseller products. I know there are many extension available for bestseller products but i want to display only configurable products....
Sanjay Gohil's user avatar
  • 2,200
0 votes
2 answers
2k views

Get latest product programmatically in magento 2

How to get Latest products in Magento 2 programmatically in phtml file ?
Arun Karnawat's user avatar
3 votes
3 answers
2k views

Magento 2 set locale in CLI command module

I'm developing a command line module in magento 2 and I cannot find a way to set locale programmatically. The module takes care of creating a pdf invoice from an order and sends it via email to ...
floyd's user avatar
  • 51
1 vote
2 answers
3k views

Magento 2 Restrict access to CMS page

I am looking a way to restrict access to specific CMS page, allowing only logged in customers with a special customer group to see it. EDIT: I have this function, but observer isn't working properly ...
kilis's user avatar
  • 489
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
2 answers
3k views

Magento2 Split Order Based on Vendor

I am trying to split the order based on vendor during checkout process. If there are two products in cart from different vendor, order needs to be split based on vendor. I have followed the answer ...
blakcaps's user avatar
  • 1,965
2 votes
3 answers
2k views

Magento 2: How to get welcome text in template

How I can get fetch the welcome text of Configuration in Template file. For e.g. welcome text : "DEFAULT WELCOME MSG!" I know I can get it in Magento 1.9 with: <?php echo Mage::getStoreConfig('...
Omer's user avatar
  • 151
2 votes
3 answers
1k views

Product collection has no name attribute how can I get it magento2?

I tried this code but :- $product = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addAttributeToSelect('*') ->...
Manish Goswami's user avatar
3 votes
1 answer
848 views

Is it okay to use $this->helper(...) in phtml files?

A question on using $this->helper(...) in phtml files: It returns something like this: ObjectManager->get(...) (Singleton) I'm aware it's bad practice to use Object Manager phtml, but ...
Chris Rogers's user avatar
1 vote
2 answers
1k views

Magento2 Admin Grid Image Display

Note: I am not using UI component I have a file Grid.php where I have a column image, in this column, image URL is populating I want to show the image in this column instead of image URL
Muhammad Anas's user avatar

15 30 50 per page