Forked from herveguetin/cart_price_rule_1_steps.txt
Created
January 13, 2017 06:08
-
-
Save manojchowrasiya/15e6f5a747591c37e656b2965baa6c02 to your computer and use it in GitHub Desktop.
Programmatically create a cart price rule in Magento
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a quick an easy hack to programmatically create a cart price rule. | |
1. Temporarely edit Mage_Adminhtml_Promo_QuoteController::saveAction() | |
2. Around line 123, you will find $data = $this->getRequest()->getPost(); | |
3. Below this line create a new line containing : | |
var_export($data); | |
die(); | |
4. Go to the admin panel and create a cart promo rule as you want it to be | |
5. Save it and then the code created at step 3 will throw you the data as it has been posted on the step 4 form. | |
6. Copy the thrown code | |
7. Rollback steps 1, 2, 3. | |
8. Use the code method supplied in the cart_price_rule_setup.php file of this gist to create the rule based on code copied at step 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = ARRAY_PASTED_FROM_STEP_6; | |
$model = Mage::getModel('salesrule/rule'); | |
if (isset($data['rule_id'])) { | |
$model->load($data['rule_id']); | |
} | |
if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent' | |
&& isset($data['discount_amount']) | |
) { | |
$data['discount_amount'] = min(100, $data['discount_amount']); | |
} | |
if (isset($data['rule']['conditions'])) { | |
$data['conditions'] = $data['rule']['conditions']; | |
} | |
if (isset($data['rule']['actions'])) { | |
$data['actions'] = $data['rule']['actions']; | |
} | |
unset($data['rule']); | |
$model->loadPost($data); | |
$useAutoGeneration = (int)!empty($data['use_auto_generation']); | |
$model->setUseAutoGeneration($useAutoGeneration); | |
$model->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment