/** * Returns a shipping estimate, unformatted. * * @param int $shipping_method_id * @param int $geozone_id * @param array $orderItems an array of TiendaTableOrderItems objects, each with ->product_id and ->orderitem_quantity * * @return object with ->shipping_rate_price and ->shipping_rate_handling and ->shipping_tax_total, all decimal(12,5) */ public function getTotal($shipping_method_id, $geozone_id, $orderItems) { $return = new JObject(); $return->shipping_rate_price = '0.00000'; $return->shipping_rate_handling = '0.00000'; $return->shipping_tax_rate = '0.00000'; $return->shipping_tax_total = '0.00000'; // cast product_id as an array $orderItems = (array) $orderItems; // determine the shipping method type JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $shippingmethod = JTable::getInstance('ShippingMethods', 'TiendaTable'); $shippingmethod->load($shipping_method_id); if (empty($shippingmethod->shipping_method_id)) { // TODO if this is an object, setError, otherwise return false, or 0.000? $return->setError(JText::_('COM_TIENDA_UNDEFINED_SHIPPING_METHOD')); return $return; } switch ($shippingmethod->shipping_method_type) { case "2": // 2 = per order // if any of the products in the order require shipping $order_ships = false; JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); foreach ($orderItems as $item) { //$pid = $orderItems[$i]->product_id; $pid = $item->product_id; $product = JTable::getInstance('Products', 'TiendaTable'); $product->load($pid); if (!empty($product->product_ships)) { $product_id = $item->product_id; $order_ships = true; } } if ($order_ships) { //$shippingrate = TiendaHelperShipping::getRate( $shipping_method_id, $geozone_id, $product_id ); //$return->shipping_rate_price = $shippingrate->shipping_rate_price; //$return->shipping_rate_handling = $shippingrate->shipping_rate_handling; } break; case "1": case "0": // 0 = per item // 1 = weight based $rates = array(); foreach ($orderItems as $item) { $pid = $item->product_id; $qty = $item->orderitem_quantity; //$rates[$pid] = TiendaHelperShipping::getRate( $shipping_method_id, $geozone_id, $pid, $shippingmethod->shipping_method_type ); //$return->shipping_rate_price += ($rates[$pid]->shipping_rate_price * $qty); //$return->shipping_rate_handling += ($rates[$pid]->shipping_rate_handling * $qty); } break; default: // TODO if this is an object, setError, otherwise return false, or 0.000? $return->setError(JText::_('COM_TIENDA_INVALID_SHIPPING_METHOD_TYPE')); return $return; break; } // get the shipping tax rate and total $return->shipping_tax_rate = TiendaHelperShipping::getTaxRate($shipping_method_id, $geozone_id); $return->shipping_tax_total = $return->shipping_tax_rate / 100 * ($return->shipping_rate_price + $return->shipping_rate_handling); return $return; }