/** * Add product to shopping cart * * @param string $products_id_string * @param array $variants * @param int $quantity * @param stirng $action */ function add($products_id_string, $variants = NULL, $quantity = NULL, $action = 'add') { //load product object $product = load_product_library($products_id_string); if ($product->is_valid()) { //if product has variants and variants is not given if ($product->has_variants() && $variants == NULL) { $variant = $product->get_default_variant(); $variants = parse_variants_from_id_string($variant['product_id_string']); } //get products id string $products_id_string = get_product_id_string($products_id_string, $variants); //if product already exists in shopping cart if ($this->exists($products_id_string)) { $old_quantity = $this->get_quantity($products_id_string); //if quantity is not specified if (!is_numeric($quantity)) { $quantity = $this->get_quantity($products_id_string) + 1; } else { if (is_numeric($quantity) && $quantity == 0) { $this->remove($products_id_string); return; } else { if ($action == 'add') { $quantity = $this->get_quantity($products_id_string) + $quantity; } } } //check minimum order quantity $products_moq = $product->get_moq(); if ($quantity < $products_moq) { $quantity = $products_moq; $error = sprintf(lang('error_minimum_order_quantity'), $product->get_title(), $products_moq); } //check maximum order quantity $products_max_order_quantity = $product->get_max_order_quantity(); if ($products_max_order_quantity > 0) { if ($quantity > $products_max_order_quantity) { $quantity = $products_max_order_quantity; $error = sprintf(lang('error_maximum_order_quantity'), $product->get_title(), $products_max_order_quantity); } } //check order increment $increment = $product->get_order_increment(); if (($quantity - $products_moq) % $increment != 0) { $quantity = $products_moq + (floor(($quantity - $products_moq) / $increment) + 1) * $increment; $error = sprintf(lang('error_order_increment'), $product->get_title(), $increment); } //set error to session if (isset($error) && !empty($error)) { $this->contents[$products_id_string]['error'] = $error; } $price = $product->get_price($variants, $quantity); //specials $this->contents[$products_id_string]['quantity'] = $quantity; $this->contents[$products_id_string]['price'] = $price; $this->contents[$products_id_string]['final_price'] = $price; // update database if ($this->ci->customer->is_logged_on()) { $this->ci->shopping_cart_model->update_content($this->ci->customer->get_id(), $products_id_string, $quantity); } } else { if (!is_numeric($quantity)) { $quantity = 1; } //check minimum order quantity $products_moq = $product->get_moq(); if ($quantity < $products_moq) { $quantity = $products_moq; $error = sprintf(lang('error_minimum_order_quantity'), $product->get_title(), $products_moq); } //check order increment $increment = $product->get_order_increment(); if (($quantity - $products_moq) % $increment != 0) { $quantity = $products_moq + (floor(($quantity - $products_moq) / $increment) + 1) * $increment; $error = sprintf(lang('error_order_increment'), $product->get_title(), $increment); } $price = $product->get_price($variants, $quantity); $this->contents[$products_id_string] = array('id' => $products_id_string, 'name' => $product->get_title(), 'type' => $product->get_product_type(), 'keyword' => $product->get_keyword(), 'sku' => $product->get_sku($variants), 'image' => $product->get_image(), 'price' => $price, 'final_price' => $price, 'quantity' => $quantity, 'weight' => $product->get_weight($variants), 'tax_class_id' => $product->get_tax_class_id(), 'date_added' => get_date_short(get_date_now()), 'weight_class_id' => $product->get_weight_class()); //set in stock status $this->contents[$products_id_string]['in_stock'] = $this->is_in_stock($products_id_string); //set error to session if (isset($error) && !empty($error)) { $this->contents[$products_id_string]['error'] = $error; } // insert into database if ($this->ci->customer->is_logged_on()) { $this->ci->shopping_cart_model->insert_content($this->ci->customer->get_id(), $products_id_string, $quantity, $price); } if (is_array($variants) && !empty($variants)) { $variants_array = $product->get_variants(); $products_variants_id_string = get_product_id_string($products_id_string, $variants); $products_variants_id = $variants_array[$products_variants_id_string]['variants_id']; $this->contents[$products_id_string]['products_variants_id'] = $products_variants_id; if (isset($variants_array[$products_variants_id_string]['filename']) && !empty($variants_array[$products_variants_id_string]['filename'])) { $this->contents[$products_id_string]['variant_filename'] = $variants_array[$products_variants_id_string]['filename']; $this->contents[$products_id_string]['variant_cache_filename'] = $variants_array[$products_variants_id_string]['cache_filename']; } foreach ($variants as $group_id => $value_id) { $names = $this->ci->products_model->get_product_variant_group_and_value_name($product->get_id(), $group_id, $value_id); $this->contents[$products_id_string]['variants'][$group_id] = array('groups_id' => $group_id, 'variants_values_id' => $value_id, 'groups_name' => $names['products_variants_groups_name'], 'values_name' => $names['products_variants_values_name']); } } } $this->clean_up(); $this->calculate(); } }
/** * Get weight * * @access public * @param $variants * @return float */ public function get_weight($variants = null) { if ($variants == null || empty($variants)) { return $this->data['products_weight']; } else { $product_id_string = get_product_id_string($this->get_id(), $variants); return $this->data['variants'][$product_id_string]['weight']; } }
/** * Get product data * * @access public * @param $id * @return array */ public function get_data($id) { $data = FALSE; //get products & products description data $result = $this->db->select('p.products_id, p.products_model as model, p.products_sku as sku, p.products_type as type, p.products_quantity as quantity, p.products_max_order_quantity as max_order_quantity, p.products_moq as moq, p.order_increment as order_increment, p.products_price as price, p.products_tax_class_id as tax_class_id, p.products_date_added as date_added, p.products_date_available as date_available, p.manufacturers_id, p.quantity_discount_groups_id, p.quantity_unit_class, p.quantity_discount_groups_id as quantity_discount_groups_id, p.products_weight as products_weight, p.products_weight_class as products_weight_class, pd.products_name as name, pd.products_short_description as short_description, pd.products_description as description, pd.products_page_title as page_title, pd.products_meta_keywords as meta_keywords, pd.products_meta_description as meta_description, pd.products_keyword as keyword, pd.products_tags as tags, pd.products_url as url, quc.quantity_unit_class_title as unit_class, m.manufacturers_name, i.image as default_image, f.products_id as featured_products_id, s.specials_new_products_price as specials_price')->from('products as p')->join('products_description as pd', 'p.products_id = pd.products_id', 'inner')->join('manufacturers as m', 'p.manufacturers_id = m.manufacturers_id', 'left')->join('products_images as i', 'p.products_id = i.products_id', 'left')->join('products_frontpage as f', 'p.products_id = f.products_id', 'left')->join('quantity_unit_classes as quc', 'p.quantity_unit_class = quc.quantity_unit_class_id', 'left')->join('specials as s', 'p.products_id = s.products_id and s.status = 1 and s.start_date <= now() and s.expires_date >= now()', 'left')->where('p.products_id', $id)->where('p.products_status', '1')->where('i.default_flag', '1')->where('pd.language_id', lang_id())->where('quc.language_id = pd.language_id')->get(); if ($result->num_rows() > 0) { $data = $result->row_array(); $result->free_result(); //get products images data $result = $this->db->select('id, image, default_flag')->from('products_images')->where('products_id', $id)->order_by('sort_order')->get(); if ($result->num_rows() > 0) { $data['images'] = $result->result_array(); $result->free_result(); } //get products categories $result = $this->db->select('categories_id')->from('products_to_categories')->where('products_id', $id)->limit(1)->get(); if ($result->num_rows() > 0) { $row = $result->row_array(); $data['categories_id'] = $row['categories_id']; $result->free_result(); } //products attachments $result = $this->db->select('attachments_name, pa.attachments_id, filename, cache_filename, attachments_description')->from('products_attachments as pa')->join('products_attachments_description as pad', 'pa.attachments_id = pad.attachments_id', 'inner')->join('products_attachments_to_products as pa2p', 'pa2p.attachments_id = pa.attachments_id', 'inner')->where('pa2p.products_id', $id)->where('pad.languages_id', lang_id())->get(); if ($result->num_rows() > 0) { $data['attachments'] = $result->result_array(); $result->free_result(); } //products accessories $data['accessories'] = NULL; $result = $this->db->select('accessories_id')->from('products_accessories')->where('products_id', $id)->get(); if ($result->num_rows() > 0) { $data['accessories'] = $result->result_array(); $result->free_result(); } //products accessories $result = $this->db->select('pav.name, pav.module, pav.value as selections, pa.value')->from('products_attributes as pa')->join('products_attributes_values as pav', 'pa.products_attributes_values_id = pav.products_attributes_values_id', 'inner')->where('pa.language_id = pav.language_id')->where('pa.products_id', $id)->where('pa.language_id', lang_id())->get(); if ($result->num_rows() > 0) { $attributes = array(); foreach ($result->result_array() as $row) { if ($row['module'] == 'pull_down_menu') { $selections = $row['selections']; $selections = explode(',', $selections); if (isset($selections[$value - 1])) { $value = $selections[$value - 1]; } } $attributes[] = array('name' => $name, 'value' => $value); } $data['accessories'] = $attributes; $result->free_result(); } //products variants $products_variants = array(); $res_variants = $this->db->select('*')->from('products_variants')->where('products_id', $id)->order_by('is_default', 'desc')->get(); $groups = array(); $values = array(); $groups_values = array(); foreach ($res_variants->result_array() as $variant) { $res_values = $this->db->select('pve.products_variants_groups_id as groups_id, pve.products_variants_values_id as variants_values_id, pvg.products_variants_groups_name as groups_name, pvv.products_variants_values_name as variants_values_name')->from('products_variants_entries as pve')->join('products_variants_groups as pvg', 'pve.products_variants_groups_id = pvg.products_variants_groups_id', 'inner')->join('products_variants_values as pvv', 'pve.products_variants_values_id = pvv.products_variants_values_id', 'inner')->where('pvg.language_id = ' . lang_id())->where('pvv.language_id = ' . lang_id())->where('pve.products_variants_id = ' . $variant['products_variants_id'])->order_by('pve.products_variants_groups_id')->get(); $variants = array(); $groups_name = array(); foreach ($res_values->result_array() as $value) { $variants[$value['groups_id']] = $value['variants_values_id']; $groups_name[$value['groups_name']] = $value['variants_values_name']; $groups[$value['groups_id']] = $value['groups_name']; $values[$value['variants_values_id']] = $value['variants_values_name']; if (!isset($groups_values[$value['groups_id']]) || !is_array($groups_values[$value['groups_id']])) { $groups_values[$value['groups_id']] = array(); } if (!in_array($value['variants_values_id'], $groups_values[$value['groups_id']])) { $groups_values[$value['groups_id']][] = $value['variants_values_id']; } } $product_id_string = get_product_id_string($id, $variants); $products_variants[$product_id_string]['variants_id'] = $variant['products_variants_id']; $products_variants[$product_id_string]['is_default'] = $variant['is_default']; $products_variants[$product_id_string]['sku'] = $variant['products_sku']; $products_variants[$product_id_string]['price'] = $variant['products_price']; $products_variants[$product_id_string]['status'] = $variant['products_status']; $products_variants[$product_id_string]['weight'] = $variant['products_weight']; $products_variants[$product_id_string]['groups_id'] = $variants; $products_variants[$product_id_string]['groups_name'] = $groups_name; $products_variants[$product_id_string]['filename'] = $variant['filename']; $products_variants[$product_id_string]['cache_filename'] = $variant['cache_filename']; //get variant image through id foreach ($data['images'] as $image) { if ($image['id'] == $variant['products_images_id']) { $products_variants[$product_id_string]['image'] = $image['image']; } } if ($variant['is_default'] == 1) { $data['default_variant'] = $products_variants[$product_id_string]; $data['default_variant']['product_id_string'] = $product_id_string; } } $data['variants'] = $products_variants; $data['variants_groups'] = $groups; $data['variants_values'] = $values; $data['variants_groups_values'] = $groups_values; //average reviews rating $data['rating'] = $this->get_average_reviews_rating($data['products_id']); } return $data; }
private function _get_products() { $this->_ci->load->helper('core'); $order_products = $this->_ci->order_model->get_products($this->_order_id); if (!empty($order_products)) { foreach ($order_products as $order_product) { $product = array('id' => $order_product['products_id'], 'orders_products_id' => $order_product['orders_products_id'], 'type' => $order_product['products_type'], 'quantity' => $order_product['products_quantity'], 'return_quantity' => $order_product['products_return_quantity'], 'name' => $order_product['products_name'], 'sku' => $order_product['products_sku'], 'tax' => $order_product['products_tax'], 'tax_class_id' => $order_product['products_tax_class_id'], 'price' => $order_product['products_price'], 'final_price' => $order_product['final_price'], 'weight' => $order_product['products_weight'], 'tax_class_id' => $order_product['products_tax_class_id'], 'weight_class_id' => $order_product['products_weight_class']); if ($product['type'] == PRODUCT_TYPE_GIFT_CERTIFICATE) { $certificate = $this->_ci->order_model->get_certificate($this->_order_id, $product['orders_products_id']); if (!empty($certificate)) { $product['gift_certificates_type'] = $certificate['gift_certificates_type']; $product['gift_certificates_code'] = $certificate['gift_certificates_code']; $product['senders_name'] = $certificate['senders_name']; $product['senders_email'] = $certificate['senders_email']; $product['recipients_name'] = $certificate['recipients_name']; $product['recipients_email'] = $certificate['recipients_email']; $product['messages'] = $certificate['messages']; } } $variants_result = $this->_ci->order_model->get_variants($this->_order_id, $product['orders_products_id']); $variants = array(); if (!empty($variants_result)) { foreach ($variants_result as $variant) { $product['variants'][] = array('groups_id' => $variant['groups_id'], 'values_id' => $variant['values_id'], 'groups_name' => $variant['groups_name'], 'values_name' => $variant['values_name']); $variants[$variant['groups_id']] = $variant['values_id']; } } $customizations_result = $this->_ci->order_model->get_customizations($this->_order_id, $product['orders_products_id']); $customizations = null; if (!empty($customizations_result)) { foreach ($customizations_result as $customization) { $fields_result = $this->_ci->order_model->get_customization_fields($customization['orders_products_customizations_id']); $fields = array(); if (!empty($fields_result)) { foreach ($fields_result as $field) { $fields[$field['orders_products_customizations_values_id']] = array('customization_fields_id' => $field['customization_fields_id'], 'customization_fields_name' => $field['customization_fields_name'], 'customization_type' => $field['customization_fields_type'], 'customization_value' => $field['customization_fields_value'], 'cache_filename' => $field['cache_file_name']); } } $customizations[] = array('qty' => $customization['quantity'], 'fields' => $fields); } } if ($customizations != null) { $product['customizations'] = $customizations; } if ($product['type'] == PRODUCT_TYPE_GIFT_CERTIFICATE) { $products_id_string = $product['id'] . '#' . $product['orders_products_id']; } else { $products_id_string = get_product_id_string($product['id'], $variants); } $this->_contents[$products_id_string] = $product; } } }