Get subcategories of a main category in magento

To insincere sales it is important that your store must be user friendly where user easily find his/ her needed product and easily navigate in store.

one of important page in magento is to display subcategory of main category. i.e let\’s assume we have store with category Women and we are selling all products which was related to women so it is preferable if we bifurcate all products so customer can easily find product.

Below is the code for display all sub-category of parent category.

$layerobject = Mage::getSingleton('catalog/layer');
$_category = $layerobject->getCurrentCategory();
$currentCategoryId= $_category->getId();
$childrencategory = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
foreach ($childrencategory as $category)
{
echo $category->getName();
echo $category->getRequestPath();
}

Filter product collection with multiple SKU magento

Suppose, you need product information of more than one product using SKU at that time we can get all information using “array in” with filter.

Below is code for the same. you just need to change SKU name in below code.

$productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter( 'sku', array( 'in' => array('sku1', 'sku2', 'sku3' )));
$product = $productCollection->getFirstItem();

By using the above code we can get a specific product from product collection using SKU.

Class Authorization GroupFactory Model does not exist magento2

Getting error Class Magento\Authorization\Model\Acl\Role\GroupFactory does not exist

Magento 2 is quite complex to understand. We frequently get this error while developing the store. Here is the solution to fix this error.

1) go to the var folder and find generation. now remove the folder.

2) Now run below code using the command line

php bin/magento setup:di:compile

It will automatically create all folder in the generation folder.

3) Then clear cache using below code:

php bin/magento cache:flush

Now, Run your store it will run without error.

Update database password in magento

Changing database password frequently is always good practice to make your store secure and it will protract your store with malware and other hacking attacks.

Below are the steps on how to change the database password in Magento.

1) Connect to your server using cPanel.
2) update the password of the database user and copy it. As we have to update that new password in the local.xml file.
3) Now, go to app/etc and open local.xml using any editor.
4) find below code in the local.xml file


<username><![CDATA[your_database_username]]></username>
<password><![CDATA[password]]></password>
<dbname><![CDATA[database_name]]></dbname>

5) Replace your new password in password tag and save the file.

6) Now, Refresh cache and run your domain URL.

How to change admin url in magento

By default, we can access Magento admin by simple using /admin after Magento store URL. To secure your store it’s preferable that you change the default URL of Magento store.

Below are the steps to change the admin URL in Magento.

1) Connect to your server using SSH or cPanel or FTP
2) go to app/etc and open local.xml using any editor.
3) find below code in local.xml file

<admin>
<routers>
<adminhtml>
<args>
<frontName><![CDATA[admin]]></frontName>
</args>
</adminhtml>
</routers>
</admin>

4) Replace your new URL name with admin in a frontName tag.

Note: Please note that there are no special characters in a new name, then save the file.

5) Now, Refresh cache and run your domain URL.

Database Connection in php

By using below code you can connect the database with PHP application. After database connection we can access all database and also we can CRUD operation by using PHP application.


$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

Add top links in magento

Magento is an ecommerce platform built on open source technology and which use MVC system. It is managing all request response using Model, View and Controller. So, all view file call base on controller mapping as per there configuration in layout/*.xml file.

Layout file is use to define phtml file on specific module request. Let’s say if we want to add top link in magento at that time we need to use below code.

Add custom link to top link in magento


<block type="page/template_links" name="top.links" as="topLinks">
<action method="addLink" translate="label title">
<label>Blog</label>
<url>/blog</url>
<title>Blog</title>
<prepare></prepare>
<urlparams></urlparams>
<position>1</position>
</action>
</block>

If you want to open same URL in new tab at that time you can use below code.


<block type="page/template_links" name="top.links" as="topLinks">
<action method="addLink" translate="label title">
<label>Blog</label>
<url>/blog</url>
<title>Blog</title>
<prepare></prepare>
<urlparams></urlparams>
<position>1</position>
<liparams></liparams>
<aparams><!--[CDATA[target="_blank"]]--></aparams>
</action>
</block>

Magento get login customer information

Suppose we want to get all information of login User like Full Name, First Name, Middle Name, Last Name etc. We can also check user registration date, Date of Birth and many more information. Below is code to display information of customer.

// Check if any customer is logged in or not
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
// Load the customer's data
$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->getPrefix();
$customer->getName(); // Full Name
$customer->getFirstname(); // First Name
$customer->getMiddlename(); // Middle Name
$customer->getLastname(); // Last Name
$customer->getSuffix();
// All other customer data
$customer->getWebsiteId(); // ID
$customer->getEntityId(); // ID
$customer->getEntityTypeId(); // ID
$customer->getAttributeSetId(); // ID
$customer->getEmail();
$customer->getGroupId(); // ID
$customer->getStoreId(); // ID
$customer->getCreatedAt(); // yyyy-mm-ddThh:mm:ss+01:00
$customer->getUpdatedAt(); // yyyy-mm-dd hh:mm:ss
$customer->getIsActive(); // 1
$customer->getDisableAutoGroupChange();
$customer->getTaxvat();
$customer->getPasswordHash();
$customer->getCreatedIn(); // Admin
$customer->getGender(); // ID
$customer->getDefaultBilling(); // ID
$customer->getDefaultShipping(); // ID
$customer->getDob(); // yyyy-mm-dd hh:mm:ss
$customer->getTaxClassId(); // ID
}

Get customer detail using email address in magento

Suppose we have the customer email id and we want to fetch all details of that customer in Magento, its very easy and you can use below code it will give you all information with the email address which you have added in $email.

$customerExist = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('email', $email )
->getFirstItem();

Convert .SVG image

Below is script to convert svg image from img tag to SVG tag. After converting image in svg tag you can able to fill color in that image.

jQuery('img.svg').each(function(){
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
jQuery.get(imgURL, function(data) {
var $svg = jQuery(data).find('svg');
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
$svg = $svg.removeAttr('xmlns:a');
$img.replaceWith($svg);
]}, 'xml');
});