/** * Saves each individual item in the order to the DB * * return unknown_type */ function saveOrderItems() { JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables'); $order = $this->_order; $items = $order->getItems(); if (empty($items) || !is_array($items)) { $this->setError("saveOrderItems:: " . JText::_('COM_CITRUSCART_ITEMS_ARRAY_INVALID')); return false; } $error = false; $errorMsg = ""; Citruscart::load('CitruscartHelperUser', 'helpers.user'); foreach ($items as $item) { $item->order_id = $order->order_id; if (!$item->save()) { // track error $error = true; $errorMsg .= $item->getError(); } else { //fire onAfterSaveOrderItem $dispatcher = JDispatcher::getInstance(); JFactory::getApplication()->triggerEvent('onAfterSaveOrderItem', array($item)); // does the orderitem create a subscription? if (!empty($item->orderitem_subscription)) { $date = JFactory::getDate(); // these are only for one-time payments that create subscriptions // recurring payment subscriptions are handled differently - by the payment plugins $subscription = JTable::getInstance('Subscriptions', 'CitruscartTable'); $subscription->user_id = $order->user_id; $subscription->order_id = $order->order_id; $subscription->product_id = $item->product_id; $subscription->orderitem_id = $item->orderitem_id; $subscription->transaction_id = ''; // in recurring payments, this is the subscr_id $subscription->created_datetime = $date->toSql(); $subscription->subscription_enabled = '0'; // disabled at first, enabled after payment clears switch ($item->subscription_period_unit) { case "Y": $period_unit = "YEAR"; break; case "M": $period_unit = "MONTH"; break; case "W": $period_unit = "WEEK"; break; case "I": // expiration date is not important (it's calculated on-the-fly) => create a seemingly lifetime subscription $period_unit = 'YEAR'; $item->subscription_period_interval = '100'; // we dont need to know the interval (we will know the last ID) break; case "D": default: $period_unit = "DAY"; break; } if (!empty($item->subscription_lifetime)) { // set expiration 100 years in future $period_unit = "YEAR"; $item->subscription_period_interval = '100'; $subscription->lifetime_enabled = '1'; } $database = JFactory::getDBO(); $query = " SELECT DATE_ADD('{$subscription->created_datetime}', INTERVAL {$item->subscription_period_interval} {$period_unit} ) "; $database->setQuery($query); $subscription->expires_datetime = $database->loadResult(); if ($this->defines->get('display_subnum', 0)) { $subscription->sub_number = CitruscartHelperUser::getSubNumber($order->user_id); } if (!$subscription->save()) { $error = true; $errorMsg .= $subscription->getError(); } // add a sub history entry, email the user? $subscriptionhistory = JTable::getInstance('SubscriptionHistory', 'CitruscartTable'); $subscriptionhistory->subscription_id = $subscription->subscription_id; $subscriptionhistory->subscriptionhistory_type = 'creation'; $subscriptionhistory->created_datetime = $date->toSql(); $subscriptionhistory->notify_customer = '0'; // notify customer of new trial subscription? $subscriptionhistory->comments = JText::_('COM_CITRUSCART_NEW_SUBSCRIPTION_CREATED'); $subscriptionhistory->save(); } // Save the attributes also if (!empty($item->orderitem_attributes)) { $attributes = explode(',', $item->orderitem_attributes); foreach ($attributes as $attribute) { unset($productattribute); unset($orderitemattribute); $productattribute = JTable::getInstance('ProductAttributeOptions', 'CitruscartTable'); $productattribute->load($attribute); $orderitemattribute = JTable::getInstance('OrderItemAttributes', 'CitruscartTable'); $orderitemattribute->orderitem_id = $item->orderitem_id; $orderitemattribute->productattributeoption_id = $productattribute->productattributeoption_id; $orderitemattribute->orderitemattribute_name = $productattribute->productattributeoption_name; $orderitemattribute->orderitemattribute_price = $productattribute->productattributeoption_price; $orderitemattribute->orderitemattribute_code = $productattribute->productattributeoption_code; $orderitemattribute->orderitemattribute_prefix = $productattribute->productattributeoption_prefix; $orderitemattribute->orderitemattribute_weight = $productattribute->productattributeoption_weight; $orderitemattribute->orderitemattribute_prefix_weight = $productattribute->productattributeoption_prefix_weight; if (!$orderitemattribute->save()) { // track error $error = true; $errorMsg .= $orderitemattribute->getError(); } } } } } if ($error) { $this->setError($errorMsg); return false; } return true; }