Load product by SKU in Magento 1.x

How to load Magento product by SKU or by product id to get product name, Image, and other useful information.

Load product by SKU
$product = Mage::getModel('catalog/product')->loadByAttribute('sku','product_sku');

Load product by product id
$product = Mage::getModel('catalog/product')->load($product_id);

Warning: simplexml_load_string() in Magento

Warning: simplexml_load_string() in Magento

This error comes when there is any unwanted white space in any file or in the URL path.

To fix this error you need to connect to the database and open table core_config_data.

find web/unsecure/base_url and web/secure/base_url URL in this table.

check domain URL contains white space or not if yes then remove and save it.

now clear cache from var folder and run URL.

Direct SQL Queries In Magento

Let’s assume we need to fetch data from direct database and we don’t want to use model in magento. At that time we can also access data direct from database using magento database connection and simple SQL query.

Below is the code for how can we access data using simple SQL query.

$new_db_resource = Mage::getSingleton('core/resource');
$connection = $new_db_resource->getConnection('magento');
$data='SELECT design_id FROM sales_flat_quote_item WHERE item_id = '.$_item->getId();
$outout=$connection->fetchRow($data);
print_r($outout);

Get product image in Magento

Magento basically has 3 different types of product image which are as followed.
Base Image
Small Image
Thumbnail image

Now let’s see how we can get product images using product id.

$products = Mage::getModel('catalog/product')->load(900);
echo $products->getImage();
echo $products->getSmallImage();
echo $products->getThumbnail();
echo Mage::helper('catalog/image')->init($products, 'small_image')->resize(163,100);
echo Mage::helper('catalog/image')->init($products, 'image')->resize(400,400);

Get product collection filter by category id in magento

Below is the code by using we can get product collection filter by category id in Magento

$category = Mage::getModel('catalog/category')->load('category_id');
$productCollection = $category->getProductCollection();
$productCollection
->addStoreFilter()
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();

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.

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
}