Display Out Of Stock Configuration Option
The decision of whether to show or hide a product based on this setting is implemented
by the Product Price Index.
You can verify this by changing the setting to "Yes" and then viewing a category with an
out of stock product; you see the product is present. If you then change the setting to
"No" and view the category again you will see the out of stock product is still present.
You will also notice that these two indexes now need refreshing:
- Product Attributes (catalog_product_...
Setting Data Per-Store On Entites
Certain entities (Products and Categories, for example) have the concept of a
store scope, whereby you can set the default value for all stores, or you can
set a value on a per-store basis.
You could do it this way, and this is probably how most guides will tell you to
do it, but I'll show you in a minute why not:
$product = Mage::getModel('catalog/product');
$product->setStoreId(1);
$product->load(100);
$product->setName('Store 1 Product Name');
$product->save();
So, you'll probably have noticed that worked, however w...
Altering Config Data During Setup
Assuming $installer
refers to Mage_Core_Model_Resource_Setup
or a subclass of it, you can do the following:
Setting a value in the default scope:
$installer->setConfigData('some/path', 'value');
Setting a value in a specific store:
$installer->setConfigData('some/path', 'value', 'stores', 1);
Deleting a value from all scopes:
$installer->deleteConfigData('some/path');
Deleting a value from a certain scope (unfortunately you cannot choose which scope ID though:
$installer->deleteConfigData('some/path', 'stores');
...
Cache Clearing And General Information
Clean everything (use either):
Mage::app()->getCacheInstance()->flush();
Mage::app()->getCache()->clean();
Clean specific types:
Mage::app()->getCacheInstance()->cleanType('config');
Mage::app()->getCacheInstance()->cleanType('layout');
Mage::app()->getCacheInstance()->cleanType('block_html');
Mage::app()->getCacheInstance()->cleanType('translate');
Mage::app()->getCacheInstance()->cleanType('collections');
Mage::app()->getCacheInstance()->cleanType('eav');
Mage::app()->getCacheInstance()->cleanType('co...
Config Menu Definition
This should go in adminhtml.xml. Also see Config ACL Definition (note that the XML path of the ACL entry needs to match up with the XML path of the menu entry).
To place an entry under the sales node (alter the name for other nodes):
<config>
<menu>
<sales>
<children>
<namespace_module translate="title" module="namespace_module">
<title>Your Module</title>
<sort_order>10...
Config ACL Definition
This should go in adminhtml.xml
. Also see Config Menu Definition (note that the XML path of the menu entry needs to match up with the XML path of the ACL entry). To see how to implement the checking of an ACL within an admin controller see the _isAllowed()
method in Boilerplate Admin Controller.
To place an entry under the sales node (alter the name for other nodes):
<adminhtml>
<acl>
...
Adding A Layout Handle From A Controller
Doing this is a slight pain, to quote what Alan Storm said on StackOverflow:
- If you add your handle before you call $this->loadLayout() from a controller it's too soon
- If you add your handle after you call $this->loadLayout() it's too late
So, if you really need to add a handle to a controller, you need to replace the call to $this->loadLayout()
with:
$update = $this->getLayout()->getUpdate();
$update->addHandle('default');
$thi...
Config URL Rewrite Definition
<config>
<global>
<rewrite>
<namespace_module>
<from><![CDATA[#^/some/regex/([a-z]*/?$#]]></from>
<to><![CDATA[/frontname/whatever/whatever/blah/$1]]></to>
</namespace_module>
</rewrite>
</global>
</config>
Config Model Rewrite Definition
<config>
<global>
<models>
<catalog>
<rewrite>
<product>Namespace_Module_Model_Catalog_Product</product>
</rewrite>
</catalog>
</models>
</global>
</config>
Pretty Backtrace / Stack Trace
In app/code/core/Mage/Core/functions.php
Magento offers a function named mageDebugBacktrace()
which is nice, but I thought could be made nicer. This backtrace shows frame numbers, file names and line numbers, classes, methods and arguments where possible. It can be placed anywhere you fancy, as long as it is included by Magento - I normally add it to functions.php when needed, and remove when I'm done.
Example output:
[ 0] app/code/core/Enterprise/Search/Model/Adapter/Abstract.php:414 Enterprise_Search_Model_Adapter_HttpSt...
Add comment to form input in admin
$afterElementHtml = '<p class="nm"><small>' . ' this is the hint! ' . '</small></p>';
$linkFieldset->addField('field_name', 'text', array(
'after_element_html' => $afterElementHtml,
));
Config Two-Level Memcached & DB Definition
<config>
<global>
<cache>
<backend>memcached</backend>
<slow_backend>database</slow_backend>
<id_prefix>cache_</id_prefix>
<memcached>
<servers>
<server1>
<host><![CDATA[localhost]]></host>
<port><![CDATA[11211]]></port>
<persistent><![CDATA[0]]></persistent>
<weight><![CDATA[1]]></weight>
...
Config Redis Cache Definition
You need Cm_Cache_Backend_Redis for this
<config>
<global>
<cache>
<backend>Cm_Cache_Backend_Redis</backend>
<backend_options>
<server>localhost</server> <!-- or absolute path to unix socket for better performance -->
<port>6379</port>
<database>0</database>
<force_standalone>0</force_standalone> <!-- 0 for phpredis, 1 for standalone PHP -->
...
Category Product Collection
This snippet provides a collection of products within a category
$cat = Mage::getModel('catalog/category')->load(1);
$coll = Mage::getResourceModel('catalog/product_collection');
$coll->addCategoryFilter($cat);
Block Caching
To cache a block individually, add this method to the blocks class:
protected function _construct()
{
$this->addData(array(
'cache_lifetime' => 3600,
'cache_tags' => array(Mage_Cms_Model_Block::CACHE_TAG),
'cache_key' => 'CACHE_KEY',
));
}
_Note - if you set cache_lifetime to 0 (or any other value which PHP evaluates to false) then the cache will actually last 7200 seconds (2 hours), so in order to have a very high cache time then the cache_lifetime should be set ...
Config Transactional Email Template Definition
<config>
<global>
<template>
<email>
<your_module_email_something_template translate="label" module="namespace_module">
<label>Something</label>
<file>namespace/module/something.html</file>
<type>html</type>
</your_module_email_something_template>
</email>
</template>
</global>
</config>
![](http://mikewhitby.co.uk/makandra-track.gif?a=config-transacti...
Sandbox Script
Sometimes you want to run some code to see what it does, dump an object out or check a class but the thing you want to run/dump/check is hidden deep in the bowels of the code somewhere, and you feel it might be easier to run a bit of PHP to check just that one thing, without having to fire up or your browser, or wait for cron to run - that's where this sandbox script comes in.
To use either of the examples below, save them into a file in the root of your magento install called sandbox.php
, then either browse to the directory and run `php ...
Overriding A Controller
<config>
<frontend>
<routers>
<checkout>
<args>
<modules>
<your_module before="Mage_Checkout">Your_Module</your_module>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Formatting A Price For The Current Locale
$price = 500;
$formattedPrice = Mage::helper('core')->currency($price, true, false);
Resizing A Product Image
$helper = Mage::helper('catalog/image');
$helper->init($product, 'image');
$helper->resize(216, 161);
// the helpers __toString() method outputs the url
echo $helper;
Collection Manipulation (Filtering, Limiting, etc)
A list of collection methods, similar to this
Where
OR:
You have to first specify an array of attributes in the filter, then a two dimensional array, with each second dimension have the filters for attribute n, as below:
$this->addFieldToFilter(
array('email', 'customer_id'),
array(
array('eq' => $customer->getEmail()), // filters for e-mail
array('eq' => $customer->getId()), // filters for...
Restoring console.log
To do this permanently by altering your JS files, comment out this code on line 637 of js/varien/js/js
:
if (!("console" in window) || !("firebug" in console))
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
Or, you could paste this into your layout somewhere (...
Product Types
Type | Composite[^1] | Inventory[^2] | Description | Example |
---|---|---|---|---|
Simple | - | Yes | The most basic form of product | Example |
Grouped | Yes | No | Several simple products displayed together, customer selection determines one or more simple products to be added to the basket, which are shown in the basket as seperate products | Example |
Configurable | Yes | No | Allows choosing of options which determin... |
Sending Transactional E-Mails
$email = Mage::getModel('core/email_template');
$email->sendTransactional(
'some_email_template', // template
array('name' => 'Your Company', 'email' => 'contact@yourcompany.com'), // sender details
'joe@joebloggs.com', // recipient email
'Joe Bloggs', // recipient name
array('customerName' => 'Joe Bloggs'), /...