Example #1
0
 public function updateProduct($id, $info, $categories = 0, $bCheckId = false)
 {
     $id = (int) $id;
     if ($bCheckId && !Product::existsInDatabase($id)) {
         return false;
     }
     $info['date_upd'] = date('Y-m-d H:i:s');
     $ret = Db::getInstance()->autoExecute(_DB_PREFIX_ . 'product', $info, 'UPDATE', '`id_product` = ' . $id);
     if (is_array($categories) && count($categories)) {
         $product = new Product($id);
         $b_retcats = $product->addToCategories(array_map('intval', $categories));
     }
     return $ret ? $id : false;
 }
Example #2
0
 /**
  * Add categorie
  */
 private function eeweeProductAddCateg()
 {
     $p = new Product($this->id_product);
     $p->addToCategories(array(2, 3, 4));
 }
 /**
  * Inserts a product in Prestashop.
  * 
  * @param array $product_attributes
  * @param string $url
  * @param array $triple_cod_col_siz
  * @param array $array_combinations
  * @param integer $language
  * @return array 
  * @see $this->setSupplierForProduct
  * @see $this->setArrayElementForLinkRewrite
  * @see $this->setManufacturerForProduct
  * @see $this->addFeaturesForProducts
  * @see $this->setCategoriesForProduct
  * @see $this->addCombinationsForPrestashop
  * @see PrestashopImageHandler insertImageInPrestashop()
  *
  */
 public function insertProductForPrestashop($product_attributes = array(), $url_photo, $triple_cod_col_siz, $array_combinations, $language = 1)
 {
     $product = new Product();
     //$language is 1 because 1 -> italian
     $string_name_product = trim($product_attributes["Nome"]);
     $product->name = $this->setArrayElementForLinkRewrite($string_name_product, true, $language);
     $product->meta_keywords = $string_name_product;
     $product->link_rewrite = $this->setArrayElementForLinkRewrite($string_name_product, false, $language);
     $product->id_category_default = 2;
     //Home
     $product->redirect_type = '404';
     $product->price = (double) $product_attributes["Prezzo"];
     $product->active = (int) $product_attributes["Attivo"];
     $product->minimal_quantity = (int) $product_attributes["Qta_min"];
     $product->show_price = 1;
     $product->reference = $product_attributes["Reference"];
     $product->id_tax_rules_group = 0;
     //$product->id_supplier = $this->setSupplierForProduct(trim($product_attributes["Supplier"]));
     $product->id_supplier = 0;
     $product->id_manufacturer = $this->setManufacturerForProduct(trim($product_attributes["Manufacture"]));
     $array_features = $product_attributes["Feature"];
     $product->width = (double) $array_features["Larghezza"];
     $product->height = (double) $array_features["Altezza"];
     $product->add();
     $this->addFeaturesForProducts($product->id, $language, $array_features);
     $categories = trim($product_attributes["Categorie"]);
     $tmp = explode(",", $categories);
     $griffe = $tmp[1];
     $modello = $tmp[0];
     $id_parent = null;
     $id_child = null;
     $id_parent = $this->setCategoriesForProduct(trim($griffe), 2, $language);
     $id_child = $this->setCategoriesForProduct(trim($modello), $id_parent, $language);
     $cat_pos_tmp = array();
     array_push($cat_pos_tmp, $id_child);
     array_push($cat_pos_tmp, $id_parent);
     $product->addToCategories($cat_pos_tmp);
     if ($product->active != 0) {
         $activeCategory = new Category($id_parent);
         $activeCategory->active = 1;
         $activeCategory->update();
         $activeCategory = new Category($id_child);
         $activeCategory->active = 1;
         $activeCategory->update();
     }
     $tmp = explode(".jpg,", trim($product_attributes["URL"]));
     $size_url = sizeof($tmp);
     $array_id_image = array();
     for ($i = 0; $i < $size_url; $i++) {
         if (!empty($tmp[$i])) {
             $url = trim($url_photo);
             $image = new PrestashopImageHandler();
             $id_image = $image->insertImageInPrestashop($product->id, $url, $tmp[$i]);
             array_push($array_id_image, $id_image);
         }
     }
     $this->addCombinationsForPrestashop($product->id, $url_photo, $triple_cod_col_siz, $array_combinations, $language);
     $return = array();
     $array_images_combinations_of_the_product = $product->getImages($language);
     array_push($return, $product->id);
     $element = array();
     foreach ($array_images_combinations_of_the_product as $array_combo_image) {
         $name_of_the_image = $array_combo_image['legend'];
         $id_image_of_the_product = $array_combo_image['id_image'];
         array_push($element, $id_image_of_the_product . ";" . $name_of_the_image);
     }
     array_push($return, $element);
     return $return;
 }
