public function applyItemTaxRate(Item $item) { $order = $item->Order(); // Orders shipped within New Zealand have tax applied if ($order && $order->exists() && $order->ShippingCountryCode == 'NZ') { $item->XeroTaxType = 'OUTPUT2'; $item->XeroTaxRate = 15.0; } else { $item->XeroTaxType = 'NONE'; $item->XeroTaxRate = 0.0; } $item->write(); }
/** * 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 ArrayList $productOptions The product variations to be added, usually just one */ public function addItem(Product $product, Variation $variation, $quantity = 1, ArrayList $options = null) { //Increment the quantity if this item exists already $item = $this->findIdenticalItem($product, $variation, $options); if ($item && $item->exists()) { $item->Quantity = $item->Quantity + $quantity; $item->write(); } else { DB::getConn()->transactionStart(); try { $item = new Item(); $item->ProductID = $product->ID; $item->ProductVersion = $product->Version; //TODO: Think about percentage discounts and stuff like that, needs to apply to variation as well for total price to be correct //TODO: Do not use Amount() here, need another accessor to support price discounts and changes though $item->Price = $product->Amount()->getAmount(); $item->Currency = $product->Amount()->getCurrency(); if ($variation && $variation->exists()) { $item->VariationID = $variation->ID; $item->VariationVersion = $variation->Version; //TODO: Do not use Amount() here, need another accessor to support price discounts and changes though $item->Price += $variation->Amount()->getAmount(); } $item->Quantity = $quantity; $item->OrderID = $this->ID; $item->write(); if ($options->exists()) { foreach ($options as $option) { $option->ItemID = $item->ID; $option->write(); } } } catch (Exception $e) { DB::getConn()->transactionRollback(); SS_Log::log(new Exception(print_r($e->getMessage(), true)), SS_Log::NOTICE); throw $e; } DB::getConn()->transactionEnd(); } $this->updateTotal(); return $item; }
/** * 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(); }