As we know, there are so many situation where we require to check whether customer is currently logged in or not. In this blog post you will learn about how to check customer is logged in or not in Magento 2. In order to check this situation you can use below code snippet.
/** @var \Magento\Framework\App\ObjectManager $om */
$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\App\Http\Context $context */
$context = $om->get('Magento\Framework\App\Http\Context');
/** @var bool $isLoggedIn */
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
Once you got the result in $isLoggedIn variable, you can check condition as per below.
// If customer is logged in
// Some code snippet here
You can use above code snippet. That’s it. Enjoy!!
PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
Developers are always looking for ways to improve their skills, and taking advantage of PHP’s versatility is a great way to create more robust and efficient code. There are many things that you can do to make your next project more readable and maintainable. Below are tips to help you in PHP development.
1. Use Comments
It can be frustrating when you look back at old code and cannot remember what it was for. Using comments effectively will give your code a clearly defined purpose and help you to recall things that would otherwise be forgotten. Comments can also help others that read your code to quickly ascertain what it is designed to accomplish.
2. Name Variables Meaningfully
Your variable names should always be meaningful to you. If you are using a variable as an integer try beginning its name with an i. This will make the purpose of the variable obvious at first glance.
3. Use Constants
It is counter-productive to repeatedly re-write the same thing over and over again. Constants can be used to store data that is needed frequently throughout your code. Error messages, directory paths, and other recurring information could be called upon quickly and easily in this way.
4. Replace Functions with Objects
Functions that have lots of parameters can make a project overly and unnecessarily complex. When functions get unwieldy, requiring you to change all of your calls, then it’s time to use an object instead.
5. Don’t Repeat Code
Any code that has to be repeated throughout your project should be contained in an object or function. It may seem like extra work at first, but when your project gets more complex you will save yourself some headaches.
6. Force type checking
$amount = intval( $_GET['amount'] );
$rate = (int) $_GET['rate'];
7.Use Separate Configuration Files
Configuration settings should not be placed in you script file. Always use a separate configuration document that is then included in the beginning of the script. You will avoid having to edit every page due to a small configuration change.
8. Don’t check submit button value to check form submission
if($_POST['submit'] == 'Save')
//Save the things
The above is mostly correct , except when your application is multi-lingual. Then the ‘Save’ can be many different things. How would you compare then. So do not rely on the value of submit button. Instead use this :
if( $_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['submit']) )
//Save the things
9. Initialize Variables
PHP automatically creates a variable when a value is assigned to it. Leaving things up to PHP like this is a bad idea. Always initialize your variables prior to using them.
10. Assume its False
When evaluating a Boolean expression, always begin by assuming it is false. It is always better to prevent something from running until properly validated.
11. Develop With Error Reporting On
The very first thing you do when starting a new project is to turn error reporting to E_ALL, and you should only turn it off ten seconds before going to production mode.
12. Use Indentation and Spacing
You can dramatically improve the readability of your code by utilizing indentation and spacing effectively. This will allow you to quickly identify the ends of loops in your code and will gain you respect as a PHP developer.
13. Avoid using globals
- Use defines/constants
- Get value using a function
- Use Class and access via $this
In Magento 1.x, store owner can directly do reindex from Index management. But in Magento 2.x there is no direct options available for Reindex from backend.
As Magento 2.x provides reindex using command line. So lets discuss about how to do reindex using command line.
If you need to do whole index management in the Magento 2.x store, then you have to run the following command.
First go to magento root directory, then run below command to apply reindex to whole store.
php bin/magento indexer:reindex
But what if you want to reindex specific indexer. for that you will need list of the indexer name. You will get the list of indexer using below command.
In root directory run below command,
php bin/magento indexer:info
Now you have list of all the indexer available, so you can run command for specific indexer.
To run single reindex command,
php bin/magento indexer:reindex indexer_name
here [indexer_name] means name of the indexer which you received from ‘indexer:info’ command.
If you have any trouble to connect with SSH panel Or If you want to do index management from admin then you can download below Free Magento 2 Extension.
Click Here to download this free Magento 2 extension.
Let me know your comments at below and help me to improve this post and help more and more to Magento 2.x developer.
Magento by default provides us a very good template for displaying different different pages. It also provides variety of layout files to display different pages with different different layouts. But for our convenience we need to do some modifications in the default layout of magento.
One of the most common issue for product display is how to show 4 products instead of 3 in a row in magento. Because, magento does not provide any functionality to change it from admin side, we need to make it possible from our side i.e., by coding. We can easily change it by the help of an xml file. So, let’s see how we can change it from coding.
Step:1 Open your template file folder like app/design/frontend/default/YOUR_THEME/layout/catalog.xml.
Be careful that you must not modify the core file. If you don’t have any theme than go to app/design/frontend/default/default/layout/catalog.xml.
step:2 In that file find out the code like this
step:3 add this line below it
You will now able to see 4 products per row. If you want to see more products you can change it from here easily.
Every time you log in to the Magento Admin Panel, you will see an admin magento notification popop message.From the developer point of view, it’s best way to stay informed about new updates. But end client never like these pop up on every login about which they don’t know anything.
In this post I will show you how to disable Admin Notification Popup. This popup functionality is provided by one of the magento module. So if you disable this Magento module then you will not get this popup message next time.
Steps to disable admin notification popup
- Login to Magento Admin Panel. You will get popup.
- Go to System >> Configuration >> Advance
- Disable the Module named “Mage_AdminNotification”.
- Clear Magento cache and try to login again.