Get Categories and Its Sub Categories in Magento

Today I will show you how you can fetch magento categories and its subcategories. Use below code

 

<?php 
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
if (count($_categories) > 0){
    foreach($_categories as $_category){
        $_category = Mage::getModel('catalog/category')->load($_category->getId());
        $_subcategories = $_category->getChildrenCategories();
        if (count($_subcategories) > 0){
            echo $_category->getName();
            echo $_category->getId();  
            echo $_category->getUrl();    
            foreach($_subcategories as $_subcategory){
                 echo $_subcategory->getName();
                 echo $_subcategory->getId();
                 echo $_subcategory->getUrl();
            }
        }
    }
}
?>

To show only one category where 2 is your main category id

<?php 
$category = Mage::getModel('catalog/category')->load(2);
$subcategories = $category->getChildrenCategories();
if (count($subcategories) > 0){
    echo $category->getName();
    foreach($subcategories as $subcategory){
         echo $subcategory->getName();
    }
}
?>

 

How to Remove SID query from Magento URLs

We will apply the site redirects technique for this issue . While working on the SEO someone can place a wrong link to some forum or blog. We don’t want those links to lead to improper URL and we don’t want SIDs to appear to those visitors. Most important: We don’t want that search engines index the URLs with SIDs.

The solution is simple.

For magento1 you will have to go to root directory .htacess file and add the below code and in Magento2 you should write the code in the pub directory .htaccess file.

RewriteEngine on
If you want to have www part:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
If you don’t want to have www part:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

How to get Magento low stock report

In magento admin panel Go to
System->Configuration->Inventoty->Product Stock Options

and set Notify for Quantity Below to the required value. Now when you generate low stock report all the products having the quantity below Notify for Quantity Below value wil be dispalyed.

How to remove header links in Magento 2

Please follow the below instructions :

Step 1 :- Go to app/design/frontend/VenderName/ThemeName/Magento_Theme/layout/default.xml

Step 2 :- Copy below Code

<referenceBlock name="wish-list-link" remove="true"/>
<referenceBlock name="catalog.compare.link" remove="true"/>

Step 3 :- Paste this code in default.xml before body tag () and save it.

It will remove the links from the header

CSS Media Queries for iPads & iPhones

Sometimes we got very confusion while developing responsive work. When we apply the breakpoints for iPad and iPhone that does not works properly. As per my experience while searching on the Internet I found some useful information regarding the media queries. I applied those and that works fine for me. So today I am going to add the media queries here for the same.

iPad Media Queries

iPad Media Queries (All generations – including iPad mini)

 

iPad in portrait & landscape


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) { /* STYLES GO HERE */}

iPad in landscape


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) { /* STYLES GO HERE */}

iPad in portrait


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) { /* STYLES GO HERE */ }

Retina iPad in landscape


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio: 2) { /* STYLES GO HERE */}

Retina iPad in portrait


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio: 2) { /* STYLES GO HERE */ }

iPad mini Media Queries

iPad mini in portrait & landscape


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (-webkit-min-device-pixel-ratio: 1) { /* STYLES GO HERE */}

iPad mini in landscape


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio: 1) { /* STYLES GO HERE */}

iPad mini in portrait


@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio: 1) { /* STYLES GO HERE */ }

iPad mini Resolution


Screen Width = 768px (CSS Pixels)
Screen Height = 1024px (CSS Pixels)

Screen Width = 768px (Actual Pixels)
Screen Height = 1024px (Actual Pixels)

Device-pixel-ratio: 1

iPhone Media Queries

iPhone 6 Media Queries

iPhone 6 in portrait & landscape


@media only screen
and (min-device-width : 375px)
and (max-device-width : 667px) { /* STYLES GO HERE */}

iPhone 6 in landscape


@media only screen
and (min-device-width : 375px)
and (max-device-width : 667px)
and (orientation : landscape) { /* STYLES GO HERE */}

iPhone 6 in portrait


@media only screen
and (min-device-width : 375px)
and (max-device-width : 667px)
and (orientation : portrait) { /* STYLES GO HERE */ }

How to Export Magento Product Database

In this tutorial I will show you how you can export entire Magento product database.
Most people can find their way to the standard Export options under System -> Import/Export, but that often doesn’t produce the results they’re expecting. A better approach is to use the Dataflow – Profiles that Magento provides. These out-of-the-box export “profiles” take care of a lot of the configurations you will need to make if you use the standard Magento Export option. Depending on the data you wish to export there are options for customers, products and stock.
In this post, we are going to look specifically at the Export All Products option.

We are going to use Magento Data-Profiles to Export your entire product database:

