Reindex Via Code
An example of code qhich allows you to reindex:
$indexer = Mage::getSingleton('index/indexer');
$process = $indexer->getProcessByCode('catalog_product_price');
$process->reindexEverything();
The following are indexer codes which you can use instead of the catalog_product_price
index above:
catalog_product_attribute Product Attributes
catalog_product_price Product Prices
catalog_url Catalog URL Rewrites
catalog_product_flat Product Flat Data
catalog_category_flat ...
Getting The Last Order ID From The Session
Mage::getSingleton('checkout/session')->getLastOrderId();
Adding A CSS Class To The Body Tag
To add a class via layout XML:
<reference name="root">
<action method="addBodyClass"><classname>whatever</classname></action>
</reference>
Or to add via code, from a controller or block
if ($root = $this->getLayout()->getBlock('root')) {
$root->addBodyClass('whatever');
}
EAV Tables
A list of EAV tables and their purpose.
|----------------------------+-------------------------------------------------------------------------------------|
| Table | Contains |
|----------------------------+-------------------------------------------------------------------------------------|
| eav_attribute | Attributes and their global config values |
| catalog_eav_attribute | Catalog...
Import Resetting visibility and stock attributes
If you are updating an existing product you need to include the stock and visibility attributes, otherwise the stock for the product will get set to 0 and out of stock, and the visibility will be reset to Catalog, Search.
Allowing Import Of Invisible Attributes
By setting a Product Attribute to be invisible via the visible
property, you stop the ability to import that attribute unless you alter the _forcedAttributesCodes
property of the relevant product type. The class names are:
Mage_ImportExport_Model_Import_Entity_Product_Type_Simple
Mage_ImportExport_Model_Import_Entity_Product_Type_Grouped
Mage_ImportExport_Model_Import_Entity_Product_Type_Configurable
The best method is probably to subclass the relevant pro...
Custom Admin Grid Column Renderer
When you need to display an image in a column, or format a value in a particular way, you can use a custom renderer to present the data differently.
Custom EAV Frontend Input Renderer
Handy for adding an attribute to a product or category which needs a custom frontend input type.
Image Resize Helper Composition
The product image resizing goes through a few layers of abstraction, so I thought I'd add a quick UML diagram showing composition through the layers. The resizing ultimately gets done by the gd PHP extension.
startSetup() and endSetup()
Ever wondered what they do? Basically they disable and then enable foreign key checks, and set the SQL mode to NO_AUTO_VALUE_ON_ZERO, then back to the old SQL mode. Below is the code taken from Varien_Db_Adapter_Pdo_Mysql
:
/**
* Run additional environment before setup
*
* @return Varien_Db_Adapter_Pdo_Mysql
*/
public function startSetup()
{
$this->raw_query("SET SQL_MODE=''");
$this->raw_query("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0");
$this->raw_query...
Attribute Source Model
Used when you want to use a multiselect or select dropdown box on an EAV entity, such as products or categories.
class Namespace_Module_Model_Select extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
/**
* Retrieve Full Option values array
*
* @param bool $withEmpty Add empty option to array
* @param bool $defaultValues
*
* @return array
*/
public function getAllOptions($withEmpty = true)
{
if (!$this->_options) {
...
Javascript Cookie Functions
This doesn't really go here, but hey ho. Magento's functions in js/Mage/cookies.js
are not very good.
/**
* Get a cookie value
*
* @param String name The cookie name
*
* @return String The cookie value
*/
getCookie: function(name)
{
var val = '';
var cookies = document.cookie.split('; ');
for (i = 0; i < cookies.length; i++) {
nameVal = cookies[i].split('=');
if (nameVal[0] == name) {
val = unescape(nameVal[1]);
}
...
Changing The Admin Theme
Put this in your local.xml
:
<config>
<stores>
<admin>
<design>
<theme>
<default>yourtheme</default>
</theme>
</design>
</admin>
</stores>
</config>
Fetching Store Contact E-Mail Addresses
// General Contact
$name = Mage::getStoreConfig('trans_email/ident_general/name');
$email = Mage::getStoreConfig('trans_email/ident_general/email');
// Sales Representative
$name = Mage::getStoreConfig('trans_email/ident_sales/name');
$email = Mage::getStoreConfig('trans_email/ident_sales/email');
// Customer Support
$name = Mage::getStoreConfig('trans_email/ident_support/name');
$email = Mage::getStoreConfig('trans_email/ident_support/email');
// Custom Email 1
$name = Mage::getStoreConf...
Order States and Statuses
The only state to hold more than one status by default is payment_review
, it seems a little odd to me that PayPal creates a state of pending_paypal
- that seems like it should be a child of pending_payment
.
- new
- pending
- pending_payment
- pending_payment
- payment_review
- payment_review
- fraud
- processing
- processing
- complete
- complete
- closed
- closed
- canceled
- canceled
- holded
- holded
Boilerplate Controller
<?php
/**
* Yourcompany.com
*
* PHP Version 5
*
* @category Namespace
* @package Namespace_Module
* @author Your Name <your.name@yourcompany.com>
* @copyright 2012 yourcompany.com
* @license http://www.yourcompany.com/license.txt Your license
* @link N/A
*/
/**
* A description of the controller
*
* @category Namespace
* @package Namespace_Module
* @author Your Name <your.name@yourcompany.com>
* @license http://www.yourcompany.co...
Paypal Express Flow
The PayPal Express Checkout Integration Guide is a great
source of well-written information for anything relating to the Express
checkout, including information regarding all of the API calls. Also, the PayPal
Express Checkout flow diagram illustrates the process well:
The same information as contained in the diagram above is also in the table
below:
|-------------------------------------------------------------------+-------------...
Disabling A Category
For some reason this does not work:
$category->setIsActive(false);
You must use this instead:
$category->setIsActive(0);
Stop Controller Dispatch
In an action controllers preDispatch() method you can stop dispatch by calling the following:
$this->setFlag('', self::FLAG_NO_DISPATCH, true);
If you were listening via an observer, your code would look like this:
public function someObserver(Varien_Event_Observer $observer)
{
$action = $observer->getEvent()->getControllerAction();
$action->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
}
Get A Products Stock Quantity
$product->getStockItem()->getQty();
Yes, there is no method for it - it's set using setStockItem()
from Mage_CatalogInventory_Model_Stock_Item::assignProduct()
via the catalog_product_load_after
event.
Getting A Products URL
Potentially confusing due to the 3 methods you could use, all of which are in Mage_Catalog_Model_Product
:
public function getUrlPath($category=null)
public function getUrlInStore($params = array())
public function getProductUrl($useSid = null)
The best way to explain is to simply show the results of several calls. Given a product whose URL key is mondrian-large-coffee-table-set-multicolour
on the domain of http://made.local
the results are:
$product->getUrlPath();
'mondrian-large-coffee-table-set-multicolour'...