public static function getItems() { $list = array(); $j2params = JComponentHelper::getParams('com_j2store'); if (J2StoreHelperCart::hasProducts()) { require_once JPATH_SITE . '/components/com_j2store/models/mycart.php'; $cart_model = new J2StoreModelMyCart(); $totals = $cart_model->getTotals(); $product_count = J2StoreHelperCart::countProducts(); if ($j2params->get('auto_calculate_tax', 1)) { $total = $totals['total']; } else { $total = $totals['total_without_tax']; } $list['total'] = $total; $list['product_count'] = $product_count; //$html = JText::sprintf('J2STORE_CART_TOTAL', $product_count, J2StorePrices::number($total)); } else { $list['total'] = 0; $list['product_count'] = 0; //$html = JText::_('J2STORE_NO_ITEMS_IN_CART'); } return $list; }
/** * * Returns an object with the total cost of shipping for this method and the array of geozones * * @param unknown_type $shipping_method_id * @param array $geozones * @param unknown_type $orderItems * @param unknown_type $order_id */ protected function getTotal($shipping_method_id, $geozones, $orderItems, $geozones_taxes) { $return = new JObject(); $return->shipping_rate_id = '0'; $return->shipping_rate_price = '0.00000'; $return->shipping_rate_handling = '0.00000'; $return->shipping_tax_rates = '0.00000'; $return->shipping_tax_total = '0.00000'; $rate_exists = false; $geozone_rates = array(); //include custom modals $this->includeCustomModel('ShippingMethods'); $this->includeCustomModel('ShippingRates'); // cast product_id as an array $orderItems = (array) $orderItems; // determine the shipping method type $this->includeCustomTables('shipping_standard'); $this->includeCustomTables(); $shippingmethod = JTable::getInstance('ShippingMethods', 'Table'); $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::_('J2STORE_UNDEFINED_SHIPPING_METHOD')); return $return; } //initiliase cart helper $carthelper = new J2StoreHelperCart(); //initliase cart model JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_j2store/models'); $cart_model = new J2StoreModelMyCart(); switch ($shippingmethod->shipping_method_type) { case "2": // 2 = per order - price based // Get the total of the order, and find the rate for that $total = 0; //foreach ($orderItems as $item) // { // $total += $item->orderitem_final_price; // } $order_ships = false; $products = $cart_model->getDataNew(); foreach ($products as $product) { //check if shipping is enabled for this item if (!empty($product['shipping'])) { $order_ships = true; $total += $product['total']; // product total } } if ($order_ships) { foreach ($geozones as $geozone) { unset($rate); $geozone_id = $geozone->geozone_id; if (empty($geozone_rates[$geozone_id]) || !is_array($geozone_rates[$geozone_id])) { $geozone_rates[$geozone_id] = array(); } // JModelLegacy::addIncludePath( JPATH_ADMINISTRATOR.'/components/com_j2store/models' ); $model = JModelLegacy::getInstance('ShippingRates', 'J2StoreModel'); $model->setState('filter_shippingmethod', $shipping_method_id); $model->setState('filter_geozone', $geozone_id); $model->setState('filter_weight', $total); // Use weight as total $items = $model->getList(); if (count($items) < 1) { //return JTable::getInstance('ShippingRates', 'Table'); } else { $rate = $items[0]; $geozone_rates[$geozone_id]['0'] = $rate; // if $rate->shipping_rate_id is empty, then no real rate was found if (!empty($rate->shipping_rate_id)) { $rate_exists = true; } $geozone_rates[$geozone_id]['0']->qty = '1'; $geozone_rates[$geozone_id]['0']->shipping_method_type = $shippingmethod->shipping_method_type; } } } break; case "1": // 1 = per order - quantity based // first, get the total quantity of shippable items for the entire order // then, figure out the rate for this number of items (use the weight range field) + geozone // 1 = per order - quantity based // first, get the total quantity of shippable items for the entire order // then, figure out the rate for this number of items (use the weight range field) + geozone case "0": // 0 = per order - flat rate // 0 = per order - flat rate case "5": // 5 = per order - weight based // if any of the products in the order require shipping $sum_weight = 0; $count_shipped_items = 0; $order_ships = false; $products = $cart_model->getDataNew(); foreach ($products as $product) { //check if shipping is enabled for this item if (!empty($product['shipping'])) { $product_id = $product['product_id']; $order_ships = true; $sum_weight += $product['weight_total']; // we already have a weight total. So we dont have to multiply weight*quantity again $count_shipped_items += $product['quantity']; } } if ($order_ships) { foreach ($geozones as $geozone) { unset($rate); $geozone_id = $geozone->geozone_id; if (empty($geozone_rates[$geozone_id]) || !is_array($geozone_rates[$geozone_id])) { $geozone_rates[$geozone_id] = array(); } switch ($shippingmethod->shipping_method_type) { case "0": // don't use weight, just do flat rate for entire order // regardless of weight and regardless of the number of items $rate = $this->getRate($shipping_method_id, $geozone_id, $product_id); break; case "1": // get the shipping rate for the entire order using the count of all products in the order that ship $rate = $this->getRate($shipping_method_id, $geozone_id, $product_id, '1', $count_shipped_items); break; default: // get the shipping rate for the entire order using the sum weight of all products in the order that ship $rate = $this->getRate($shipping_method_id, $geozone_id, $product_id, '1', $sum_weight); break; } $geozone_rates[$geozone_id]['0'] = $rate; // if $rate->shipping_rate_id is empty, then no real rate was found if (!empty($rate->shipping_rate_id)) { $rate_exists = true; } $geozone_rates[$geozone_id]['0']->qty = '1'; $geozone_rates[$geozone_id]['0']->shipping_method_type = $shippingmethod->shipping_method_type; } } break; case "6": case "4": case "3": // 6 = per item - price based, a percentage of the product's price // 4 = per item - weight based // 3 = per item - flat rate $rates = array(); $products = $cart_model->getDataNew(); foreach ($products as $product) { // print_r($item); $pid = $product['product_id']; $qty = $product['quantity']; $hash = $product['key']; foreach ($geozones as $geozone) { unset($rate); $geozone_id = $geozone->geozone_id; if (empty($geozone_rates[$geozone_id]) || !is_array($geozone_rates[$geozone_id])) { $geozone_rates[$geozone_id] = array(); } // $geozone_rates[$geozone_id][$pid] contains the shipping rate object for ONE product_id at this geozone. // You need to multiply by the quantity later $rate = $this->getRate($shipping_method_id, $geozone_id, $pid, $shippingmethod->shipping_method_type); //price per item if ($shippingmethod->shipping_method_type == '6') { // the rate is a percentage of the product's price $rate->shipping_rate_price = $rate->shipping_rate_price / 100 * $item->orderitem_final_price; $geozone_rates[$geozone_id][$hash] = $rate; $geozone_rates[$geozone_id][$hash]->shipping_method_type = $shippingmethod->shipping_method_type; $geozone_rates[$geozone_id][$hash]->qty = '1'; // If the method_type == 6, qty should be 1 (we don't need to multiply later, in the "calc for the entire method", since this is a percentage of the orderitem_final_price) //weight per item //if weight based per item, we need to use weight. //Per product weight (including the option weight) is already present in the products array. So pass it. } elseif ($shippingmethod->shipping_method_type == '4') { $rate = $this->getRate($shipping_method_id, $geozone_id, $pid, '1', $product['weight']); $geozone_rates[$geozone_id][$hash] = $rate; $geozone_rates[$geozone_id][$hash]->shipping_method_type = $shippingmethod->shipping_method_type; $geozone_rates[$geozone_id][$hash]->qty = $qty; } else { //obviously, this is flat rate per item $geozone_rates[$geozone_id][$hash] = $rate; $geozone_rates[$geozone_id][$hash]->shipping_method_type = $shippingmethod->shipping_method_type; $geozone_rates[$geozone_id][$hash]->qty = $qty; } // if $rate->shipping_rate_id is empty, then no real rate was found if (!empty($rate->shipping_rate_id)) { $rate_exists = true; } } } break; default: $this->setError(JText::_('J2STORE_INVALID_SHIPPING_METHOD_TYPE')); return false; break; } if (!$rate_exists) { $this->setError(JText::_('J2STORE_NO_RATE_FOUND')); return false; } $shipping_tax_rates = array(); $shipping_method_price = 0; $shipping_method_handling = 0; $shipping_method_tax_total = 0; $j2tax = new J2StoreTax(); // now calc tax for the entire method foreach ($geozone_rates as $geozone_id => $geozone_rate_array) { foreach ($geozone_rate_array as $geozone_rate) { if ($shippingmethod->tax_class_id) { $value = $geozone_rate->shipping_rate_price * $geozone_rate->qty + $geozone_rate->shipping_rate_handling; $tax_rates = $j2tax->getRates($shippingmethod->tax_class_id); $shipping_tax_rates[$geozone_id] = 0; foreach ($tax_rates as $tax_rate) { $shipping_tax_rates[$geozone_id] += $tax_rate['rate']; } $shipping_method_tax_total += $j2tax->getTax($value, $shippingmethod->tax_class_id); } $shipping_method_price += $geozone_rate->shipping_rate_price * $geozone_rate->qty; $shipping_method_handling += $geozone_rate->shipping_rate_handling; } } // return formatted object $return->shipping_rate_price = $shipping_method_price; $return->shipping_rate_handling = $shipping_method_handling; $return->shipping_tax_rates = $shipping_tax_rates; $return->shipping_tax_total = $shipping_method_tax_total; $return->shipping_method_id = $shipping_method_id; $return->shipping_method_name = $shippingmethod->shipping_method_name; // print_r($return); return $return; }
/** * Calculates the per_order coupon discount for the order * and the total post-tax/shipping discount * and sets order->order_discount * * @return unknown_type */ function calculateDiscountTotals() { $this->_taxes = J2StoreHelperCart::getTaxes(); $session = JFactory::getSession(); $tax = new J2StoreTax(); JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_j2store/models'); $model = JModelLegacy::getInstance('MyCart', 'J2StoreModel'); $products = $model->getDataNew(); if ($session->has('coupon', 'j2store')) { $coupon_info = J2StoreHelperCart::getCoupon($session->get('coupon', '', 'j2store')); if ($coupon_info) { $discount_total = 0; if (!$coupon_info->product) { $sub_total = J2StoreHelperCart::getSubTotal(); } else { $sub_total = 0; foreach ($products as $product) { if (in_array($product['product_id'], $coupon_info->product)) { $sub_total += $product['total']; } } } if ($coupon_info->value_type == 'F') { $coupon_info->value = min($coupon_info->value, $sub_total); } //maximum value restriction. If set then we need to check if ($coupon_info->value_type == 'P' && !empty($coupon_info->max_value) && (double) $coupon_info->max_value > 0) { //calculate the actual discount require_once JPATH_SITE . '/components/com_j2store/models/mycart.php'; $cart_model = new J2StoreModelMyCart(); $actual_discount = $cart_model->getDiscountTotal(); //is the actual discount greater than the max value if ($actual_discount > 0 && $actual_discount > (double) $coupon_info->max_value) { //set the coupon to be of fixed value $coupon_info->value = (double) $coupon_info->max_value; $coupon_info->value_type = 'F'; } } foreach ($products as $product) { $discount = 0; if (!$coupon_info->product) { $status = true; } else { if (in_array($product['product_id'], $coupon_info->product)) { $status = true; } else { $status = false; } } if ($status) { if ($coupon_info->value_type == 'F') { $discount = $coupon_info->value * ($product['total'] / $sub_total); } elseif ($coupon_info->value_type == 'P') { $discount = $product['total'] / 100 * $coupon_info->value; } if ($product['tax_profile_id']) { $tax_rates = $tax->getRateArray($product['total'] - ($product['total'] - $discount), $product['tax_profile_id']); foreach ($tax_rates as $tax_rate) { // if ($tax_rate['value_type'] == 'P') { $this->_taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount']; // } } } } $discount_total += $discount; } } } // store the total amount of the discount //set the total as equal to the order_subtotal + order_tax if its greater than the sum of the two $this->order_discount = $discount_total > $this->order_subtotal + $this->order_tax ? $this->order_subtotal + $this->order_tax : $discount_total; }
public static function isAllowed($item, $qty = 1, $attributes_csv = '') { $params = JComponentHelper::getParams('com_j2store'); //set the result object $result = new JObject(); $result->backorder = false; //we always want to allow users to buy. so initialise to 1. $result->can_allow = 1; //if basic version return true if (J2STORE_PRO != 1) { $result->can_allow = 1; return $result; } //first check if global inventory is enabled. if (!$params->get('enable_inventory', 0)) { //if not enabled, allow adding and return here $result->can_allow = 1; return $result; } //global inventory enabled. Now check at the product level. if (isset($item->product->stock) && is_object($item->product->stock)) { $stock = $item->product->stock; } elseif (isset($item->product->manage_stock) && is_object($item->product)) { $stock = $item->product; } else { $stock = J2StoreInventory::getStock($item->product_id); } if ((int) $stock->manage_stock < 1) { //Stock disabled. Return true $result->can_allow = 1; return $result; } if (empty($attributes_csv)) { $model = new J2StoreModelMyCart(); //get attributes $attributes = $model->getProductOptions($item->product_id); $list = array(); foreach ($attributes as $option) { if ($option['type'] == 'select' || $option['type'] == 'radio') { $option['product_option_id']; //get the default attributes foreach ($option['optionvalue'] as $optionvalue) { if ($optionvalue['product_optionvalue_default']) { //we have a default option $list[$option['product_option_id']] = $optionvalue['product_optionvalue_id']; } } if (empty($list[$option['product_option_id']])) { $list[$option['product_option_id']] = $option['optionvalue'][0]['product_optionvalue_id']; } } } sort($list); $attributes_csv = implode(',', $list); } $availableQuantity = J2StoreInventory::getAvailableQuantity($item->product_id, $attributes_csv); if ($availableQuantity->manage_stock && $qty > $availableQuantity->quantity) { $result->can_allow = 0; } $quantity_min = 1; if ($stock->min_out_qty) { $quantity_min = $stock->min_out_qty; } if ($quantity_min >= $availableQuantity->quantity) { $result->can_allow = 0; } if ($availableQuantity->quantity <= 0) { $result->can_allow = 0; } //TODO:: is there a better way of doing this. We are checking the product's total stock if ($item->product_stock > 0) { $result->can_allow = 1; } else { $result->can_allow = 0; } //if backorder is allowed, set it and override to allow adding if ($params->get('enable_inventory', 0) && $params->get('allow_backorder', 0)) { $result->backorder = true; } return $result; }