Exemple #1
0
 public function index($slug)
 {
     $product = \CI::Products()->slug($slug);
     if (!$product) {
         throw_404();
     } else {
         $product->images = json_decode($product->images, true);
         if ($product->images) {
             $product->images = array_values($product->images);
         } else {
             $product->images = [];
         }
         //set product options
         $data['options'] = \CI::ProductOptions()->getProductOptions($product->id);
         $data['posted_options'] = \CI::session()->flashdata('option_values');
         //get related items
         $data['related'] = $product->related_products;
         //create view variable
         $data['page_title'] = $product->name;
         $data['meta'] = $product->meta;
         $data['seo_title'] = !empty($product->seo_title) ? $product->seo_title : $product->name;
         $data['product'] = $product;
         //load the view
         $this->view('product', $data);
     }
 }
Exemple #2
0
 public function index($slug)
 {
     $product = \CI::Products()->slug($slug);
     //echo '<pre>'; print_r($product);exit;
     if (!$product) {
         throw_404();
     } else {
         $product->images = json_decode($product->images, true);
         if ($product->images) {
             $product->images = array_values($product->images);
         } else {
             $product->images = [];
         }
         // get documents
         $data['documents'] = \CI::Products()->get_documents($product->id);
         //echo \CI::db()->last_query(); print_r($data['documents']);exit;
         //echo $product->manufacturers;exit;
         // get parameter of product
         $data['get_parameters_of_product'] = \CI::Products()->get_parameters_of_product($product->id, $product->primary_category, $product->manufacturers);
         //echo '<pre>'; print_r($data['documents']);exit;
         //set product options
         $data['options'] = \CI::ProductOptions()->getProductOptions($product->id);
         $data['posted_options'] = \CI::session()->flashdata('option_values');
         //get related items
         $data['related'] = $product->related_products;
         //create view variable
         $data['page_title'] = $product->name;
         $data['meta'] = $product->meta;
         $data['seo_title'] = !empty($product->seo_title) ? $product->seo_title : $product->name;
         $data['product'] = $product;
         //load the view
         $this->view('product', $data);
     }
 }
