/**
  * Add an item to the order representing the product, 
  * if an item for this product exists increase the quantity. Update the Order total afterward.
  * 
  * @param DataObject $product The product to be represented by this order item
  * @param DataObjectSet $productOptions The product variations to be added, usually just one
  */
 function addItem(DataObject $product, $quantity = 1, DataObjectSet $productOptions = null)
 {
     //Check that product options exist if product requires them
     //TODO perform this validation in Item->validate(), cannot at this stage because Item is written before ItemOption, no transactions, chicken/egg problem
     if ((!$productOptions || !$productOptions->exists()) && $product->requiresVariation()) {
         user_error("Cannot add item to cart, product options are required.", E_USER_WARNING);
         //Debug::friendlyError();
         return;
     }
     //Increment the quantity if this item exists already
     $item = $this->findIdenticalItem($product, $productOptions);
     if ($item && $item->exists()) {
         $item->Quantity = $item->Quantity + $quantity;
         $item->write();
     } else {
         //TODO this needs transactions for Item->validate() to check that ItemOptions exist for Item before it is written
         $item = new Item();
         $item->ObjectID = $product->ID;
         $item->ObjectClass = $product->class;
         $item->ObjectVersion = $product->Version;
         $item->Amount->setAmount($product->Amount->getAmount());
         $item->Amount->setCurrency($product->Amount->getCurrency());
         $item->Quantity = $quantity;
         $item->OrderID = $this->ID;
         $item->write();
         if ($productOptions && $productOptions->exists()) {
             foreach ($productOptions as $productOption) {
                 $itemOption = new ItemOption();
                 $itemOption->ObjectID = $productOption->ID;
                 $itemOption->ObjectClass = $productOption->class;
                 $itemOption->ObjectVersion = $productOption->Version;
                 $itemOption->Amount->setAmount($productOption->Amount->getAmount());
                 $itemOption->Amount->setCurrency($productOption->Amount->getCurrency());
                 $itemOption->ItemID = $item->ID;
                 $itemOption->write();
             }
         }
     }
     $this->updateTotal();
 }