예제 #1
0
 /**
  * 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();
     }
 }
예제 #2
0
 /**
  * Synchronize with database
  * 
  * @access public
  * @return void
  */
 public function synchronize_with_database()
 {
     if (!$this->ci->customer->is_logged_on()) {
         return;
     }
     $this->customers_id = $this->ci->customer->get_id();
     //get wishlist
     $data = $this->ci->wishlist_model->get_wishlist_by_customers_id($this->customers_id);
     if ($data !== NULL) {
         $products = $this->ci->wishlist_model->get_wishlist_products($this->wishlists_id);
         if (is_array($products) && !empty($products)) {
             //delete temp wishlist
             $temp_wishlists_id = $this->wishlists_id;
             $contents = $this->contents;
             //reset wishlist
             $this->reset();
             //assign wishlists data
             $this->wishlists_id = $data['wishlists_id'];
             $this->token = $data['wishlists_token'];
             //assign products
             foreach ($products as $products_id) {
                 $product = load_product_library($products_id);
                 if ($product->is_valid()) {
                     $this->contents[$products_id] = array('products_id' => $products_id, 'name' => $product->get_title(), 'image' => $product->get_image(), 'price' => $product->get_price(), 'date_added' => get_date_short(get_date_now()), 'variants' => array(), 'comments' => '');
                 } else {
                     //remove invalid products id from database
                     $this->ci->wishlist_model->delete_product($this->wishlists_id, $products_id);
                 }
             }
             //merge current wishlists
             foreach ($contents as $products_id => $content) {
                 $this->add_product($products_id);
             }
             //remove temp wishlists
             $this->ci->wishlist_model->delete_product($temp_wishlists_id);
             //remove temp wishlists
             $this->ci->wishlist_model->delete_wishlist($temp_wishlists_id);
             //save session
             $this->save_session();
         }
     } else {
         if ($this->has_wishlist_id()) {
             //regenerate token
             $token = $this->generate_token();
             //update model
             $this->ci->wishlist_model->update_wishlist($this->wishlists_id, $this->customers_id, $token);
             //save session
             $this->save_session();
         }
     }
 }
예제 #3
0
 function get_date_long($date = null, $with_time = false)
 {
     $CI =& get_instance();
     if (empty($date)) {
         $date = get_date_now();
     }
     $year = substr($date, 0, 4);
     $month = (int) substr($date, 5, 2);
     $day = (int) substr($date, 8, 2);
     $hour = (int) substr($date, 11, 2);
     $minute = (int) substr($date, 14, 2);
     $second = (int) substr($date, 17, 2);
     if (@date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year) {
         return strftime($CI->lang->get_date_format_long(), mktime($hour, $minute, $second, $month, $day, $year));
     } else {
         return preg_replace('/2037/', $year, strftime($CI->lang->get_date_format_long(), mktime($hour, $minute, $second, $month, $day, 2037)));
     }
 }