Example #4
0
    /**
     * Save in special database each buyer protection product for a certificate,
     * Each Trusted Shops particular characteristics is saved.
     * Create a product in Prestashop database to allow added each of them in cart.
     *
     * @param array|stdClass $protection_items
     * @param string $ts_id
     */
    private function saveProtectionItems($protection_items, $ts_id)
    {
        $query = '
		DELETE ts, p, pl
		FROM `' . _DB_PREFIX_ . TSCommon::DB_ITEMS . '` AS ts
		LEFT JOIN `' . _DB_PREFIX_ . 'product` AS p ON ts.`id_product` = p.`id_product`
		LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` AS pl ON ts.`id_product` = pl.`id_product`
		WHERE ts.`ts_id`="' . pSQL($ts_id) . '"';
        Db::getInstance()->Execute($query);
        foreach ($protection_items as $item) {
            //add hidden product
            $product = new Product();
            foreach (array_keys(self::$available_languages) as $iso) {
                $language = Language::getIdByIso(Tools::strtolower($iso));
                if ((int) $language !== 0) {
                    $product->name[$language] = 'TrustedShops guarantee';
                    $product->link_rewrite[$language] = 'trustedshops_guarantee';
                }
            }
            // If the default lang is different than available languages :
            // (Bug occurred otherwise)
            if (!array_key_exists(Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')), self::$available_languages)) {
                $product->name[(int) Configuration::get('PS_LANG_DEFAULT')] = 'Trustedshops';
                $product->link_rewrite[(int) Configuration::get('PS_LANG_DEFAULT')] = 'trustedshops';
            }
            // Add specifics translations
            $id_lang = Language::getIdByIso('de');
            if ((int) $id_lang > 0) {
                $product->name[$id_lang] = 'Trusted Shops Käuferschutz';
            }
            $id_lang = Language::getIdByIso('en');
            if ((int) $id_lang > 0) {
                $product->name[$id_lang] = 'Trusted Shops buyer protection';
            }
            $id_lang = Language::getIdByIso('fr');
            if ((int) $id_lang > 0) {
                $product->name[$id_lang] = 'Trusted Shops protection acheteur';
            }
            $id_lang = Language::getIdByIso('it');
            if ((int) $id_lang > 0) {
                $product->name[$id_lang] = 'Trusted Shops protezione acquirenti';
            }
            $product->quantity = 1000;
            $product->price = Tools::convertPrice($item->grossFee, Currency::getIdByIsoCode($item->currency));
            $product->id_category_default = TSCommon::$cat_id;
            $product->active = true;
            $product->visibility = 'none';
            $product->id_tax_rules_group = 0;
            $product->add();
            $product->addToCategories(TSCommon::$cat_id);
            if ($product->id) {
                $query = '
				INSERT INTO `' . _DB_PREFIX_ . TSCommon::DB_ITEMS . '`
				(`creation_date`, `id_product`, `ts_id`, `id`, `currency`, `gross_fee`, `net_fee`,
				`protected_amount_decimal`, `protection_duration_int`, `ts_product_id`)
				VALUES ("' . pSQL($item->creationDate) . '", "' . pSQL($product->id) . '", "' . pSQL($ts_id) . '",
				"' . (int) $item->id . '", "' . pSQL($item->currency) . '", "' . pSQL($item->grossFee) . '",
				"' . pSQL($item->netFee) . '", "' . pSQL($item->protectedAmountDecimal) . '",
				"' . pSQL($item->protectionDurationInt) . '", "' . pSQL($item->tsProductID) . '")';
                Db::getInstance()->Execute($query);
                if (class_exists('StockAvailable')) {
                    $id_stock_available = Db::getInstance()->getValue('
						SELECT s.`id_stock_available` FROM `' . _DB_PREFIX_ . 'stock_available` s
						WHERE s.`id_product` = ' . (int) $product->id);
                    $stock = new StockAvailable($id_stock_available);
                    $stock->id_product = $product->id;
                    $stock->out_of_stock = 1;
                    $stock->id_product_attribute = 0;
                    $stock->quantity = 1000000;
                    $stock->id_shop = Context::getContext()->shop->id;
                    if ($stock->id) {
                        $stock->update();
                    } else {
                        $stock->add();
                    }
                }
            } else {
                $this->errors['products'] = $this->l('Product wasn\'t saved.');
            }
        }
    }
Example #5
0
 /**
  * Added Dotpay virtual product for extracharge option
  * @return bool
  */
 private function addDotpayVirtualProduct()
 {
     if (Validate::isInt($this->config->getDotpayExchVPid()) and Validate::isLoadedObject($product = new Product($this->config->getDotpayExchVPid())) and Validate::isInt($product->id)) {
         return true;
     }
     $product = new Product();
     $product->name = array((int) Configuration::get('PS_LANG_DEFAULT') => 'Online payment');
     $product->link_rewrite = array((int) Configuration::get('PS_LANG_DEFAULT') => 'online-payment');
     $product->visibility = 'none';
     $product->reference = 'DOTPAYFEE';
     $product->price = 0.0;
     $product->is_virtual = 1;
     $product->online_only = 1;
     $product->redirect_type = '404';
     $product->quantity = 9999999;
     $product->id_tax_rules_group = 0;
     $product->active = 1;
     $product->meta_keywords = 'payment';
     $product->id_category = 1;
     $product->id_category_default = 1;
     if (!$product->add()) {
         return false;
     }
     $product->addToCategories(array(1));
     StockAvailable::setQuantity($product->id, NULL, $product->quantity);
     $this->config->setDotpayExchVPid($product->id);
     return true;
 }
 protected function importProducts()
 {
     $this->truncateTables(array('product', 'product_lang', 'product_shop', 'product_sale', 'product_supplier', 'product_tag', 'feature_product', 'category_product', 'product_carrier', 'compare_product', 'product_attachment', 'product_country_tax', 'product_download', 'product_group_reduction_cache', 'scene_products', 'warehouse_product_location', 'customization', 'customization_field', 'customization_field_lang', 'supply_order_detail', 'attribute_impact', 'pack'));
     $handle = $this->openCsvFile('products.csv');
     $languages = Language::getLanguages(false);
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) {
         $res = false;
         $product = new Product((int) $line[0]);
         $product->id = (int) $line[0];
         $product->active = $line[1];
         foreach ($languages as $lang) {
             $product->name[$lang['id_lang']] = $line['2'];
         }
         $categories = explode(',', $line[3]);
         $product->id_category_default = $categories[0] ? $categories[0] : Configuration::get('PS_HOME_CATEGORY');
         if (isset($categories) && $categories && count($categories)) {
             $product->addToCategories($categories);
         }
         if (isset($line[4])) {
             $product->price_tex = $line[4];
         }
         if (isset($line[5])) {
             $product->price_tin = $line[5];
         }
         $product->id_tax_rules_group = trim($line[6]) ? $line[6] : 0;
         $product->wholesale_price = trim($line[7]) ? $line[7] : 0;
         $product->on_sale = trim($line[8]) ? $line[8] : 0;
         if (trim($line[13])) {
             $product->reference = $line[13];
         }
         if (trim($line[14])) {
             $product->supplier_reference = trim($line[14]);
         }
         if (trim($line[15])) {
             $product->id_supplier = (int) $line[15];
         }
         if (isset($product->id) && $product->id && isset($product->id_supplier) && property_exists($product, 'supplier_reference')) {
             $id_product_supplier = (int) ProductSupplier::getIdByProductAndSupplier((int) $product->id, 0, (int) $product->id_supplier);
             if ($id_product_supplier) {
                 $product_supplier = new ProductSupplier($id_product_supplier);
             } else {
                 $product_supplier = new ProductSupplier();
             }
             $product_supplier->id_product = (int) $product->id;
             $product_supplier->id_product_attribute = 0;
             $product_supplier->id_supplier = (int) $product->id_supplier;
             $product_supplier->product_supplier_price_te = $product->wholesale_price;
             $product_supplier->product_supplier_reference = $product->supplier_reference;
             $product_supplier->save();
         }
         if (trim($line[16])) {
             $product->id_manufacturer = $line[16];
         }
         if (!Tools::isEmpty(trim($line[17]))) {
             $product->ean13 = $line[17];
         }
         if (trim($line[18])) {
             $product->upc = $line[18];
         }
         if (trim($line[19])) {
             $product->ecotax = $line[19];
         }
         $product->width = $line[20];
         $product->height = $line[21];
         $product->depth = $line[22];
         $product->weight = $line[23];
         if ($line[24]) {
             StockAvailable::setQuantity((int) $product->id, 0, (int) $line[24], (int) $this->context->shop->id);
         }
         $product->minimal_quantity = $line[25];
         $product->visibility = $line[26];
         $product->additional_shipping_cost = $line[27];
         if (trim($line[28])) {
             $product->unity = $line[28];
         }
         if (trim($line[29])) {
             $product->unit_price = $line[29];
         }
         foreach ($languages as $lang) {
             $product->description_short[$lang['id_lang']] = $line[30];
             $product->description[$lang['id_lang']] = $line[31];
         }
         if ($line[32]) {
             foreach ($languages as $lang) {
                 Tag::addTags($lang['id_lang'], $product->id, $line[32]);
             }
         }
         foreach ($languages as $lang) {
             $product->meta_title[$lang['id_lang']] = $line[33];
             $product->meta_keywords[$lang['id_lang']] = $line[34];
             $product->meta_description[$lang['id_lang']] = $line[35];
             $product->link_rewrite[$lang['id_lang']] = $line[36];
             $product->available_now[$lang['id_lang']] = $line[37];
             $product->available_later[$lang['id_lang']] = $line[38];
         }
         $product->available_for_order = $line[39];
         $product->available_date = $line[40];
         $product->date_add = $line[41];
         $product->show_price = $line[42];
         // Features import
         $features = explode(',', $line[45]);
         if ($features) {
             foreach ($features as $feature) {
                 $value = explode(':', $feature);
                 if ($value[0] && $value[1]) {
                     Product::addFeatureProductImport((int) $product->id, (int) $value[0], (int) $value[1]);
                     SpecificPriceRule::applyAllRules(array((int) $product->id));
                 }
             }
         }
         $product->online_only = trim($line[46]) ? $line[46] : 0;
         $product->condition = $line[47];
         $product->customizable = trim($line[48]) ? $line[48] : 0;
         $product->uploadable_files = trim($line[49]) ? $line[49] : 0;
         $product->text_fields = trim($line[50]) ? $line[50] : 0;
         if ($product->getType() == Product::PTYPE_VIRTUAL) {
             StockAvailable::setProductOutOfStock((int) $product->id, 1);
         } else {
             StockAvailable::setProductOutOfStock((int) $product->id, (int) $line[51]);
         }
         $product->id_shop_default = $line[52];
         // add product accessories
         if ($line[56]) {
             $accessories = explode(',', $line[56]);
             foreach ($accessories as $accessory) {
                 $a[]['id'] = $accessory;
             }
             $product->setWsAccessories($a);
         }
         // add product carriers
         if ($line[57]) {
             $carriers = explode(',', $line[57]);
             $product->setCarriers($carriers);
         }
         // add costomisation fields
         if (!Tools::isEmpty($line[58]) && class_exists('CustomizationField')) {
             $customisation_fields_ids = explode(',', $line[58]);
             foreach ($customisation_fields_ids as $customisation_field) {
                 $result = false;
                 $customisation_data = explode(':', $customisation_field);
                 $cf = new CustomizationField();
                 $cf->id_product = $product->id;
                 $cf->type = $customisation_data[1];
                 $cf->required = $customisation_data[2];
                 foreach ($languages as $lang) {
                     $cf->name[$lang['id_lang']] = $customisation_data[3] ? $customisation_data[3] : ' ';
                 }
                 $cf->force_id = 1;
                 if (!$result) {
                     $result = $cf->add();
                 }
             }
         }
         // add attachments
         if ($line[59]) {
             $attachments = explode(',', $line[59]);
             if (isset($attachments) && count($attachments)) {
                 Attachment::attachToProduct($product->id, $attachments);
             }
         }
         if ($line[60]) {
             $product->date_upd = $line[60];
         }
         $product->price = $line[61];
         $product->force_id = 1;
         if (!$res) {
             $res = $product->add();
         }
     }
     $this->closeCsvFile($handle);
     Search::indexation(true);
     return true;
 }
 public function createDummyDataForProject()
 {
     //delete privious products of prestashop
     $all_products = Product::getSimpleProducts(Configuration::get('PS_LANG_DEFAULT'));
     foreach ($all_products as $key_pro => $value_pro) {
         $obj_product = new Product($value_pro['id_product']);
         $obj_product->delete();
     }
     // first add a hotel.................
     $def_cont_id = Country::getDefaultCountryId();
     $obj_hotel_info = new HotelBranchInformation();
     $obj_hotel_info->active = 1;
     $obj_hotel_info->hotel_name = "The Hotel Prime";
     $obj_hotel_info->phone = 01234567;
     $obj_hotel_info->email = "*****@*****.**";
     $obj_hotel_info->check_in = '12:00';
     $obj_hotel_info->check_out = '12:00';
     $obj_hotel_info->short_description = $this->l('Nice place to stay');
     $obj_hotel_info->description = $this->l('Nice place to stay');
     $obj_hotel_info->rating = 3;
     $obj_hotel_info->city = 'Nainital';
     $states = State::getStatesByIdCountry($def_cont_id);
     $state_id = $states[0]['id_state'];
     $obj_hotel_info->state_id = $state_id;
     $obj_hotel_info->country_id = $def_cont_id;
     $obj_hotel_info->zipcode = 263001;
     $obj_hotel_info->policies = $this->l('1. intelligentsia tattooed pop-up salvia asymmetrical mixtape meggings tousled ramps VHS cred. 2. intelligentsia tattooed pop-up salvia asymmetrical mixtape meggings tousled ramps VHS cred. 3. intelligentsia tattooed pop-up salvia asymmetrical mixtape meggings tousled ramps VHS cred. 4. intelligentsia tattooed pop-up salvia asymmetrical mixtape meggings tousled ramps VHS cred.');
     $obj_hotel_info->address = 'Near post office, Mallital, Nainital';
     $obj_hotel_info->save();
     $htl_id = $obj_hotel_info->id;
     $grp_ids = array();
     $obj_grp = new Group();
     $data_grp_ids = $obj_grp->getGroups(1, $id_shop = false);
     foreach ($data_grp_ids as $key => $value) {
         $grp_ids[] = $value['id_group'];
     }
     $country_name = (new Country())->getNameById(Configuration::get('PS_LANG_DEFAULT'), $def_cont_id);
     $cat_country = $this->addCategory($country_name, false, $grp_ids);
     if ($cat_country) {
         $states = State::getStatesByIdCountry($def_cont_id);
         $state_name = $states[0]['name'];
         $cat_state = $this->addCategory($state_name, $cat_country, $grp_ids);
     }
     if ($cat_state) {
         $cat_city = $this->addCategory('DefCity', $cat_state, $grp_ids);
     }
     if ($cat_city) {
         $cat_hotel = $this->addCategory('The Hotel Prime', $cat_city, $grp_ids, 1, $htl_id);
     }
     if ($cat_hotel) {
         $obj_hotel_info = new HotelBranchInformation($htl_id);
         $obj_hotel_info->id_category = $cat_hotel;
         $obj_hotel_info->save();
     }
     $branch_ftr_ids = array(1, 2, 4, 7, 8, 9, 11, 12, 14, 16, 17, 18, 21);
     foreach ($branch_ftr_ids as $key_ftr => $value_ftr) {
         $htl_ftr_obj = new HotelBranchFeatures();
         $htl_ftr_obj->id_hotel = $htl_id;
         $htl_ftr_obj->feature_id = $value_ftr;
         $htl_ftr_obj->save();
     }
     $prod_arr = array('Delux Rooms', 'Executive Rooms', 'luxury Rooms');
     $img_num = 1;
     foreach ($prod_arr as $key_prod => $value_prod) {
         // Add Product
         $product = new Product();
         $product->name = array();
         $product->description = array();
         $product->description_short = array();
         $product->link_rewrite = array();
         foreach (Language::getLanguages(true) as $lang) {
             $product->name[$lang['id_lang']] = $value_prod;
             $product->description[$lang['id_lang']] = $this->l('Fashion axe kogi yuccie, ramps shabby chic direct trade before they sold out distillery bicycle rights. Slow-carb +1 quinoa VHS. +1 brunch trust fund, meggings chartreuse sustainable everyday carry tumblr hoodie tacos tilde ramps post-ironic fixie.');
             $product->description_short[$lang['id_lang']] = $this->l('Fashion axe kogi yuccie, ramps shabby chic direct trade before they sold out distillery bicycle rights. Slow-carb +1 quinoa VHS. +1 brunch trust fund, meggings chartreuse sustainable everyday carry tumblr hoodie tacos tilde ramps post-ironic fixie.');
             $product->link_rewrite[$lang['id_lang']] = Tools::link_rewrite('Super Delux Rooms');
         }
         $product->id_shop_default = Context::getContext()->shop->id;
         $product->id_category_default = 2;
         $product->price = 1000;
         $product->active = 1;
         $product->quantity = 99999999;
         $product->is_virtual = 1;
         $product->indexed = 1;
         $product->save();
         $product_id = $product->id;
         Search::indexation(Tools::link_rewrite($value_prod), $product_id);
         $product->addToCategories(2);
         StockAvailable::updateQuantity($product_id, null, 99999999);
         //image upload for products
         $count = 0;
         $have_cover = false;
         $old_path = _PS_MODULE_DIR_ . $this->name . '/views/img/prod_imgs/' . $img_num . '.png';
         $image_obj = new Image();
         $image_obj->id_product = $product_id;
         $image_obj->position = Image::getHighestPosition($product_id) + 1;
         if ($count == 0) {
             if (!$have_cover) {
                 $image_obj->cover = 1;
             }
         } else {
             $image_obj->cover = 0;
         }
         $image_obj->add();
         $new_path = $image_obj->getPathForCreation();
         $imagesTypes = ImageType::getImagesTypes('products');
         foreach ($imagesTypes as $image_type) {
             ImageManager::resize($old_path, $new_path . '-' . $image_type['name'] . '.jpg', $image_type['width'], $image_type['height']);
         }
         ImageManager::resize($old_path, $new_path . '.jpg');
         for ($k = 1; $k <= 5; $k++) {
             $htl_room_info_obj = new HotelRoomInformation();
             $htl_room_info_obj->id_product = $product_id;
             $htl_room_info_obj->id_hotel = $htl_id;
             $htl_room_info_obj->room_num = 'A' . $i . '-10' . $k;
             $htl_room_info_obj->id_status = 1;
             $htl_room_info_obj->floor = 'first';
             $htl_room_info_obj->save();
         }
         $htl_rm_type = new HotelRoomType();
         $htl_rm_type->id_product = $product_id;
         $htl_rm_type->id_hotel = $htl_id;
         $htl_rm_type->adult = 2;
         $htl_rm_type->children = 2;
         $htl_rm_type->save();
         $img_num++;
         // Add features to the product
         $ftr_arr = array(0 => 8, 1 => 9, 2 => 10, 3 => 11);
         $ftr_val_arr = array(0 => 34, 1 => 35, 2 => 36, 3 => 37);
         foreach ($ftr_arr as $key_htl_ftr => $val_htl_ftr) {
             $product->addFeaturesToDB($val_htl_ftr, $ftr_val_arr[$key_htl_ftr]);
         }
     }
     return true;
 }