Magento-Functions - A Resource of Magento Functions

Overview

Magento-Functions

A Resource of Magento Functions

Table of Contents


  1. Category
  2. Product
  3. User
  4. Cart
  5. Checkout
  6. General
  7. Account
  8. [Working w/ URL's] (#urls)

Category

Product

Fetch Product Collection

$collection = Mage::getModel('catalog/product')
              ->getCollection()
              ->addAttributeToSelect('*')
              ->addAttributeToSort('name', 'DESC')
              ->setOrder('name', 'ASC');

Magento Load Products

Individual Product Helper
--------------------------------------------------------------------------------------
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();

Load Product from Collection
--------------------------------------------------------------------------------------
// Load by name
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'product_name');

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

//Load by ID (just load):
$_product = Mage::getModel('catalog/product')->load($productID);

Fetch Default Product Info


echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getSku(); //product Sku
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url
echo $_product->getAvailability(); //product's thumbnail image url     
?>

Custom Product Attributes

For drop-down Product Attributes use the following code 
--------------------------------------------------------------------------------------
 echo $_product->getAttributeText('attribute_name') ?>


For all other Product Attribute types
--------------------------------------------------------------------------------------
 echo $_product->getAttributeName() ?>

-or-

 echo $_product['attribute_name'];?>


Display Product Attributes Globally
--------------------------------------------------------------------------------------
 if($product->getResource()->getAttribute('ATTRIBUTE_CODE_HERE')->getFrontend()->getValue($product)) : ?> 

  echo 'Attribute Title: '.$product->getResource()->getAttribute('ATTRIBUTE_CODE_HERE')->getFrontend()->getValue($product); ?>  

 endif; ?>

Product Stock Count

 
$stock_count = (int) Mage::getModel(’cataloginventory/stock_item’)->loadByProduct($this->getProduct())->getQty(); 
echo $stock_count;
?>

User

/* Check if the customer is logged in or not */
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
 
    /* Get the customer data */
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    /* Get the customer's full name */
    $fullname = $customer->getName();
    /* Get the customer's first name */
    $firstname = $customer->getFirstname();
    /* Get the customer's last name */
    $lastname = $customer->getLastname();
    /* Get the customer's email address */
    $email = $customer->getEmail();
 
}

Cart

/* Get all the items in the cart */
$cartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
/* Iterate through the items */
foreach ($cartItems as $item) {
 /* Load the product and get the custom attribute */
 Zend_Debug::dump(Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getMyCustomAttribute());
}

Checkout

Shipping

Retrive Shipping Method from Quote
--------------------------------------------------------------------------------------
$rate = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection();

$rate->getCarrier(); // This will provide you with the carrier code
$rate->getCarrierTitle(); // This will give you the carrier title
$rate->getCode(); // This will give you **current shipping method** code
$rate->getMethod(); // This will provide you with the **shipping method** code
$rate->getMethodTitle(); // This will tell you current shipping method title
$rate->getMethodDescription(); // And this is the description of the current shipping method and **it could be NULL**

General

Working with Blocks

xxxxxx Step 4: Navigate your way to the template folder (app > design > frontend > default > your_theme > template) Open the file that you would like the block to appear in and insert the following code in the appropriate position where xxxxxx is the ‘Identifier’ you set earlier when creating your block. getChildHtml('xxxxxx') ?> Render Block within .phtml getLayout()->createBlock('cms/block')->setBlockId('my-new-block')->toHtml() ?>">
Step 1:
Create your static block
 
Step 2:
Open the file that references the page you intend to put the block into IE‘page.xml’.
 
Step 3:
Add this to the appropriate place in the XML file
 

   
  
    
     
      xxxxxx
     
    

   
 
Step 4:
Navigate your way to the template folder (app > design > frontend > default > your_theme > template) Open the file that you would like the block to appear in and insert the following code in the appropriate position where xxxxxx is the ‘Identifier’ you set earlier when creating your block.
 
getChildHtml('xxxxxx') ?>
 
 
Render Block within .phtml
 
getLayout()->createBlock('cms/block')->setBlockId('my-new-block')->toHtml() ?>

####Passing Data Through Blocks

getData('product'); ?>">

getChild('block_name')->setData("product", $_product); ?>


getData('product'); ?>

Account

Navigation Links

Removing Navigation Links

In local.xml add the following

recurring_profiles billing_agreements reviews downloadable_products OAuth Customer Tokens account account_edit address_book orders tags wishlist newsletter ">

   
    
    
            
     
      
       recurring_profiles
      
     
            
     
      
       billing_agreements
      
     
            
     
      
       reviews
      
     
            
     
      
       downloadable_products
      
     
            
     
      
       OAuth Customer Tokens
      
     

            
     
      
       account
      
     
            
     
      
       account_edit
      
     
            
     
      
       address_book
      
     
            
     
      
       orders
      
     
            
     
      
       tags
      
     
            
     
      
       wishlist
      
     
            
     
      
       newsletter
      
     
    
    

   

Adding Navigation Links

In local.xml add the following

my_new_section module/index/index <_secure>true ">

   
    
    
        
     
            
      
       my_new_section
      
            
      
       module/index/index
      
            
            
      
            
      
       <_secure>true
      
        
     
    
    

   

URLs

To Retrieve URL path in STATIC BLOCK
---------------------------------------
To get SKIN URL
{{skin url='images/sampleimage.jpg'}}
 
To get Media URL
{{media url='/sampleimage.jpg'}}
 
To get Store URL
{{store url='mypage.html'}}
 
To get Base URL
{{base url='yourstore/mypage.html'}}
 
 
TO Retrieve URL path in PHTML
---------------------------------------
 
Not secure Skin URL:
 echo $this->getSkinUrl('images/sampleimage.jpg') ?>
 
