Most Common & Frequently Used PHP functions in Magento CMS

Most Comman & Frequently Used PHP functions in Magento CMS

Magento is a very highly effective and thus a little bit challenging to theme CMS. It is designed on the Zend framework, and it often becomes a challenging task to develop or modify Magento templates. Here in this post, I’ll try to cover a brief list of useful Magento template development functions, which are used regularly during theme development.

1. Magento Check & Get Secure URL

1.1. Check if Secure URL is enabled in Frontend

1.2. Magento Check if Secure URL is enabled in Admin

1.3. Magento Check if current URL is Secure

1.4. Magento Check if current page is Frontend or Admin

1.5. Magento Get Base Url

1.6. Magento Get Shopping Cart URL:

2. Magento How to check if current page is homepage?

2.1. If you are in template/page/html/header.phtml

2.2. In any other .phtml template file or in any other .php class file

2.3. Alternative way

3. Get current URL of the page

3.1. From Mage_Core_Helper_Url class

3.2. From Mage_Core_Model_Store class

3.3. If some missing css, js, images etc

3.4. Base URL

3.4.1. Get base url path

3.4.2. Get home URL

3.4.3. Get MEDIA folder path

3.4.4. Get JS folder path

3.4.5. Get SKIN folder path

3.4.6. Get url of custom module

3.4.7. CMS Page

3.4.8. Custom Page URL

3.4.9. CMS Page with id

4. Customer

4.1. Get Customer Login URL

4.2. Get Customer LogOut URL

4.3. Get Customer My Account URL

4.4. Get User Register URL

4.5. Get Customer Checkout URL

4.6. Check Customer Logged in

4.7. Check whether the customer is logged in the magento system or not.

4.8. Get customer id.

4.9. Get guest id.

4.10. Check whether admin is logged in or not in a magento site.

5. Get Current Directory Paths

5.1. Get Base Directory

5.2. Get Magento app Directory

5.3. Get Media Directory

5.4. Get design directory path

5.5. Get code directory file path

5.6. Get lib directory file path

5.7. Get skin directory file path

5.8. Get var directory file path

5.9. Get cache directory file path

5.10. Get log directory file path

5.11. Get URL Path in Static Block

6. Get currency code & currency rate

6.1. Get Base Currency Code

6.2. Get Default Currency Code

6.3. Get Current Currency Code

6.4. Get Current Currency Rate

6.5. Get currency rates of allowed currencies

6.7. Changing price from any one currency to another

6.8. Convert price from current currency to base currency

6.9. Get currency rates for Nepalese Currency

6.10. Get currency symbol by currency code

7. Modules

7.1. To Check module enabled

7.2. To get list of all modules

7.3. Check whether any particular module is active

8. General Store-Related Functions

8.1. Display products list page (list.phtml).

8.2. Get current store id.

8.3. Get current store name.

8.4. Get current store code.

8.5. Get website name.

8.6. Get session id.

9. Product/Category-Related Functions

9.1. Get list of all Categories

9.2. Get all active categories

9.3. Get the product link.

9.4. Get the product name.

9.5. Get Image url of current category.

9.6. Check whether the current category is Top category.

9.7. Get description of current category.

9.8. Load the current category (from category page).

9.10. Get Current Category:

9.11. Load category by category id:

9.12. Load Product by product id:

9.13. Load Product by SKU:

9.14. Get product images with customized parameters (size, style, etc.) from product/view/media.phtml file

10. Cart/Checkout-Related Functions

10.1. Get all products in cart

10.2. Get total items and total quantity in cart

10.3. Get subtotal and grand total price of cart

10.4. Get products details present in your cart

11. CMS Page-Related Functions

11.1. Call a Magento static block from within a CMS page content area

11.2. Add Bread Crumb

11.3. Call static block from template file

11.4. Call a static block in .phtml file.

11.5. Display CMS block page.

12. Product Collections Related Functions

Product Collection class in magento is Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
12.1. Get All Products of a category

12.2. Get All products which are visible in front end.

12.3. Get all products which are eanbaled

12.4. Add Product Price To Collection

12.5. Filter collections by product ids.

In above code snippet only product with ids 1,2,3 remain in the collection. The function parameter is true/false, this means include or exclude products from collection.

12.6. Add Website ID to the collection

This code snippet adds website_id of each product to that collection. Only useful when using multiple websites in magento.

12.7. Filter Current Store Products

12.7. Filter Current Website Products

12.8. Get All Products Ids

This returns an array with only products ids of collection.

12.9. Add SEO Product URL

This adds SEO friends urls to our product collection.

12.10. Add Category Ids

This will add category ids to the products.

12.11. Add Tier Pricing

12.12. Magento Filtering Collections
addAttributeToFilter() is used to filter EAV collections. addAttributeToFilter() will filter the products in accordance with the attributes that you’ve included in your collection.

addFieldToFilter() is used to filter Non-EAV collections. addFieldToFilter() will filter the items based on columns in the database from the table catalog_product_entity.

addFieldToFilter() is mapped to addAttributeToFilter() for EAV entities. So you can just use addFieldToFiler().

12.13. Select collection by product attribute

addAttributeToFilter() is the function that can be called on a product collection in Magento. In other words, it adds a condition to the WHERE section of the MySQL query used to extract a product collection from the database.

12.14. Debugging The SQL Query
There are two ways to debug the query being executed when loading a collection in Magento.

12.15. AND & OR Operation
When you do query in database, we often need to make either OR or AND operation or even both. How do we create this in Magento?

12.16. Use of operators with examples:
Equals: eq

Not Equals – neq

Contains – Like – like

Not contains – not Like – nlike

Contains – In – in

No Contain – Not In – nin

Null- null

not Null- null

Greater than – gt

Less than – lt

Greater than o equals to – gteq

Less than o equals to – lteq

If we want to use it in other classes, try withAddFieldToFilter() function, whose uses in the same way.

Filtering by date:

Leave a Reply

Your email address will not be published. Required fields are marked *