Magento IMP 6

Product Attribute Addition

This code shows all possible configuration options for an attribute that I know of, you need not include all of these when adding an attribute (although it won’t hurt)
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup'); if (!$installer->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'attribute_name')) { $installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'attribute_name', array( // TABLE.COLUMN: DESCRIPTION: 'label' => 'Label', // eav_attribute.frontend_label admin input label 'group' => 'General', // (not a column) tab in product edit screen 'sort_order' => 0, // eav_entity_attribute.sort_order sort order in group 'backend' => 'module/class_name', // eav_attribute.backend_model backend class (module/class_name format) 'type' => 'varchar', // eav_attribute.backend_type backend storage type (varchar, text etc) 'frontend' => 'module/class_name', // eav_attribute.frontend_model admin class (module/class_name format) 'note' => null, // eav_attribute.note admin input note (shows below input) 'default' => null, // eav_attribute.default_value admin input default value 'wysiwyg_enabled' => false, // catalog_eav_attribute.is_wysiwyg_enabled (products only) admin input wysiwyg enabled 'input' => 'input_name', // eav_attribute.frontend_input admin input type (select, text, textarea etc) 'input_renderer' => 'module/class_name', // catalog_eav_attribute.frontend_input_renderer (products only) admin input renderer (otherwise input is used to resolve renderer) 'source' => null, // eav_attribute.source_model admin input source model (for selects) (module/class_name format) 'required' => true, // eav_attribute.is_required required in admin 'user_defined' => false, // eav_attribute.is_user_defined editable in admin attributes section, false for not 'unique' => false, // eav_attribute.is_unique unique value required 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // catalog_eav_attribute.is_global (products only) scope 'visible' => true, // catalog_eav_attribute.is_visible (products only) visible on admin, setting to false stops import of this attribute 'visible_on_front' => false, // catalog_eav_attribute.is_visible_on_front (products only) visible on frontend (store) attribute table 'used_in_product_listing' => false, // catalog_eav_attribute.used_in_product_listing (products only) made available in product listing 'searchable' => false, // catalog_eav_attribute.is_searchable (products only) searchable via basic search 'visible_in_advanced_search' => false, // catalog_eav_attribute.is_visible_in_advanced_search (products only) searchable via advanced search 'filterable' => false, // catalog_eav_attribute.is_filterable (products only) use in layered nav 'filterable_in_search' => false, // catalog_eav_attribute.is_filterable_in_search (products only) use in search results layered nav 'comparable' => false, // catalog_eav_attribute.is_comparable (products only) comparable on frontend 'is_html_allowed_on_front' => true, // catalog_eav_attribute.is_visible_on_front (products only) seems obvious, but also see visible 'apply_to' => 'simple,configurable', // catalog_eav_attribute.apply_to (products only) which product types to apply to 'is_configurable' => false, // catalog_eav_attribute.is_configurable (products only) used for configurable products or not 'used_for_sort_by' => false, // catalog_eav_attribute.used_for_sort_by (products only) available in the 'sort by' menu 'position' => 0, // catalog_eav_attribute.position (products only) position in layered naviagtion 'used_for_promo_rules' => false, // catalog_eav_attribute.is_used_for_promo_rules (products only) available for use in promo rules )); }

Getting A Database Adapter (Read or Write)

To read:
Mage::getSingleton('core/resource')->getConnection('core_read');
To write:
Mage::getSingleton('core/resource')->getConnection('core_write');

Adding Breadcrumbs

Via code (usually in a controller):
$crumbs = Mage::app()->getLayout->getBlock('breadcrumbs'); $crumbs->addCrumb('home', array( 'label' => 'Home', 'title' => 'Go to Home Page', 'link' => Mage::getUrl('') ));
Via Layout XML:
<reference name="breadcrumbs"> <action method="addCrumb"> <crumbname>Home</crumbname> <crumbinfo> <label>Home</label> <title>Go to Home Page</title> <link>/</link> </crumbinfo> </action> </reference>

