예제 #1
0
 public function add($product_id, $quantity = null)
 {
     global $lC_Database, $lC_Services, $lC_Language, $lC_Customer, $lC_Product;
     if (!is_numeric($product_id)) {
         return false;
     }
     $Qproduct = $lC_Database->query('select p.*, i.image from :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag) where p.products_id = :products_id');
     $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
     $Qproduct->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
     $Qproduct->bindInt(':default_flag', 1);
     $Qproduct->bindInt(':products_id', $product_id);
     $Qproduct->execute();
     if ($Qproduct->value('image') == null) {
         // check for parent image
         $Qimage = $lC_Database->query('select image from :table_products_images where products_id = :parent_id');
         $Qimage->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
         $Qimage->bindInt(':default_flag', 1);
         $Qimage->bindInt(':parent_id', $Qproduct->valueInt('parent_id'));
         $Qimage->execute();
         $image = $Qimage->value('image');
     } else {
         $image = $Qproduct->value('image');
     }
     if ($Qproduct->valueInt('products_status') === 1) {
         if ($this->exists($product_id)) {
             $item_id = $this->getBasketID($product_id);
             if (is_numeric($quantity)) {
                 $quantity = $this->getQuantity($item_id) + 1;
             }
             $this->_contents[$item_id]['quantity'] = $quantity;
             if ($lC_Customer->isLoggedOn()) {
                 $Qupdate = $lC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id');
                 $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
                 $Qupdate->bindInt(':quantity', $quantity);
                 $Qupdate->bindInt(':customers_id', $lC_Customer->getID());
                 $Qupdate->bindInt(':item_id', $item_id);
                 $Qupdate->execute();
             }
         } else {
             if (!is_numeric($quantity)) {
                 $quantity = 1;
             }
             $Qdescription = $lC_Database->query('select products_name, products_keyword, products_description, products_tags, products_url from :table_products_description where products_id = :products_id and language_id = :language_id');
             $Qdescription->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
             $Qdescription->bindInt(':products_id', $product_id);
             $Qdescription->bindInt(':language_id', $lC_Language->getID());
             $Qdescription->execute();
             $desc = $Qdescription->toArray();
             if ($Qproduct->valueInt('parent_id') > 0) {
                 $Qmaster = $lC_Database->query('select products_name as parent_name, products_description as description, products_keyword as keyword, products_tags as tags, products_url as url from :table_products_description where products_id = :products_id and language_id = :language_id limit 1');
                 $Qmaster->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
                 $Qmaster->bindInt(':products_id', $Qproduct->valueInt('parent_id'));
                 $Qmaster->bindInt(':language_id', $lC_Language->getID());
                 $Qmaster->execute();
                 if ($Qproduct->valueInt('is_subproduct') > 0) {
                     $desc['products_name'] = $Qmaster->value('parent_name') . ' - ' . $desc['products_name'];
                 } else {
                     $desc['products_name'] = $Qmaster->value('parent_name');
                 }
                 $desc['products_description'] = $Qmaster->value('description');
                 $desc['products_keyword'] = $Qmaster->value('keyword');
                 $desc['products_tags'] = $Qmaster->value('tags');
                 $desc['products_url'] = $Qmaster->value('url');
             }
             // we get the product price from the product class - price already includes options, etc.
             if (!isset($lC_Product)) {
                 $lC_Product = new lC_Product($product_id);
             }
             $price = $lC_Product->getPrice($product_id, $lC_Customer->getCustomerGroup(), $_POST);
             if ($lC_Customer->isLoggedOn()) {
                 $Qid = $lC_Database->query('select max(item_id) as item_id from :table_shopping_carts where customers_id = :customers_id');
                 $Qid->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
                 $Qid->bindInt(':customers_id', $lC_Customer->getID());
                 $Qid->execute();
                 $item_id = $Qid->valueInt('item_id') + 1;
             } else {
                 if (empty($this->_contents)) {
                     $item_id = 1;
                 } else {
                     $item_id = max(array_keys($this->_contents)) + 1;
                 }
             }
             $this->_contents[$item_id] = array('item_id' => $item_id, 'id' => $product_id, 'parent_id' => $Qproduct->valueInt('parent_id'), 'name' => $desc['products_name'], 'model' => $Qproduct->value('products_model'), 'sku' => $Qproduct->value('products_sku'), 'keyword' => $desc['products_keyword'], 'tags' => $desc['products_tags'], 'url' => $desc['products_url'], 'description' => $desc['products_description'], 'image' => $image, 'price' => $price, 'quantity' => $quantity, 'weight' => $Qproduct->value('products_weight'), 'tax_class_id' => $Qproduct->valueInt('products_tax_class_id'), 'date_added' => lC_DateTime::getShort(lC_DateTime::getNow()), 'weight_class_id' => $Qproduct->valueInt('products_weight_class'));
             // simple options
             if (isset($_POST['simple_options']) && empty($_POST['simple_options']) === false) {
                 foreach ($_POST['simple_options'] as $options_id => $values_id) {
                     if (is_array($values_id)) {
                         $text_value = current($values_id);
                         // for text fields
                         $values_id = key($values_id);
                     }
                     $QsimpleOptionsValues = $lC_Database->query('select price_modifier from :table_products_simple_options_values where options_id = :options_id and values_id = :values_id and customers_group_id = :customers_group_id');
                     $QsimpleOptionsValues->bindTable(':table_products_simple_options_values', TABLE_PRODUCTS_SIMPLE_OPTIONS_VALUES);
                     $QsimpleOptionsValues->bindInt(':options_id', $options_id);
                     $QsimpleOptionsValues->bindInt(':values_id', $values_id);
                     $QsimpleOptionsValues->bindInt(':customers_group_id', '1');
                     $QsimpleOptionsValues->execute();
                     $Qvariants = $lC_Database->query('select pvg.title as group_title, pvg.module, pvv.title as value_title from :table_products_variants_groups pvg, :table_products_variants_values pvv where pvg.id = :options_id and pvv.id = :values_id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id limit 1');
                     $Qvariants->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
                     $Qvariants->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
                     $Qvariants->bindInt(':options_id', $options_id);
                     $Qvariants->bindInt(':values_id', $values_id);
                     $Qvariants->bindInt(':languages_id', $lC_Language->getID());
                     $Qvariants->bindInt(':languages_id', $lC_Language->getID());
                     $Qvariants->execute();
                     if (strstr($Qvariants->value('module'), 'file_upload')) {
                         $group_title = is_array($_FILES['simple_options_upload']['name']) && count($_FILES['simple_options_upload']['name']) > 2 ? $lC_Language->get('text_label_files') : $lC_Language->get('text_label_file');
                         $value_title = is_array($_FILES['simple_options_upload']['name']) ? implode(', ', $_FILES['simple_options_upload']['name']) : $_FILES['simple_options_upload']['name'];
                         if (substr($value_title, -2) == ', ') {
                             $value_title = substr($value_title, 0, -2);
                         }
                         $value_title = str_replace(', ,', ', ', $value_title);
                         if ($value_title == '') {
                             $group_title = '';
                         }
                         if (is_array($_FILES['simple_options_upload']['name'])) {
                             $filesArr = $_FILES;
                             $_SESSION['file_upload'] = $_FILES['simple_options_upload'];
                             foreach ($filesArr['simple_options_upload']['name'] as $key => $file) {
                                 $_FILES = array('simple_options_upload' => array('name' => $file, 'type' => $filesArr['simple_options_upload']['type'][$key], 'tmp_name' => $filesArr['simple_options_upload']['tmp_name'][$key], 'error' => $filesArr['simple_options_upload']['error'][$key], 'size' => $filesArr['simple_options_upload']['size'][$key]));
                                 // upload the file
                                 $image = new upload('simple_options_upload', realpath('pub'));
                                 if ($image->exists()) {
                                     if ($image->parse() && $image->save()) {
                                         // success
                                     }
                                 }
                             }
                         }
                     } else {
                         if ($Qvariants->value('module') == 'text_field') {
                             $group_title = $Qvariants->value('group_title');
                             $value_title = $text_value;
                         } else {
                             $group_title = $Qvariants->value('group_title');
                             $value_title = $Qvariants->value('value_title');
                         }
                     }
                     $this->_contents[$item_id]['simple_options'][] = array('value_id' => $values_id, 'group_id' => $options_id, 'group_title' => $group_title, 'value_title' => $value_title, 'price_modifier' => $QsimpleOptionsValues->valueDecimal('price_modifier'));
                     $QsimpleOptionsValues->freeResult();
                     $Qvariants->freeResult();
                 }
             }
             if ($lC_Customer->isLoggedOn()) {
                 $Qnew = $lC_Database->query('insert into :table_shopping_carts (customers_id, item_id, products_id, quantity, meta_data, date_added) values (:customers_id, :item_id, :products_id, :quantity, :meta_data, :date_added)');
                 $Qnew->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
                 $Qnew->bindInt(':customers_id', $lC_Customer->getID());
                 $Qnew->bindInt(':item_id', $item_id);
                 $Qnew->bindInt(':products_id', $product_id);
                 $Qnew->bindInt(':quantity', $quantity);
                 $Qnew->bindValue(':meta_data', serialize($this->_contents[$item_id]['simple_options']));
                 $Qnew->bindRaw(':date_added', 'now()');
                 $Qnew->execute();
             }
             if ($Qproduct->valueInt('parent_id') > 0) {
                 $Qvariant = $lC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.products_variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id');
                 $Qvariant->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
                 $Qvariant->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
                 $Qvariant->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
                 $Qvariant->bindInt(':products_id', $product_id);
                 $Qvariant->bindInt(':languages_id', $lC_Language->getID());
                 $Qvariant->bindInt(':languages_id', $lC_Language->getID());
                 $Qvariant->execute();
                 while ($Qvariant->next()) {
                     $group_title = lC_Variants::getGroupTitle($Qvariant->value('module'), $Qvariant->toArray());
                     $value_title = lC_Variants::getValueTitle($Qvariant->value('module'), $Qvariant->toArray());
                     $has_custom_value = lC_Variants::hasCustomValue($Qvariant->value('module'));
                     if (strstr($Qvariant->value('module'), 'file_upload')) {
                         $group_title = is_array($_FILES['variants_upload']['name']) && count($_FILES['variants_upload']['name']) > 2 ? $lC_Language->get('text_label_files') : $lC_Language->get('text_label_file');
                         $value_title = is_array($_FILES['variants_upload']['name']) ? implode(', ', $_FILES['variants_upload']['name']) : $_FILES['variants_upload']['name'];
                         if (substr($value_title, -2) == ', ') {
                             $value_title = substr($value_title, 0, -2);
                         }
                         if (is_array($_FILES['variants_upload']['name'])) {
                             $filesArr = $_FILES;
                             $_SESSION['file_upload'] = $_FILES['variants_upload'];
                             foreach ($filesArr['variants_upload']['name'] as $key => $file) {
                                 $_FILES = array('variants_upload' => array('name' => $file, 'type' => $filesArr['variants_upload']['type'][$key], 'tmp_name' => $filesArr['variants_upload']['tmp_name'][$key], 'error' => $filesArr['variants_upload']['error'][$key], 'size' => $filesArr['variants_upload']['size'][$key]));
                                 // upload the file
                                 $image = new upload('variants_upload', realpath('pub'));
                                 if ($image->exists()) {
                                     if ($image->parse() && $image->save()) {
                                         // success
                                     }
                                 }
                             }
                         }
                     } else {
                         if ($Qvariant->value('module') == 'text_field') {
                             $group_title = $Qvariant->value('group_title');
                         } else {
                             $group_title = $Qvariant->value('group_title');
                             $value_title = $Qvariant->value('value_title');
                         }
                     }
                     $this->_contents[$item_id]['variants'][] = array('group_id' => $Qvariant->valueInt('group_id'), 'value_id' => $Qvariant->valueInt('value_id'), 'group_title' => $group_title, 'value_title' => $value_title, 'has_custom_value' => $has_custom_value);
                     if ($lC_Customer->isLoggedOn() && $has_custom_value === true) {
                         $Qnew = $lC_Database->query('insert into :table_shopping_carts_custom_variants_values (shopping_carts_item_id, customers_id, products_id, products_variants_values_id, products_variants_values_text) values (:shopping_carts_item_id, :customers_id, :products_id, :products_variants_values_id, :products_variants_values_text)');
                         $Qnew->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
                         $Qnew->bindInt(':shopping_carts_item_id', $item_id);
                         $Qnew->bindInt(':customers_id', $lC_Customer->getID());
                         $Qnew->bindInt(':products_id', $product_id);
                         $Qnew->bindInt(':products_variants_values_id', $Qvariant->valueInt('value_id'));
                         $Qnew->bindValue(':products_variants_values_text', $value_title);
                         $Qnew->execute();
                     }
                 }
             }
         }
         $this->_cleanUp();
         $this->_calculate();
     }
 }
예제 #2
0
 public static function defineJavascript($products)
 {
     global $lC_Currencies;
     $string = '<script>var combos = new Array();' . "\n";
     foreach ($products as $product_id => $product) {
         $string .= 'combos[' . $product_id . '] = new Array();' . "\n" . 'combos[' . $product_id . '] = { price: "' . addslashes($lC_Currencies->displayPrice($product['data']['price'], $product['data']['tax_class_id'])) . '", model: "' . addslashes($product['data']['model']) . '", availability_shipping: ' . (int) $product['data']['availability_shipping'] . ', values: [] };' . "\n";
         foreach ($product['values'] as $group_id => $variants) {
             $check_flag = false;
             foreach ($variants as $variant) {
                 if (!lC_Variants::hasCustomValue($variant['module'])) {
                     if ($check_flag === false) {
                         $check_flag = true;
                         $string .= 'combos[' . $product_id . ']["values"][' . $group_id . '] = new Array();' . "\n";
                     }
                     $string .= 'combos[' . $product_id . ']["values"][' . $group_id . '][' . $variant['value_id'] . '] = ' . $variant['value_id'] . ';' . "\n";
                 }
             }
         }
     }
     $string .= '</script>';
     return $string;
 }