Couldnot create a coupon code programmatically

I am creating coupon code(shopping cart rule) programmatically in Magento Enterprise ver.

Coupon code gets created without any problem.

But when I add the condition with the third rule i.e "1--1--1" as shown below and execute it, It gives error as

"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (dbname.salesrule_product_attribute, CONSTRAINT FK_SALESRULE_PRODUCT_ATTRIBUTE_RULE_ID_SALESRULE_RULE_ID FOREIGN KEY (rule_id) REFERENCES salesrule (rule_id) ON DELETE C)' in D:\wamp\www\projectname\lib\Zend\Db\Statement\Pdo.php".

My code for condition is as shown below:

  $conditions = array(
        "1" => array(
            'type' => 'salesrule/rule_condition_combine',
            'aggregator' => 'all',
            'value' => 1,
            'new_child' => false
        "1--1" => array(
            'type' => 'salesrule/rule_condition_product_found',
            'value' => 1,
            'aggregator' => 'all',
            'new_child' => false
        "1--1--1" => array(
            'type' => 'salesrule/rule_condition_product',
            'attribute' => "category_ids",
            'operator' => "!=",
            'value' => "10"
$rule = Mage::getModel('salesrule/rule');

Can anyone help on this?


If you're not using it, I believe that disabling the Enterprise Rewards module can sometimes fix this PDO exception error. Try following this guide and see if it fixes your issue.

Tags: PHP / Magento / Magento 1.7

Similar questions

How to get coupon times used with coupon code using REST Api?
I have no experience with Magento. I was asigned the task to validate a coupon code via magento rest api. I've tried to use /V1/coupons/search GET method. My request url is: However, my java based server receives a 500 status response. I've also tried to make a curl request like this: curl -X GET --header "Authorization: Bearer some-token-string" H...
How to remove particular coupon code if other cart rule without specific coupon is applied on product in magento2?
condition 1: I have 2 cart rule which are directly applied on product if specific conditions are true as given in cart rule. condition 2: there is an another cart rule in which i have added one coupon code "10OFF" for all products. Question is that if for particular product one cart rule is already applied then i want to remove coupon code because ...
how to apply only 1 coupon code when there are multiples coupon codes
I am running magento 1.9 and i have multiple coupon codes on site. how to make that only 1 coupon code apply on cart? any body know how to apply on 1 coupon on cart?
Check coupon code programmatically api
I'm trying to make a php files, to apply coupon code to shipping cart. I explain. We have a mobile-commerce application, and wanna add coupon code. Creating them on magento, adding rules, date, products, there is no problem here, but when customer have choose products, they enter the coupon code in a text field. My php files take the code they ente...
Why Coupon code is not applied to cart programmatically?
I tried to generate coupon and apply to cart programmatically. With the following script coupon generate successfully but its not applied to cart, Even if tried to apply manually. If I just save this coupon code without any changes in admin panel and then applied to cart its works. I think I am missing something here. (I got this code from here) Pl...
programmatically check if coupon code is correct
I need to check to see if a coupon code is correct in an ajax controller, how is this done? I get the code like this: $param['code'];

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.