1-Navigate to System -> Import/Export -> Data-Profiles

2- Select Export All Products:

3- Review your options in the Profile Wizard. You may wish to change the name of your export file under File Information -> File Name.

4. To define which product attributes you wish to export for each product, under Profile Wizard -> Data Format -> Export you can choose to export All Fields, or select specific product attributes you wish to export by selecting Only Mapped Fields and selecting your required attributes.

5. To define a specific range of products you wish to export under Profile Wizard -> Export Filters -> you can filter your products by name, sku, product type, attribute set, price, stock quantity, visibility or status. Save and Continue Edit, and we’re ready to export.

6. Now that the parameters for your export are set, choose the Run Profile tab and click the Run Profile in Popup button. Your export will start automatically in a pop up window.

7. Once your export is complete, you now need to retrieve it from your server via SFTP. If you are unsure of how to connect to your server, please contact your hosting provider or web support. We recommend Filezilla as a good FTP program you could use.

8. Once logged into ftp, you will find you exported csv file waiting for you inside the var/export folder. Download the file, and there you have it!

How to put Magento in Maintenance Mode only for Front end

In this tutorial we will show you how to put your Magento online store in maintenance mode only for front end site. The admin can access the site without any issue

To put a Magento site in maintenance mode, you need to create an empty maintenance.flag file and upload it to the root folder of your site.

When you will upload maintenance.flag file then your website will be down and will show the error message

Service temporarily unavailable.

So for this message will show for all types of users. Now we will come to point and restrict the above message only for front users. For this follow the below instructions :

You need to replace the below code in index.php file (keep in mind when you will upgrade the site site then you must revert this process).

if (file_exists($maintenanceFile)) {
include_once dirname(__FILE__) . ‘/errors/503.php’;
exit;
}

To this

if (file_exists($maintenanceFile) && strpos($_SERVER[‘REQUEST_URI’], ‘/admin/’) === false) {
include_once dirname(__FILE__) . ‘/errors/503.php’;
exit;
}

Please note the the code above is quick and dirty hack and I just check if /admin/ exists in url. If you have another url then you should replace with that.

Static blocks not working – Magento 1.9.x

After updating Magento to version 1.9.x, or installing security patch SUPEE-6788 on an older version, lots of people are noticing that their static block shortcodes are no longer working. So may be some developers frustrated or confused why this happen. The solution is simple magento people added security feature on the newer version.
Simply navigate to

System => Permissions => Blocks

And add the block type you wish to display.

Magento – Show Inventory Levels on Product Pages

Today I will show you how we can add Quantity on hand information on the product detail page.Normal Magento shows ‘Availability-In Stock’ by default. We need to change that Qty on hand like the below image

qtyonhand

For this you need to do the below steps :
Go to app/design/frontend/base/default/template/catalog/product/view/type/default.phtml.
Get a copy of that file and move it to your custom theme, making sure to add folders as needed to maintain the correct path. Here is the code snippet for adding Quantity on Hand to your product pages.

Replace below code

<?php if ($_product--->isAvailable()): ?>

with

<?php if ($_product--->isAvailable()): ?>
<?php $quantity1 = intval(Mage::getModel('cataloginventory/stock_item')--->loadByProduct($_product)->getQty()); ?>

<div class=”add-to-box”>

<?php if($quantity1 > 0) { ?>

<p class=”availability in-stock”>
<?php echo $this->__(‘Qty on Hand:’) ?>
<?php echo $quantity1;?>
</span>
</p>

<?php } elseif($quantity1 == 0) { ?>
<p class=”availability in-stock”><?php echo $this—>__(‘Qty on Hand:’) ?></p>
<?php foreach ($_product ->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) { $stock = intval(Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($simple)->getQty()); echo $simple->getName().” “.$simple->getSize().” have a stock of $stock”; echo ‘<br>’; } ?>
</p>
<?php } ?>
</div>

How to change the Magento Admin URL

Here is a quick guide on how to change admin url path in Magento. To protect your Magento backend against hackers and brute-force attacks, we recommend that you change the default URL to the Magento Admin Panel is must. It is a quick and easy way to add an extra layer of security to your site.

Generally, Magento have ‘admin’ as the administrator path. So, the admin URL will be http://www.example.com/admin/

Here is process how you can do this :

1) Changing local.xml
– Open app/etc/local.xml
– Find the following:-

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

– Go to this line
<frontName><![CDATA[admin]]></frontName>

and change “admin” as you desired the admin path like

<frontName><![CDATA[siteadmin]]></frontName>

and then upload file. After file update clear magento cache.

The process is now complete and you can login your magento with new url path

http://www.example.com/siteadmin/