Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Currently, Commerce 1.x doesn't store the order "placed" timestamp, we're currently handling that by abusing the "created" timestamp.
Commerce 2.x added a "placed" column to the schema, which is set via an eventSubscriber on "commerce_order.place.pre_transition".
Open questions:
- What should be the name of that column ("placed", "placed_date", "order_date", "ordered_date")? Most ERP systems call this "order_date"
- Once added, should we completly get rid of the "commerce_checkout_order_created_date_update" rule which sets the created timestamp on checkout completion, or should we keep it for backward compatibility
- Should we set that value programmatically or via an additional rule?
- Should we upgrade existing installs (Or just add the column), wondering if it's not too risky to set that value for existing installs that could potentially have a million orders.
Remaining tasks
- Add the "placed" column to the order schema
- Set the value on checkout completion
- Expose the new order property to views/Entity API
Data model changes
commerce_order table schema change (new column).
Comment | File | Size | Author |
---|---|---|---|
#19 | commerce_order-backport-placed-timestamp-2888407-19.patch | 34.32 KB | jsacksick |
| |||
#19 | interdiff-2888407-18-19.txt | 1.06 KB | jsacksick |
#17 | commerce_order-backport-placed-2888407-17.patch | 34.19 KB | krystalcode |
| |||
#2 | commerce_order-backport-placed-2888407-2.patch | 8.28 KB | jsacksick |
|
Comments
Comment #2
jsacksick CreditAttribution: jsacksick commentedI decided to go with "placed", I updated the commerce_orders view to include this new column. Should we update the placed timestamp manually in a hook_commerce_checkout_complete instead of relying on Rules, I'd be in favor of that, but decided to go with the rule to just comply with what was done before?
Comment #3
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedHi @jsacksick,
Comment #4
jsacksick CreditAttribution: jsacksick commentedI thought about doing it in MySQL as well, I'm just concerned that some installs might not be using the created timestamp the way we assume they are (You could totally disable the rule that sets the created timestamp on checkout completion).
We could probably directly set "placed" inside commerce_checkout_complete().
Comment #5
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedThat's a good point, it has to be optional then. Maybe the best would be to provide a script to manually upgrade and mention it in the release notes. It may be an option to ask a Yes/No question during the installation to run the script, but we would need a UI as well for those upgrading via the browser.
If people think it's not worth the hassle, then I guess we don't do it at all and leave it up to the user.
Comment #6
mglamanIn Commerce Reports there's a batch operation for generating tax records. The big question, then, is where do we want to show this and then possibly hide it, if ran. My only wonder is if we can provide a Drush script which piggybacks on the same batch operation. Which someone wrote a script, I think, for Commerce Reports to do the taxes that way.
All for
1) Setting it in hook, no need for a new rules action. This should be as concrete as when the created and updated timestamps are set.
2) Provide batch operation UI/Drush to allow people to populate field if they choose for old orders.
Comment #7
mglamanPer discussions with jsacksick, we are missing tests for this field. And then the no-Rules bit.
Remove from Rules. Just do it in the main process manually.
Comment #8
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedImproved patch attached.
Changes compared to previous patch
Upgrade strategy
Reasoning
I've given a bit more though on how to maintain backward compatibility so that we don't break existing installations, and how to handle the database upgrade. I think the best is to do the database upgrade i.e. copying the values from "created" to "placed" column for existing orders on all sites. This is for the following reasons.
Implementation details
Let me know what you think about all this.
Remaining tasks
Tests, I'll have a look into it.
Proposed release notes
Something along the lines:
Currently the "commerce_checkout_order_created_date_update" Rule (enabled by default) and the corresponding "enable_commerce_checkout_order_created_date_update" variable, update the "created" property to hold the checkout completion time. This behavior is now deprecated and a new order property is introduced in this release for this purpose. The value for the new column will be automatically populated during the upgrade for existing orders so that it holds the creation time or checkout completion time, depending on whether the site had that Rule enabled.
Sites that have the Rule enabled do not have to do anything, but it is recommended that they update any code or Views using the "created" property as the checkout completion time to use the new "placed" property and disable the Rule and set the variable to FALSE.
Sites that have the Rule disabled should be aware that the new "placed" property will be holding the order creation time for orders created prior to this update, rather than nothing. If that is an issue, they can maintain the current behavior by simply not use the new "placed" property in their code and Views and override the default Views provided by the module if they currently use them.
For more information about the changes follow the corresponding issue: https://www.drupal.org/node/2888407
Comment #9
mglamanWait, do we need to save here? Shouldn't invoker call the save?
Comment #10
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedYou're right, I'll remove the hook implementation and I'll do the following. Tested, it works.
Comment #11
jsacksick CreditAttribution: jsacksick commentedI think it shouldn't be set before invoking the rules event:
By default Commerce provides a rules that'll update the order status, that will trigger an order save, otherwise the value won't be saved.
The previous query is not enough... We'd need to update only placed orders (you'll update carts, canceled orders etc)..., we'd at least need to gather the completed statuses (i.e state="completed").
that doesn't sound like a good idea to me... "set to the checkout completion time" => you don't have that information actually...
Comment #12
krystalcode CreditAttribution: krystalcode at Acro Commerce commented@jsacksick I agree about the query. I'm confused with what you mean with the rest.
The meaning of "The new "placed" column is unconditionally set to the checkout completion time on all sites." refers to setting it from now on, it does not refer to updating existing orders. We do know when checkout is completed for an order when it happens, what is the information that we don't have? I highlighted this to point out the difference with the previous patch that introduced a variable to control the behavior, and which is TRUE by default and would do exactly the same unless you would explicitly set the variable to FALSE. I've amended the comment to make clear what I mean.
Do you mean "I think it shouldn't be set before" or "I think it should be set before"? Because I do have it after ...
Comment #13
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedActually taking into account the order status for existing orders when doing the database upgrade (which should be the case) makes things more complicated ... It should be done not only for the "completed" status, but also for "checkout_complete", "pending", "processing", "completed" and there could be many more statuses introduced by custom or contrib modules, such as the ones added by Commerce POS.
This might be a reason to consider again the solution to ask the users to do it via the admin UI, allowing them to choose which order statuses to consider as having gone past the "checkout_complete" phase.
Comment #14
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedIn line with the comments above, I'm posting a new patch.
Changes compared to #8
Remaining tasks
Questions
The ability exists for site admins to simulate the checkout process for an order. This can happen for orders that have already completed checkout, such as making a product change, or applying a discount. In these cases, do we want the checkout completion time to be updated as well? Or do we want to keep the first time that the order was "placed"? How is this handled in Commerce 2?
I'd vote for the second with the ability to change this behavior using a variable.
Comment #15
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedChanges compared to #14
Remaining Tasks
Remaining Questions
Comment #16
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedTests should now be fixed ...
Comment #17
krystalcode CreditAttribution: krystalcode at Acro Commerce commentedChanges compared to #16
Remaining tasks
None, unless something arises from reviewing the items below.
Review needed for the following
I'd like to get some public opinion on the following decisions I've made with this patch.
Proposed release notes
A new order property is introduced with this release that holds the order's checkout completion time. The current behavior of many sites that were storing it in the "created" property is now deprecated. It is recommended to visit the new Commerce Module Upgrades admin page in your store (admin/commerce/config/upgrades) and follow the instructions there for populating the new "placed" property for existing orders.
For more information about the changes follow the corresponding issue: https://www.drupal.org/node/2888407
Comment #18
jsacksick CreditAttribution: jsacksick commentedI've been working on this issue today (See attached patch), and implement the following changes compared to the previous patches:
- I removed the UI introduced for the upgrade and replaced it by a batched drush command (drush commerce-order-update-placed-timestamp).
- I slightly updated the Order tests
- I fixed some typos
- I altered the "Shopping carts" view, from commerce_cart_views_default_views_alter() to make sure the "Placed" timestamp isn't shown there (that was the case with the previous patches).
Comment #19
jsacksick CreditAttribution: jsacksick commentedFixed a small issue with the progress message shown by the drush command (See interdiff).
Comment #20
rszrama CreditAttribution: rszrama at Centarro commentedReviewed this, ran some local tests (on a site w/ 22k devel generated orders), and it all looks good. I made some minor changes including setting the empty placeholder to "-" for the Placed column similar to other columns in the orders view. Other than that, gonna commit it and will set the release notes accordingly!
Comment #22
rszrama CreditAttribution: rszrama at Centarro commentedComment #24
earthangelconsulting CreditAttribution: earthangelconsulting commentedQuestion: i just upgraded Commerce from 7.x-1.13 to 7.x-1.14, and ran update.php
i see that 'placed' is now a column in table commerce_order, and that the drush process commerce-order-update-placed-timestamp will update the 'placed' column from 'created', for existing orders, if that is desired
what i don't understand is: i just tried a new order, with the new 7.x-1.14 code, and 'placed' was NOT populated for that order!
what am i missing? is there a Rule that needs to be added or updated? if so, shouldn't it be in the .install file for 7.x-1.14?
i am surprised that the code isn't actually populating it!
please enlighten me!