Source of file OrderItemList.php
Size: 1,439 Bytes - Last Modified: 2021-12-24T06:35:03+00:00
/var/www/docs.ssmods.com/process/src/src/ORM/OrderItemList.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | <?php namespace SilverShop\ORM; use SilverStripe\ORM\HasManyList; /** * Additional functions for Item lists. */ class OrderItemList extends HasManyList { public function Quantity() { return $this->Sum('Quantity'); } public function Plural() { return $this->Quantity() > 1; } /** * Sums up all of desired field for items, and multiply by quantity. * Optionally sum product field instead. * * @param string $field - field to sum * @param boolean $onproduct - sum from product or not * * @return number - sum total of field */ public function Sum($field, $onproduct = false) { $total = 0; foreach ($this->getIterator() as $item) { $quantity = ($field === 'Quantity') ? 1 : $item->Quantity; if (!$onproduct) { $total += $item->$field * $quantity; } elseif ($item->hasMethod($field)) { $total += $item->$field() * $quantity; } elseif ($product = $item->Product()) { $total += $product->$field * $quantity; } } return $total; } /** * Add up the totals of all the order items in this list. */ public function SubTotal() { $result = 0; foreach ($this->getIterator() as $item) { $result += $item->Total(); } return $result; } } |