0

I have written a script to parse xml files to create magento shopping cart rules. With one of the rule types I am getting the following error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1009-1-8-74' for key 'PRIMARY', query was: INSERT INTO `salesrule_product_attribute` (`rule_id`,`website_id`,`customer_group_id`,`attribute_id`) VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)

I taking the xml data and generating a post, then creating the rule as follows:

My post for this particular rule is:

[product_ids] => 
[talcode] => CA116P64
[name] => Buy XL set of six and A4 Frisk Cartridge pad for just £19.95!
[codetype] => 1
[description] => Buy XL set of six and A4 Frisk Cartridge pad for just £19.95!
[is_active] => 1
[website_ids] => Array
    (
        [0] => 1
    )

[amstore_ids] => Array
    (
        [0] => 0
    )

[customer_group_ids] => Array
    (
        [0] => 27
        [1] => 8
        [2] => 9
        [3] => 6
        [4] => 8
        [5] => 15
        [6] => 18
    )

[coupon_type] => 1
[coupon_code] => 
[uses_per_coupon] => 1
[uses_per_customer] => 1
[from_date] => 2016-02-26
[to_date] => 2017-02-23
[sort_order] => 
[is_rss] => 1
[simple_action] => by_fixed
[ampromo_type] => 0
[ampromo_discount_value] => 
[ampromo_min_price] => 
[ampromo_use_discount_amount] => 0
[ampromo_show_orig_price] => 0
[ampromo_free_shipping] => global
[ampromo_top_banner_alt] => 
[ampromo_top_banner_hover_text] => 
[ampromo_top_banner_link] => 
[ampromo_top_banner_gift_images] => 0
[ampromo_top_banner_description] => 
[ampromo_after_name_banner_alt] => 
[ampromo_after_name_banner_hover_text] => 
[ampromo_after_name_banner_link] => 
[ampromo_after_name_banner_gift_images] => 0
[ampromo_after_name_banner_description] => 
[ampromo_label_alt] => 
[promo_sku] => 
[discount_amount] => 2.85
[discount_qty] => 0
[discount_step] => 0
[apply_to_shipping] => 0
[simple_free_shipping] => 0
[stop_rules_processing] => 0
[rule] => Array
    (
        [conditions] => Array
            (
                [1] => Array
                    (
                        [type] => salesrule/rule_condition_combine
                        [aggregator] => all
                        [value] => 1
                        [new_child] => 
                    )

                [1--1] => Array
                    (
                        [type] => salesrule/rule_condition_product_found
                        [value] => 1
                        [aggregator] => all
                        [new_child] => 
                    )

                [1--1--1] => Array
                    (
                        [type] => salesrule/rule_condition_product
                        [attribute] => sku
                        [operator] => ==
                        [value] => FCP114
                    )

                [1--2] => Array
                    (
                        [type] => salesrule/rule_condition_product_found
                        [value] => 1
                        [aggregator] => any
                        [new_child] => 
                    )

                [1--2--1] => Array
                    (
                        [type] => salesrule/rule_condition_product
                        [attribute] => sku
                        [operator] => ()
                        [value] => DXLCS6,DXLGS6
                    )

            )

        [actions] => Array
            (
                [1] => Array
                    (
                        [type] => salesrule/rule_condition_product_combine
                        [aggregator] => all
                        [value] => 1
                        [new_child] => 
                    )

                [1--1] => Array
                    (
                        [type] => salesrule/rule_condition_product
                        [attribute] => sku
                        [operator] =>  == 
                        [value] => FCP114
                    )

            )

    )

[store_labels] => Array
    (
        [0] => Buy XL set of six and A4 Frisk Cartridge pad for just £19.95!
    )

[rule_id] => 1009

I have also created the same rule (successfully) via the admin, and killed the save() action and output the post. This is what I get:

Array
(
[form_key] => fv7EJZ2KEKmL3foK
[rule_id] => 1009
[product_ids] => 
[name] => CA116P64
[description] => 
[is_active] => 1
[website_ids] => Array
    (
        [0] => 1
    )

[customer_group_ids] => Array
    (
        [0] => 4
    )

[coupon_type] => 1
[coupon_code] => 
[uses_per_customer] => 0
[from_date] => 
[to_date] => 
[sort_order] => 0
[is_rss] => 1
[talcode] => CA116P64
[codetype] => 1
[rule] => Array
    (
        [conditions] => Array
            (
                [1] => Array
                    (
                        [type] => salesrule/rule_condition_combine
                        [aggregator] => all
                        [value] => 1
                        [new_child] => 
                    )

                [1--1] => Array
                    (
                        [type] => salesrule/rule_condition_product_found
                        [value] => 1
                        [aggregator] => all
                        [new_child] => 
                    )

                [1--1--1] => Array
                    (
                        [type] => salesrule/rule_condition_product
                        [attribute] => sku
                        [operator] => ==
                        [value] => FCP114
                    )

                [1--2] => Array
                    (
                        [type] => salesrule/rule_condition_product_found
                        [value] => 1
                        [aggregator] => all
                        [new_child] => 
                    )

                [1--2--1] => Array
                    (
                        [type] => salesrule/rule_condition_product
                        [attribute] => sku
                        [operator] => ()
                        [value] => DXLGS6, DXLCS6
                    )

            )

        [actions] => Array
            (
                [1] => Array
                    (
                        [type] => salesrule/rule_condition_product_combine
                        [aggregator] => all
                        [value] => 1
                        [new_child] => 
                    )

                [1--1] => Array
                    (
                        [type] => salesrule/rule_condition_product
                        [attribute] => sku
                        [operator] => ==
                        [value] => FCP114
                    )

            )

    )

[simple_action] => by_fixed
[discount_amount] => 2.85
[ampromo_type] => 0
[promo_sku] => 
[discount_qty] => 0
[discount_step] => 0
[apply_to_shipping] => 0
[simple_free_shipping] => 0
[stop_rules_processing] => 0
[store_labels] => Array
    (
        [0] => 
        [1] => 
        [2] => 
    )

[ampromo_discount_value] => 
[ampromo_min_price] => 
[ampromo_use_discount_amount] => 0
[ampromo_show_orig_price] => 0
[ampromo_free_shipping] => global
[ampromo_top_banner_alt] => 
[ampromo_top_banner_hover_text] => 
[ampromo_top_banner_link] => 
[ampromo_top_banner_gift_images] => 0
[ampromo_top_banner_description] => 
[ampromo_after_name_banner_alt] => 
[ampromo_after_name_banner_hover_text] => 
[ampromo_after_name_banner_link] => 
[ampromo_after_name_banner_gift_images] => 0
[ampromo_after_name_banner_description] => 
[ampromo_label_alt] => 

)

The only difference I see (other than the order of elements within the array) is the form key value, but this hasn't been a problem with the other rules that I have created.

Can anybody please shed any light on what might be causing the SQL error in this instance? The script all told imports circa 500 different shopping cart rules and it is just a small number that are throwing this error.

  • i want in Magento 2 , so i done some changes on class name and executed, but the condition and action rule is not set, but the coupon is getting created. – senthil Mar 12 at 10:00
0

I found that the problem was being caused by duplicate attribute combinations being input into the database (or trying to).

To resolve (although I realise I am fixing the symptom rather than the problem) I copied the file

app/code/core/Mage/SalesRule/Model/Resource/Rule.php

to

app/code/local/Mage/SalesRule/Model/Resource/Rule.php

and amended the function setActualProductAttributes by adding the following line at around line 292:

$data = array_unique($data); // Get rid of duplicate entries

This prevents the error and the rules are created as expected.

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.