- Magento Front Controller
- Reinstalling Magento Modules
- Clearing the Magento Cache
- Magento’s Class Instantiation Abstraction and Autoload
- Magento Development Environment
- Logging Magento’s Controller Dispatch
- Magento Configuration Lint
- Slides from Magento Developer’s Paradise
- Generated Magento Model Code
- Magento Knowledge Base
- Magento Connect Role Directories
- Magento Base Directories
- PHP Error Handling and Magento Developer Mode
- Magento Compiler Mode
- Magento: Standard OOP Still Applies
- Magento: Debugging with Varien Object
- Generating Google Sitemaps in Magento
- IE9 fix for Magento
- Magento’s Many 404 Pages
- Magento Quickies
- Commerce Bug in Magento CE 1.6
- Welcome to Magento: Pre-Innovate
- Magento’s Global Variable Design Patterns
- Magento 2: Factory Pattern and Class Rewrites
- Magento Block Lifecycle Methods
- Goodnight and Goodluck
- Magento Attribute Migration Generator
- Fixing Magento Flat Collections with Chaos
- Pulse Storm Launcher in Magento Connect
- StackExchange and the Year of the Site Builder
- Scaling Magento at Copious
- Incremental Migration Scripts in Magento
- A Better Magento 404 Page
- Anatomy of the Magento PHP 5.4 Patch
- Validating a Magento Connect Extension
- Magento Cross Area Sessions
- Review of Grokking Magento
- Imagine 2014: Magento 1.9 Infinite Theme Fallback
- Magento Ultimate Module Creator Review
- Magento Imagine 2014: Parent/Child Themes
- Early Magento Session Instantiation is Harmful
- Using Squid for Local Hostnames on iPads
- Magento, Varnish, and Turpentine
This is a quick one for advanced users, and was inspired by a recent question I answered over at StackOverflow.
One of the nice things about a well abstracted system like Magento is the opportunities it presents for meta-programming. Drop the following code in a controller action (yes, you can define an inner function in PHP)
function someAction()
{
header('Content-Type: text/plain');
$coupon = Mage::getModel('salesrule/rule')->load(1);
function outputValue($value)
{
switch(gettype($value))
{
case 'string':
echo "'".str_replace("'","\\'",$value)."'";
break;
case 'array':
echo "array(";
foreach($value as $v)
{
outputValue($v);
echo ",";
}
echo ")";
break;
case 'NULL':
echo 'NULL';
break;
default:
echo "'can\'t handle ".gettype($value)."'";
}
}
echo '$model';
foreach($coupon->getData() as $key=>$value)
{
echo '->set';
echo str_replace(' ', '',ucwords(str_replace('_', ' ', $key)));
echo '(';
outputValue($value);
echo ')';
echo "\n";
}
exit;
}
Run this code and you’ll get some auto-generated code for the creation/updating of a Magento salesrule/rule
Model (assuming you have a Model with an id of 1)
$model->setRuleId('1')
->setName('test')
->setDescription('')
->setFromDate('2010-05-09')
->setToDate(NULL)
->setCouponCode('')
->setUsesPerCoupon('0')
->setUsesPerCustomer('0')
->setCustomerGroupIds(array('1',))
->setIsActive('1')
->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setStopRulesProcessing('0')
->setIsAdvanced('1')
->setProductIds('')
->setSortOrder('0')
->setSimpleAction('by_percent')
->setDiscountAmount('10.0000')
->setDiscountQty(NULL)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setTimesUsed('0')
->setIsRss('1')
->setWebsiteIds(array('1',))