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);

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();

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.

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.

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
}