Exemple #3
0
 public function insertItem($data = [])
 {
     $product = false;
     $quantity = 1;
     $postedOptions = false;
     $downloads = false;
     $combine = false;
     //is this an item from a separate cart being combined?
     extract($data);
     if (is_int($product)) {
         $product = \CI::Products()->getProduct($product);
         if (!$product) {
             return json_encode(['error' => lang('error_product_not_found')]);
         }
         //Clean up the product for the orderItems database
         $product = $this->cleanProduct($product);
         //get downloadable files
         $downloads = \CI::DigitalProducts()->getAssociationsByProduct($product->product_id);
     }
     $update = false;
     if (empty($product->hash)) {
         $product->hash = md5(json_encode($product) . json_encode($postedOptions));
         //set defaults for new items
         $product->coupon_discount = 0;
         $product->coupon_discount_quantity = 0;
         $product->coupon_code = '';
     } else {
         if (!$combine) {
             //this is an update
             $update = true;
         }
     }
     $product->order_id = $this->cart->id;
     //loop through the products in the cart and make sure we don't have this in there already. If we do get those quantities as well
     $qty_count = $quantity;
     $this->getCartItems();
     // refresh the cart items
     foreach ($this->items as $item) {
         if (intval($item->product_id) == intval($product->product_id)) {
             if ($item->hash != $product->hash) {
                 $qty_count = $qty_count + $item->quantity;
             }
         }
         if ($item->hash == $product->hash && !$update) {
             //if the item is already in the cart, send back a message
             return json_encode(['message' => lang('item_already_added')]);
         }
     }
     if (!config_item('allow_os_purchase') && (bool) $product->track_stock) {
         $stock = \CI::Products()->getProduct($product->product_id);
         if ($stock->quantity < $qty_count) {
             return json_encode(['error' => sprintf(lang('not_enough_stock'), $stock->name, $stock->quantity)]);
         }
     }
     if (!$quantity || $quantity <= 0 || $product->fixed_quantity == 1) {
         $product->quantity = 1;
     } else {
         $product->quantity = $quantity;
     }
     //create save options array here for use later.
     $saveOptions = [];
     if (!$update && $product->product_id) {
         //set the base "total_price"
         if ($product->saleprice > 0) {
             $product->total_price = $product->saleprice;
         } else {
             $product->total_price = $product->price;
         }
         //set base "total_weight"
         $product->total_weight = $product->weight;
         $productOptions = \CI::ProductOptions()->getProductOptions($product->product_id);
         //option error vars
         $optionError = false;
         $optionErrorMessage = lang('option_error') . '<br/>';
         //lets validate the options
         foreach ($productOptions as $productOption) {
             // are we missing any required values?
             $optionValue = false;
             if (!empty($postedOptions[$productOption->id])) {
                 $optionValue = $postedOptions[$productOption->id];
             }
             if ((int) $productOption->required && !$optionValue) {
                 $optionError = true;
                 $optionErrorMessage .= "- " . $productOption->name . '<br/>';
                 continue;
                 // don't bother processing this particular option any further
             }
             //create options to save to the database in case we get past the errors
             if ($productOption->type == 'checklist') {
                 if (is_array($optionValue)) {
                     foreach ($optionValue as $ov) {
                         foreach ($productOption->values as $productOptionValue) {
                             if ($productOptionValue->id == $ov) {
                                 $saveOptions[] = ['option_name' => $productOption->name, 'value' => $productOptionValue->value, 'price' => $productOptionValue->price, 'weight' => $productOptionValue->weight];
                                 $product->total_weight += $productOptionValue->weight;
                                 $product->total_price += $productOptionValue->price;
                             }
                         }
                     }
                 }
             } else {
                 $saveOption = [];
                 if ($productOption->type == 'textfield' || $productOption->type == 'textarea') {
                     $saveOption['value'] = $optionValue;
                     $productOptionValue = $productOption->values[0];
                 } else {
                     foreach ($productOption->values as $ov) {
                         if ($ov->id == $optionValue) {
                             $productOptionValue = $ov;
                             break;
                         }
                     }
                     $saveOption['value'] = $optionValue;
                 }
                 if (isset($productOptionValue)) {
                     $saveOption['option_name'] = $productOption->name;
                     $saveOption['price'] = $productOptionValue->price;
                     $saveOption['weight'] = $productOptionValue->weight;
                     //add it to the array;
                     $saveOptions[] = $saveOption;
                     //update the total weight and price
                     $product->total_weight += $productOptionValue->weight;
                     $product->total_price += $productOptionValue->price;
                 }
             }
         }
         if ($optionError) {
             return json_encode(['error' => $optionErrorMessage]);
         }
     }
     //save the product
     $product_id = \CI::Orders()->saveItem((array) $product);
     //save the options if we have them
     foreach ($saveOptions as $saveOption) {
         $saveOption['order_item_id'] = $product_id;
         $saveOption['order_id'] = $this->cart->id;
         \CI::Orders()->saveItemOption($saveOption);
     }
     if ($update) {
         foreach ($this->items as $key => $item) {
             if ($item->id == $product_id) {
                 $this->items[$key] = $product;
             }
         }
     } else {
         $product->id = $product_id;
         $this->items[] = $product;
         //update file downloads
         if ($downloads) {
             foreach ($downloads as $file) {
                 \CI::Orders()->saveOrderItemFile(['order_id' => $this->cart->id, 'order_item_id' => $product->id, 'file_id' => $file->file_id]);
             }
         }
     }
     //get current item count
     $itemCount = $this->totalItems();
     if ($update) {
         return json_encode(['message' => lang('cart_updated'), 'itemCount' => $itemCount]);
     } else {
         return json_encode(['message' => lang('item_added_to_cart'), 'itemCount' => $itemCount]);
     }
 }
