Example #1
0
 /**
  * Default Function
  *
  * @access public
  * @param string
  * @return void
  */
 public function index()
 {
     $products = $this->input->post('products');
     //if products is an array
     if ($products !== FALSE && is_array($products)) {
         foreach ($products as $products_id => $quantity) {
             //get variants from product id string
             $variants = parse_variants_from_id_string($products_id);
             //update shopping cart
             $this->shopping_cart->add($products_id, $variants, $quantity, 'update');
         }
     }
     redirect('checkout/shopping_cart');
 }
Example #2
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();
     }
 }