Hi I have a set of rules set up to create a subscription service for a groceries shop. The real-life scenario is:
1- customer adds the subscription product (a basket with groceries) into his shopping cart
2- customer completes checkout
3- upon completing checkout a Rule loops over the products and checks whether a subscription product is bought
4- if a subscription product has been bought schedule a rule for next monday which creates a new order with the subscription product and completes checkout in name of this client
5- back to step 3 (ad infinitum)
The problem is that I get an error after cron (when the scheduled rule has run):
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1000000' for key 'order_number': INSERT INTO {commerce_order} (order_number, revision_id, type, uid, mail, status, created, changed, hostname, data) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 1000000 [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => commerce_order [:db_insert_placeholder_3] => 1 [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => cart [:db_insert_placeholder_6] => 1354897305 [:db_insert_placeholder_7] => 1354897305 [:db_insert_placeholder_8] => 188.205.30.132 [:db_insert_placeholder_9] => a:0:{} ) in drupal_write_record() (line 7106 of /srv/brammer/www/commerce.ruudmaaz.nl/includes/common.inc).
The Rule Component that creates the order is:
{ "rules_voeg_abonnement_toe_aan_cart" : {
"LABEL" : "Voeg Abonnement toe aan cart",
"PLUGIN" : "rule",
"REQUIRES" : [ "rules", "commerce_cart", "commerce_checkout" ],
"USES VARIABLES" : {
"user" : { "label" : "User", "type" : "user" },
"product" : { "label" : "Product", "type" : "commerce_product" }
},
"IF" : [
{ "entity_has_field" : { "entity" : [ "user" ], "field" : "field_abonnementen" } }
],
"DO" : [
{ "entity_create" : {
"USING" : {
"type" : "commerce_order",
"param_order_number" : "1000000",
"param_status" : "cart",
"param_type" : "commerce_order",
"param_owner" : [ "user" ],
"param_commerce_order_total" : [ "product:commerce-price" ]
},
"PROVIDE" : { "entity_created" : { "order_created" : "Created order" } }
}
},
{ "commerce_cart_product_add_by_sku" : {
"USING" : {
"user" : [ "user" ],
"sku" : [ "product:sku" ],
"quantity" : "1",
"combine" : 1
},
"PROVIDE" : { "product_add_line_item" : { "product_add_line_item" : "Added product line item" } }
}
},
{ "commerce_checkout_complete" : { "commerce_order" : [ "product-add-line-item:order" ] } }
],
"PROVIDES VARIABLES" : [ "user", "product" ]
}
}
What should I enter for Order-number? I tried using a high number 10000 as this comment said it doesn't matter what you enter here.: http://drupal.org/node/1109758#comment-4279684
Cheers,
Bram.
Comment | File | Size | Author |
---|---|---|---|
#7 | 0001-Issue-1861266-Allow-orders-to-be-created-through-Rul.patch | 957 bytes | geek-merlin |
#2 | screenshot-create_entity_form.png | 155.72 KB | garbo |
#2 | screenshot-create_entity_form_error_msg.png | 162.37 KB | garbo |
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedNotice the problem is a the "Duplicate entry" of that number. Order numbers must be unique, so you shouldn't be setting it to anything, really. Can you not create an order without choosing an order number?
Comment #2
garbo CreditAttribution: garbo commentedThe field is obligatory on the component-edit-form. See attached screenshot.
Could there be some token I can use here. Or something like: NULL or ? Or using a timestamp to enforce uniqueness (I couldn't find a timestamp token)?
Comment #3
garbo CreditAttribution: garbo commentedwhen I use
<none>
I also get the error.Comment #4
rszrama CreditAttribution: rszrama commentedSounds like we might need a feature request to allow order creation without specifying an order number. You should be able to leave that NULL somehow so it can default to the Order ID, basically.
Comment #5
garbo CreditAttribution: garbo commentedI would vote for that idea!
For now I fixed it by first creating a value of
site:current-cart-order:order-id + 1
and use that as product-numberBelow is the export of my component:
Comment #6
NoahJ CreditAttribution: NoahJ commentedIt seems that the functionality is already there, anyway. When lines 27-28 in commerce_order.info.inc (where $properties['order_number']['setter callback'] and $properties['order_number']['required'] are set) are commented out, the order number defaults to the automatically incremented order id.
Comment #7
geek-merlinThat's as easy as this.
Making order_number required in entity_property_info was a bug in the first place.
Comment #8
rszrama CreditAttribution: rszrama commentedThanks! Committed.