Exemple #4
0
 public function giftCardForm($id = false, $duplicate = false)
 {
     $this->product_id = $id;
     \CI::load()->library('form_validation');
     \CI::load()->model(array('ProductOptions', 'Categories'));
     \CI::form_validation()->set_error_delimiters('<div class="error">', '</div>');
     $data['categories'] = \CI::Categories()->get_categories_tiered();
     $data['page_title'] = lang('giftcard_product_form');
     $data['groups'] = \CI::Customers()->get_groups();
     //default values are empty if the product is new
     $data['id'] = '';
     $data['sku'] = '';
     $data['primary_category'] = '';
     $data['name'] = '';
     $data['slug'] = '';
     $data['description'] = '';
     $data['excerpt'] = '';
     $data['track_stock'] = '';
     $data['seo_title'] = '';
     $data['meta'] = '';
     $data['is_giftcard'] = 1;
     $data['taxable'] = '';
     $data['images'] = [];
     $data['product_categories'] = [];
     $data['product_files'] = [];
     foreach ($data['groups'] as $group) {
         $data['enabled_' . $group->id] = '';
     }
     //create the photos array for later use
     $data['photos'] = [];
     if ($id) {
         // get product & options data
         $data['ProductOptions'] = \CI::ProductOptions()->getProductOptions($id);
         $product = \CI::Products()->find($id, true);
         //if the product does not exist, redirect them to the product list with an error
         if (!$product) {
             \CI::session()->set_flashdata('error', lang('error_not_found'));
             redirect('admin/products');
         }
         //helps us with the slug generation
         $this->product_name = \CI::input()->post('slug', $product->slug);
         //set values to db values
         $data['id'] = $id;
         $data['sku'] = $product->sku;
         $data['primary_category'] = $product->primary_category;
         $data['name'] = $product->name;
         $data['seo_title'] = $product->seo_title;
         $data['meta'] = $product->meta;
         $data['slug'] = $product->slug;
         $data['description'] = $product->description;
         $data['excerpt'] = $product->excerpt;
         $data['quantity'] = $product->quantity;
         $data['taxable'] = $product->taxable;
         $data['fixed_quantity'] = $product->fixed_quantity;
         $data['is_giftcard'] = $product->is_giftcard;
         foreach ($data['groups'] as $group) {
             $data['enabled_' . $group->id] = $product->{'enabled_' . $group->id};
         }
         //make sure we haven't submitted the form yet before we pull in the images/related products from the database
         if (!\CI::input()->post('submit')) {
             $data['product_categories'] = [];
             foreach ($product->categories as $product_category) {
                 $data['product_categories'][] = $product_category->id;
             }
             $data['related_products'] = $product->related_products;
             $data['images'] = (array) json_decode($product->images);
         }
     }
     if (!is_array($data['product_categories'])) {
         $data['product_categories'] = [];
     }
     //no error checking on these
     \CI::form_validation()->set_rules('caption', 'Caption');
     \CI::form_validation()->set_rules('primary_photo', 'Primary');
     \CI::form_validation()->set_rules('sku', 'lang:sku', 'trim');
     \CI::form_validation()->set_rules('seo_title', 'lang:seo_title', 'trim');
     \CI::form_validation()->set_rules('meta', 'lang:meta_data', 'trim');
     \CI::form_validation()->set_rules('name', 'lang:name', 'trim|required|max_length[64]');
     \CI::form_validation()->set_rules('slug', 'lang:slug', 'trim');
     \CI::form_validation()->set_rules('description', 'lang:description', 'trim');
     \CI::form_validation()->set_rules('excerpt', 'lang:excerpt', 'trim');
     \CI::form_validation()->set_rules('taxable', 'lang:taxable', 'trim|numeric');
     \CI::form_validation()->set_rules('fixed_quantity', 'lang:fixed_quantity', 'trim|numeric');
     \CI::form_validation()->set_rules('option[giftcard_values]', 'lang:giftcard_values', 'required');
     foreach ($data['groups'] as $group) {
         \CI::form_validation()->set_rules('enabled_' . $group->id, lang('enabled') . '(' . $group->name . ')', 'trim|numeric');
     }
     /*
     if we've posted already, get the photo stuff and organize it
     if validation comes back negative, we feed this info back into the system
     if it comes back good, then we send it with the save item
     
     submit button has a value, so we can see when it's posted
     */
     if ($duplicate) {
         $data['id'] = false;
     }
     if (\CI::input()->post('submit')) {
         //reset the product options that were submitted in the post
         $data['ProductOptions'] = \CI::input()->post('option');
         $data['product_categories'] = \CI::input()->post('categories');
         $data['images'] = \CI::input()->post('images');
     }
     if (\CI::form_validation()->run() == FALSE) {
         $this->view('giftcard_product_form', $data);
     } else {
         \CI::load()->helper('text');
         //first check the slug field
         $slug = \CI::input()->post('slug');
         //if it's empty assign the name field
         if (empty($slug) || $slug == '') {
             $slug = \CI::input()->post('name');
         }
         $slug = url_title(convert_accented_characters($slug), '-', TRUE);
         //validate the slug
         $slug = $id ? \CI::Products()->validate_slug($slug, $product->id) : \CI::Products()->validate_slug($slug);
         $save['id'] = $id;
         $save['sku'] = \CI::input()->post('sku');
         $save['name'] = \CI::input()->post('name');
         $save['seo_title'] = \CI::input()->post('seo_title');
         $save['meta'] = \CI::input()->post('meta');
         $save['description'] = \CI::input()->post('description');
         $save['excerpt'] = \CI::input()->post('excerpt');
         foreach ($data['groups'] as $group) {
             $save['enabled_' . $group->id] = \CI::input()->post('enabled_' . $group->id);
             $save['price_' . $group->id] = '0.00';
             $save['saleprice_' . $group->id] = '0.00';
         }
         $save['is_giftcard'] = 1;
         $save['taxable'] = \CI::input()->post('taxable');
         $save['taxable'] = \CI::input()->post('taxable');
         $post_images = \CI::input()->post('images');
         $save['slug'] = $slug;
         if ($primary = \CI::input()->post('primary_image')) {
             if ($post_images) {
                 foreach ($post_images as $key => &$pi) {
                     if ($primary == $key) {
                         $pi['primary'] = true;
                         continue;
                     }
                 }
             }
         }
         $save['images'] = json_encode($post_images);
         if (\CI::input()->post('related_products')) {
             $save['related_products'] = json_encode(\CI::input()->post('related_products'));
         } else {
             $save['related_products'] = '';
         }
         //save categories
         $categories = \CI::input()->post('categories');
         if (!$categories) {
             $categories = [];
         }
         //(\CI::input()->post('primary_category')) ? \CI::input()->post('primary_category') : 0;
         if (!\CI::input()->post('primary_category') && $categories) {
             $save['primary_category'] = $categories[0];
         } elseif (!\CI::input()->post('primary_category') && !$categories) {
             $save['primary_category'] = 0;
         } else {
             $save['primary_category'] = \CI::input()->post('primary_category');
         }
         // format options
         $options = [];
         array_push($options, ['type' => 'textfield', 'name' => 'from', 'required' => 1, 'values' => ['0' => ['name' => 'from', 'value' => '', 'price' => 0, 'limit' => 0]]]);
         array_push($options, ['type' => 'textfield', 'name' => 'to_email', 'required' => 1, 'values' => ['0' => ['name' => 'to_email', 'value' => '', 'price' => 0, 'limit' => 0]]]);
         array_push($options, ['type' => 'textarea', 'name' => 'personal_message', 'required' => 1, 'values' => ['0' => ['name' => 'personal_message', 'value' => '', 'price' => 0]]]);
         $giftcard_values = [];
         $postedValues = \CI::input()->post('option[giftcard_values]');
         if ($postedValues) {
             foreach ($postedValues as $giftcard_value) {
                 array_push($giftcard_values, ['name' => 'beginning_amount', 'value' => $giftcard_value, 'weight' => '', 'price' => $giftcard_value]);
             }
         }
         array_push($options, ['type' => 'droplist', 'name' => 'beginning_amount', 'required' => 1, 'values' => $giftcard_values]);
         // save product
         $product_id = \CI::Products()->save($save, $options, $categories);
         \CI::session()->set_flashdata('message', lang('message_saved_giftcard_product'));
         //go back to the product list
         redirect('admin/products');
     }
 }
