Exemplo n.º 1
0
 function add($products_id_string, $variants = null, $quantity = null, $gift_certificates_data = null, $action = 'add')
 {
     global $osC_Database, $osC_Services, $osC_Language, $osC_Customer, $osC_Image, $toC_Wishlist;
     $products_id = osc_get_product_id($products_id_string);
     $osC_Product = new osC_Product($products_id);
     if ($osC_Product->isGiftCertificate()) {
         if ($variants == null || empty($variants)) {
             $products_id_string = $products_id . '#' . time();
         } else {
             $products_id_string = $products_id . '#' . $variants;
             //set variants to null
             $variants = null;
         }
     } else {
         $products_id_string = osc_get_product_id_string($products_id_string, $variants);
     }
     if ($osC_Product->getID() > 0) {
         if ($toC_Wishlist->hasProduct($products_id)) {
             $toC_Wishlist->deleteProduct($products_id);
         }
         if ($this->exists($products_id_string)) {
             if (!is_numeric($quantity)) {
                 $quantity = $this->getQuantity($products_id_string) + 1;
             } else {
                 if (is_numeric($quantity) && $quantity == 0) {
                     $this->remove($products_id_string);
                     return;
                 } else {
                     if ($action == 'add') {
                         $quantity = $this->getQuantity($products_id_string) + $quantity;
                     } else {
                         if ($action == 'update') {
                             $quantity = $quantity;
                         }
                     }
                 }
             }
             if ($osC_Product->isGiftCertificate()) {
                 if ($quantity > 1) {
                     $quantity = 1;
                     $error = $osC_Language->get('error_gift_certificate_quantity_must_be_one');
                 }
             }
             //check minimum order quantity
             $products_moq = $osC_Product->getMOQ();
             if ($quantity < $products_moq) {
                 $quantity = $products_moq;
                 $error = sprintf($osC_Language->get('error_minimum_order_quantity'), $osC_Product->getTitle(), $products_moq);
             }
             //check maximum order quantity
             $products_max_order_quantity = $osC_Product->getMaxOrderQuantity();
             if ($products_max_order_quantity > 0) {
                 if ($quantity > $products_max_order_quantity) {
                     $quantity = $products_max_order_quantity;
                     $error = sprintf($osC_Language->get('error_maximum_order_quantity'), $osC_Product->getTitle(), $products_max_order_quantity);
                 }
             }
             //check order increment
             $increment = $osC_Product->getOrderIncrement();
             if (($quantity - $products_moq) % $increment != 0) {
                 $quantity = $products_moq + (floor(($quantity - $products_moq) / $increment) + 1) * $increment;
                 $error = sprintf($osC_Language->get('error_order_increment'), $osC_Product->getTitle(), $increment);
             }
             //set error to session
             if (isset($error) && !empty($error)) {
                 $this->_contents[$products_id_string]['error'] = $error;
             }
             if ($osC_Product->isGiftCertificate() && $osC_Product->isOpenAmountGiftCertificate()) {
                 $price = $this->_contents[$products_id_string]['price'];
             } else {
                 $price = $osC_Product->getPrice($variants, $quantity);
                 if ($osC_Services->isStarted('specials')) {
                     global $osC_Specials;
                     if ($new_price = $osC_Specials->getPrice($products_id)) {
                         $price = $new_price;
                     }
                 }
             }
             $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 ($osC_Customer->isLoggedOn()) {
                 $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity, gift_certificates_data = :gift_certificates_data where customers_id = :customers_id and products_id = :products_id');
                 $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
                 $Qupdate->bindInt(':customers_basket_quantity', $quantity);
                 if ($osC_Product->getProductType() == PRODUCT_TYPE_GIFT_CERTIFICATE) {
                     $Qupdate->bindValue(':gift_certificates_data', serialize($gift_certificates_data));
                 } else {
                     $Qupdate->bindRaw(':gift_certificates_data', 'null');
                 }
                 $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
                 $Qupdate->bindValue(':products_id', $products_id_string);
                 $Qupdate->execute();
             }
         } else {
             if (!is_numeric($quantity)) {
                 $quantity = 1;
             }
             if ($osC_Product->isGiftCertificate()) {
                 if ($quantity > 1) {
                     $quantity = 1;
                     $error = $osC_Language->get('error_gift_certificate_quantity_must_be_one');
                 }
             }
             //check minimum order quantity
             $products_moq = $osC_Product->getMOQ();
             if ($quantity < $products_moq) {
                 $quantity = $products_moq;
                 $error = sprintf($osC_Language->get('error_minimum_order_quantity'), $osC_Product->getTitle(), $products_moq);
             }
             //check order increment
             $increment = $osC_Product->getOrderIncrement();
             if (($quantity - $products_moq) % $increment != 0) {
                 $quantity = $products_moq + (floor(($quantity - $products_moq) / $increment) + 1) * $increment;
                 $error = sprintf($osC_Language->get('error_order_increment'), $osC_Product->getTitle(), $increment);
             }
             if ($osC_Product->isGiftCertificate() && $osC_Product->isOpenAmountGiftCertificate()) {
                 $price = $gift_certificates_data['price'];
             } else {
                 $price = $osC_Product->getPrice($variants, $quantity);
                 if ($osC_Services->isStarted('specials')) {
                     global $osC_Specials;
                     if ($new_price = $osC_Specials->getPrice($products_id)) {
                         $price = $new_price;
                     }
                 }
             }
             $this->_contents[$products_id_string] = array('id' => $products_id_string, 'name' => $osC_Product->getTitle(), 'type' => $osC_Product->getProductType(), 'keyword' => $osC_Product->getKeyword(), 'sku' => $osC_Product->getSKU($variants), 'image' => $osC_Product->getImage(), 'price' => $price, 'final_price' => $price, 'quantity' => $quantity, 'weight' => $osC_Product->getWeight($variants), 'tax_class_id' => $osC_Product->getTaxClassID(), 'date_added' => osC_DateTime::getShort(osC_DateTime::getNow()), 'weight_class_id' => $osC_Product->getWeightClass(), 'gc_data' => $gift_certificates_data);
             //set error to session
             if (isset($error) && !empty($error)) {
                 $this->_contents[$products_id_string]['error'] = $error;
             }
             // insert into database
             if ($osC_Customer->isLoggedOn()) {
                 $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, gift_certificates_data, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, :gift_certificates_data, now())');
                 $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
                 $Qnew->bindInt(':customers_id', $osC_Customer->getID());
                 $Qnew->bindValue(':products_id', $products_id_string);
                 $Qnew->bindInt(':customers_basket_quantity', $quantity);
                 if ($osC_Product->getProductType() == PRODUCT_TYPE_GIFT_CERTIFICATE) {
                     $Qnew->bindValue(':gift_certificates_data', serialize($gift_certificates_data));
                 } else {
                     $Qnew->bindRaw(':gift_certificates_data', 'null');
                 }
                 $Qnew->execute();
             }
             if (is_array($variants) && !empty($variants)) {
                 foreach ($variants as $group_id => $value_id) {
                     $Qvariants = $osC_Database->query('select pvg.products_variants_groups_name, pvv.products_variants_values_name from :table_products_variants pv, :table_products_variants_entries pve, :table_products_variants_groups pvg, :table_products_variants_values pvv where pv.products_id = :products_id and pv.products_variants_id = pve.products_variants_id and pve.products_variants_groups_id = :groups_id and pve.products_variants_values_id = :variants_values_id and pve.products_variants_groups_id = pvg.products_variants_groups_id and pve.products_variants_values_id = pvv.products_variants_values_id and pvg.language_id = :language_id and pvv.language_id = :language_id');
                     $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
                     $Qvariants->bindTable(':table_products_variants_entries', TABLE_PRODUCTS_VARIANTS_ENTRIES);
                     $Qvariants->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
                     $Qvariants->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
                     $Qvariants->bindInt(':products_id', $osC_Product->getID());
                     $Qvariants->bindInt(':groups_id', $group_id);
                     $Qvariants->bindInt(':variants_values_id', $value_id);
                     $Qvariants->bindInt(':language_id', $osC_Language->getID());
                     $Qvariants->bindInt(':language_id', $osC_Language->getID());
                     $Qvariants->execute();
                     $this->_contents[$products_id_string]['variants'][$group_id] = array('groups_id' => $group_id, 'variants_values_id' => $value_id, 'groups_name' => $Qvariants->value('products_variants_groups_name'), 'values_name' => $Qvariants->value('products_variants_values_name'));
                 }
             }
         }
         $this->_cleanUp();
         $this->_calculate();
     }
 }