Database Reference
In-Depth Information
Note that we're including items.sku in our update spec. This allows us to use the
positional $ in our $set modifier to update the correct (matching) line item.
Both here and in the rollback operation, we're using the $inc modifier rather than
$set to update the quantity. This allows us to correctly handle situations where a
user might be updating the cart multiple times simultaneously (say, in two different
browser windows).
Here again, we're using the positional $ in our update to carted , so we need to in-
clude the cart_id in our update spec.
Once again, we're using _id in all our updates, so adding an index doesn't help us here.
Checking out
The checkout operation needs to do two main operations:
▪ Capture payment details.
▪ Update the carted items once payment is made.
Our basic algorithm here is as follows:
1. Lock the cart by setting it to pending status.
2. Collect payment for the cart. If this fails, unlock the cart by setting it back to active
status.
3. Set the cart's status to complete .
4. Remove all references to this cart from any carted properties in the product collec-
tion.
The code would look something like the following:
def
def checkout ( cart_id ):
now = datetime . utcnow ()
result = db . cart . update (
{ '_id' : cart_id , 'status' : 'active' },
 
 
 
 
Search WWH ::




Custom Search