Exemple #5
0
 public function save($product, $options = false, $categories = false)
 {
     if ($product['id']) {
         CI::db()->where('id', $product['id']);
         CI::db()->update('products', $product);
         $id = $product['id'];
     } else {
         CI::db()->insert('products', $product);
         $id = CI::db()->insert_id();
     }
     //loop through the product options and add them to the db
     if ($options !== false) {
         // wipe the slate
         CI::ProductOptions()->clearOptions($id);
         // save edited values
         $count = 1;
         foreach ($options as $option) {
             $values = $option['values'];
             unset($option['values']);
             $option['product_id'] = $id;
             $option['sequence'] = $count;
             CI::ProductOptions()->saveOption($option, $values);
             $count++;
         }
     }
     if ($categories !== false) {
         if ($product['id']) {
             //get all the categories that the product is in
             $cats = $this->getProductCategories($id);
             //generate cat_id array
             $ids = [];
             foreach ($cats as $c) {
                 $ids[] = $c->id;
             }
             //eliminate categories that products are no longer in
             foreach ($ids as $c) {
                 if (!in_array($c, $categories)) {
                     CI::db()->delete('category_products', array('product_id' => $id, 'category_id' => $c));
                 }
             }
             //add products to new categories
             foreach ($categories as $c) {
                 if (!in_array($c, $ids)) {
                     CI::db()->insert('category_products', array('product_id' => $id, 'category_id' => $c));
                 }
             }
         } else {
             //new product add them all
             foreach ($categories as $c) {
                 CI::db()->insert('category_products', array('product_id' => $id, 'category_id' => $c));
             }
         }
     }
     //return the product id
     return $id;
 }