Cause FPC To Cache A Route

<config> <frontend> <cache> <requests> <module_controller_action>enterprise_pagecache/processor_default</module_controller_action> </requests> </cache> </frontend> </config>

Magento Model Diagram

This is a high-level view of the Magento Model, Resource Model and Resource Collection Model structure

In Stock Product Collection

This snippet provides a collection of in-stock products, or products which do not have stock management enabled on them
$productCollection = Mage::getResourceModel('catalog/product_collection'); Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection);

Using Design Exceptions To Serve a Mobile Theme

In order to serve a different package, or any element of a theme (translations, templates, skin or layout) you can use design exceptions, which allow you to specify different values for each of the design-related settings by using user-agent string matches

Event List

This page has a listing of all the Magento events - it has been extracted by grepping the source so may not be entirely complete.

Database Schema

This site shows details of the Magento database schema for differing versions - it shows foreign key relationships in a nice drag-and-drop interface

Setting And Getting Cookies

To set cookies:
Mage::getModel('core/cookie')->set('cookie_name', 'cookie_value', 0); # session cookie Mage::getModel('core/cookie')->set('cookie_name', 'cookie_value', 60); #lasts 60 seconds
To get cookies:
Mage::getModel('core/cookie')->get('made_productalerts_stock');

Various Link Types

Todo - this card should detail where links can be generated from (Mage, Mage_Core_Model_Store) and which methods generate them, and differentiate between web, link, direct link, skin, js and media links

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'), // merge vars Mage::app()->getStore()->getStoreId() // store id );

Product Types

TypeComposite1Inventory2DescriptionExample
Simple-YesThe most basic form of productExample
GroupedYesNoSeveral simple products displayed together, customer selection determinesone or moresimple products to be added to the basketExample
ConfigurableYesNoAllows choosing of options which determines a singlesimple product to be added to the basketExample
VirtualNoYesA simple product which will never be shipped, and as such needs no shipping information 
BundleYesNoAllows choosing of options which determinesone or moresimple products to be added to the basketExample
DownloadableNoYesA simple product with one or more downloads associated with it, essentially a virtual product with a download 
Gift CardNo?A simple product whose purchase results in a gift card being sent to the purchaser 
  1. A product which is composed of multiple simple products, and whose purchase results in one more of those products being purchased↩
  2. Whether inventory (quantity) is managable on the product type itself, irrespective of composed products↩

Restoring console.log

To do this permanently by altering your JS files, comment out this code on line 637 ofjs/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 (thanks to Alan Storm for this):
<default> <reference name="content"> <block type="core/text" name="fix.console" as="fix.console"> <action method="setText"> <text><![CDATA[<script type="text/javascript"> iframe = document.createElement('iframe'); iframe.style.display = 'none'; document.getElementsByTagName('body')[0].appendChild(iframe); window.console = iframe.contentWindow.console; console.firebug = "faketrue"; </script>]]></text> </action> </block> </reference> </default>
To do this as a one-off, you can copy and paste this code into the console of your browser window (also thanks to Alan Storm for this):
iframe = document.createElement('iframe'); iframe.style.display = 'none'; document.getElementsByTagName('body')[0].appendChild(iframe); window.console = iframe.contentWindow.console; console.firebug = "faketrue";

Collection Methods

A list of collection methods, similar to this

Order

Don’t get fooled by this:
$collection->setOrder('name', 'ASC'); echo (string) $collection->getSelect();
The reason that the ORDER BY statement is not in the select is that orders get added to the select uponload(), so this would work:
$collection->setOrder('name', 'ASC'); $colelction->load(); echo (string) $collection->getSelect();

Limit

The same applies to limitas does to order in respect of statement not being present until after load() is called.

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;

Formatting A Price For The Current Locale

$price = 500; $formattedPrice = Mage::helper('core')->currency($price, true, false);

No comments:

Post a Comment