0

I've been trying to programmatically create a salesrule that applies a fixed discount if the given conditions are met:

  • The customer email is emailtest@test.com
  • The skuvisible (custom product attribute in our Magento) is 46306

For that, I came up with this code:

$conditions['conditions']['1'] = array(
    'type' => 'salesrule/rule_condition_combine',
    'aggregator' => 'all',
    'value' => '1',
    'new_child' => ''
);
$conditions['conditions']['1--1'] = array(
    'type' => 'amrules/rule_condition_customer',
    'attribute' => 'email',
    'operator' => '==',
    'value' => 'emailtest@test.com'
);
$conditions['conditions']['1--2'] = array(
    'type' => 'salesrule/rule_condition_product_found',
    'aggregator' => 'all',
    'value' => '1',
    'new_child' => ''
);
$conditions['conditions']['1--2--1'] = array(
    'type' => 'salesrule/rule_condition_product',
    'attribute' => 'skuvisible',
    'operator' => '==',
    'value' => '46306'
);

$model = Mage::getModel('salesrule/rule');

$data = array(
    'name' => "rule-name",
    'description' => "rule-name",
    'is_active' => '1',
    'website_ids' => array(126),
    'coupon_type' => Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC,
    'uses_per_customer' => 1,
    'from_date' => date('Y-m-d', strtotime('-3 hours')),
    'to_date' => date('Y-m-d', strtotime('-3 hours')),
    'is_rss' => '0',
    'simple_action' => Mage_SalesRule_Model_Rule::CART_FIXED_ACTION,
    'discount_amount' => 455,
    'discount_qty' => 0,
    'stop_rules_processing' => '1',
    'store_labels' => array(0 => "AllwaysOn-Other"),
    'salesrule_type' => 'default',
    'conditions' => $conditions['conditions']
);

$model->loadPost($data);
$model->save();

However, when I run it, I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`clara`.`salesrule_product_attribute`, CONSTRAINT `FK_SALESRULE_PRODUCT_ATTRIBUTE_RULE_ID_SALESRULE_RULE_ID` FOREIGN KEY (`rule_id`) REFERENCES `salesrule` (`rule_id`) ON DELETE CASCADE ON UPD)INSERT INTO `salesrule_product_attribute` () VALUES () Array
(
)


Trace:
#0 /home/morgan/soft-liness-2.glamit_magento1/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/morgan/soft-liness-2.glamit_magento1/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /home/morgan/soft-liness-2.glamit_magento1/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/morgan/soft-liness-2.glamit_magento1/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `sa...', Array)
#4 /home/morgan/soft-liness-2.glamit_magento1/lib/Varien/Db/Adapter/Pdo/Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `sa...', Array)
#5 /home/morgan/soft-liness-2.glamit_magento1/lib/Zend/Db/Adapter/Abstract.php(574): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `sa...', Array)
#6 /home/morgan/soft-liness-2.glamit_magento1/lib/Varien/Db/Adapter/Pdo/Mysql.php(1929): Zend_Db_Adapter_Abstract->insert('salesrule_produ...', Array)
#7 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/SalesRule/Model/Resource/Rule.php(293): Varien_Db_Adapter_Pdo_Mysql->insertMultiple('salesrule_produ...', Array)
#8 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/SalesRule/Model/Resource/Rule.php(133): Mage_SalesRule_Model_Resource_Rule->setActualProductAttributes(Object(Mage_SalesRule_Model_Rule), Array)
#9 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(544): Mage_SalesRule_Model_Resource_Rule->_afterSave(Object(Mage_SalesRule_Model_Rule))
#10 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Core_Model_Resource_Db_Abstract->save(Object(Mage_SalesRule_Model_Rule))
#11 /home/morgan/soft-liness-2.glamit_magento1/app/code/local/Ropit/Prueba/controllers/PruebaController.php(55): Mage_Core_Model_Abstract->save()
#12 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Ropit_Prueba_PruebaController->testcoupAction()
#13 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('testcoup')
#14 /home/morgan/soft-liness-2.glamit_magento1/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#15 /home/morgan/soft-liness-2.glamit_magento1/app/code/local/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#16 /home/morgan/soft-liness-2.glamit_magento1/app/Mage.php(691): Mage_Core_Model_App->run(Array)
#17 /home/morgan/soft-liness-2.glamit_magento1/index.php(112): Mage::run('clara', 'website')
#18 {main}

I don't get why this happens, because when I try to create the same rule using the admin panel with the same conditions, the error doesn't appear.

enter image description here

0

I finally figured it out! Turns out certain salesrule conditions require you to provide a customer_group_ids, otherwise Magento will attempt to perform an empty insert on salesrule_product_attribute table.

Therefore, the working code is as follows:

$customer_group = new Mage_Customer_Model_Group();
$allGroups = $customer_group->getCollection()->toOptionHash();
$groups = array();
foreach ($allGroups as $key => $value) {
    $groups[] = $key;
}

$conditions['conditions']['1'] = array(
    'type' => 'salesrule/rule_condition_combine',
    'aggregator' => 'all',
    'value' => '1',
    'new_child' => ''
);
$conditions['conditions']['1--1'] = array(
    'type' => 'amrules/rule_condition_customer',
    'attribute' => 'email',
    'operator' => '==',
    'value' => 'emailtest@test.com'
);
$conditions['conditions']['1--2'] = array(
    'type' => 'salesrule/rule_condition_product_found',
    'aggregator' => 'all',
    'value' => '1',
    'new_child' => ''
);
$conditions['conditions']['1--2--1'] = array(
    'type' => 'salesrule/rule_condition_product',
    'attribute' => 'skuvisible',
    'operator' => '==',
    'value' => '46306'
);

$model = Mage::getModel('salesrule/rule');

$data = array(
    'name' => "rule-name",
    'description' => "rule-name",
    'is_active' => '1',
    'website_ids' => array(126),
    'coupon_type' => Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC,
    'uses_per_customer' => 1,
    'from_date' => date('Y-m-d', strtotime('-3 hours')),
    'to_date' => date('Y-m-d', strtotime('-3 hours')),
    'is_rss' => '0',
    'simple_action' => Mage_SalesRule_Model_Rule::CART_FIXED_ACTION,
    'discount_amount' => 455,
    'discount_qty' => 0,
    'stop_rules_processing' => '1',
    'store_labels' => array(0 => "AllwaysOn-Other"),
    'salesrule_type' => 'default',
    'customer_group_ids' => $groups,
    'conditions' => $conditions['conditions']
);

$model->loadPost($data);
$model->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.