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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Notice 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?

garbo’s picture

The 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)?

garbo’s picture

Title: Allow orders to be created through Rules with a NULL order_number » Errors when creating a new order entity through Rules
Category: feature » support

when I use <none> I also get the error.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '<none>' 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] => <none> [: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] => 1354906746 [:db_insert_placeholder_7] => 1354906746 [:db_insert_placeholder_8] => 195.240.52.51 [:db_insert_placeholder_9] => a:0:{} ) in drupal_write_record() (line 7106 of xxxx.xxx/includes/common.inc).
rszrama’s picture

Title: Errors when creating a new order entity through Rules » Allow orders to be created through Rules with a NULL order_number
Category: support » feature

Sounds 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.

garbo’s picture

I 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-number
Below is the export of my component:

{ "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" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "site:current-cart-order:order-id" ],
            "op" : "+",
            "input_2" : "1"
          },
          "PROVIDE" : { "result" : { "next_cart_order_id" : "Next Cart_order_id" } }
        }
      },
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_order",
            "param_order_number" : [ "next-cart-order-id" ],
            "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" ]
  }
}
NoahJ’s picture

Title: Errors when creating a new order entity through Rules » Allow orders to be created through Rules with a NULL order_number
Category: support » feature

It 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.

geek-merlin’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
957 bytes

That's as easy as this.

Making order_number required in entity_property_info was a bug in the first place.

rszrama’s picture

Status: Needs review » Fixed

Thanks! Committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.