Secure Skin URL
 echo $this->getSkinUrl('images/ sampleimage.gif', array('_secure'=>true)) ?>
 
Get  Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();
 
Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();
 
Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
 
Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
 
Get Magento Skin Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
 
Get Magento Store Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
 
Get Magento Js Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);



Mage::getBaseUrl() => Gets base url path e.g. http://my.website.com/

Mage::getBaseUrl('media') => Gets MEDIA folder path e.g. http://my.website.com/media/

Mage::getBaseUrl('js') => Gets JS folder path e.g. http://my.website.com/js/

Mage::getBaseUrl('skin') => Gets SKIN folder path e.g. http://my.website.com/skin/

Get DIRECTORY paths (physical location of your folders on the server) – Relative URL Path

Mage::getBaseDir() => Gives you your Magento installation folder / root folder e.g. /home/kalpesh/workspace/magento

Mage::getBaseDir('app') => Gives you your Magento's APP directory file location e.g. /home/kalpesh/workspace/magento/app

Mage::getBaseDir('design') => Gives you your Magento's DESIGN directory file location e.g. /home/kalpesh/workspace/magento/design

Mage::getBaseDir('media') => Gives MEDIA directory file path

Mage::getBaseDir('code') => Gives CODE directory file path

Mage::getBaseDir('lib') => Gives LIB directory file path

Mage::helper('core/url')->getCurrentUrl()

base Mage::getBaseDir()

Mage::getBaseDir('base') /var/www/magento/

app Mage::getBaseDir('app') /var/www/magento/app/

code Mage::getBaseDir('code') /var/www/magento/app/code

design Mage::getBaseDir('design') /var/www/magento/app/design/

etc Mage::getBaseDir('etc') /var/www/magento/app/etc

lib Mage::getBaseDir('lib') /var/www/magento/lib

locale Mage::getBaseDir('locale') /var/www/magento/app/locale

media Mage::getBaseDir('media') /var/www/magento/media/

skin Mage::getBaseDir('skin') /var/www/magento/skin/

var Mage::getBaseDir('var') /var/www/magento/var/

tmp Mage::getBaseDir('tmp') /var/www/magento/var/tmp

cache Mage::getBaseDir('cache') /var/www/magento/var/cache

log Mage::getBaseDir('log') /var/www/magento/var/log

session Mage::getBaseDir('session') /var/www/magento/var/session

upload Mage::getBaseDir('upload') /var/www/magento/media/upload

export Mage::getBaseDir('export') /var/www/magento/var/export
You might also like...
All PHP functions, rewritten to throw exceptions instead of returning false

Safe PHP This project is deprecated Because of how this project needs to be in sync with the official PHP documentation, maintaining a set of function

All PHP functions, rewritten to throw exceptions instead of returning false, now for php8

A set of core PHP functions rewritten to throw exceptions instead of returning false when an error is encountered.

A package that provides `array_*` like functions for iterators.

The doekenorg/iterator-functions package provides a curated set of array_* like functions for iterators in PHP. This package is built to encourage developers to make more use of Iterators by simplifying common tasks.

A utility package that helps inspect functions in PHP.

A utility package that helps inspect functions in PHP. This package provides some utilities for inspecting functions (callables) in PHP. You can use i

Adds factory functions for WooCommerce to be used with wp-browser integration tests.

wp-browser-woocommerce This library simplifies testing of WooCommerce themes and plugins with wp-browser. Several Unit Test Factories are added that a

A collection of functions to clean up WordPress

Clean WordPress Admin A collection of functions to clean up WordPress front and back-end to make it easier for editors to work and for you to look at

Collection of useful PHP functions, mini-classes, and snippets for every day.

JBZoo / Utils Collection of PHP functions, mini classes and snippets for everyday developer's routine life. Install composer require jbzoo/utils Usage

This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.

Symfony Polyfill / Intl: ICU This package provides fallback implementations when the Intl extension is not installed. It is limited to the "en" locale

just the most basic functions of the website.

cybergrungewebsite just the most basic functions of the website. upload handling the artists folder contains custom upload.php which handles uploads.

Owner
Bryan Littlefield
Bryan Littlefield
OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures all the essential functionalities required for any enterprise.

OrangeHRM Open Source Application OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures all the essential functionalities

OrangeHRM 452 Jan 4, 2023
590+ usernames in this dictionary! A list of reserved usernames to prevent url collision with resource paths.

590+ usernames in this dictionary! A list of reserved usernames to prevent url collision with resource paths. This repository hosts the list in multiple formats like JSON, CSV, SQL and plain text. You can use its just download its by wget.

ShouldBee 546 Dec 22, 2022
This plugin allows you to display code from a repository, such as Github, Gitlab or others in order to use it as a resource in the courses.

Fetch Code This plugin allows you to display code from a repository, such as Github, Gitlab or others in order to use it as a resource in the courses.

Cambá Laboratorio de Tecnología 0 Dec 26, 2021
Sentrifugo is a FREE and powerful Human Resource Management System (HRMS) that can be easily configured to meet your organizational needs.

Sentrifugo Sentrifugo is a free and powerful new-age Human Resource Management System that can be easily configured to adapt to your organizational pr

Sentrifugo 447 Dec 27, 2022
ResoLib - Resource Library System for University

ResoLib Resource Library System for University. Introduction When was the last time you were trapped trying to find resources when you needed them and

Keshaw Soni 2 Apr 24, 2022
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

EcomDev B.V. 51 Dec 5, 2022
A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel

Underscore.php The PHP manipulation toolbelt First off : Underscore.php is not a PHP port of Underscore.js (well ok I mean it was at first). It's does

Emma Fabre 1.1k Dec 11, 2022
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-PHP Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects. Lodash-PHP tr

Lodash PHP 474 Dec 31, 2022