Example #1
0
    private function upsert($line) {
        $product = DB::getInstance()->getValue("SELECT `id_product` FROM `" . _DB_PREFIX_ . "product` WHERE `reference` LIKE '{$line['reference']}'");
        $product = new Product($product);

        $product->reference = $line['reference'];
        $product->name = array(
            '1' => $line['name'],
            '2' => $line['name'],
            '3' => $line['name']
        );
        $product->description = array(
            '1' => $line['description'],
            '2' => $line['description'],
            '3' => $line['description']
        );
        $product->description_short = array(
            '1' => $line['description_short'],
            '2' => $line['description_short'],
            '3' => $line['description_short']
        );
        $product->link_rewrite = array(
            '1' => Tools::link_rewrite($line['name']),
            '2' => Tools::link_rewrite($line['name']),
            '3' => Tools::link_rewrite($line['name'])
        );
        $product->available_now = array(
            '1' => "Есть в наличии",
            '2' => "Есть в наличии",
            '3' => "Есть в наличии"
        );
        $product->id_category_default = $line['category'];
        $product->quantity = (int)$line['count'];

        $product->advanced_stock_management = 1; //использовать Advanced Stock management
        $product->depends_on_stock = 1; //1 - доступное количество на основе ASM. 0 - указывается вручную
        $product->out_of_stock = 1; //2 - как в Preferences product. 1 - allow (Как в Preferences - не дает заказать товар на сайте)

        $product->price = $line['price'];
        $product->weight = $line['weight'] / 1000;
        $product->id_tax_rules_group = $line['id_tax'];

        $product->save();

        $product->updateCategories(array($line['category']));

        $product->deleteFeatures();
        if ($line['author']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(9, $line['author'], null, Configuration::get('PS_LANG_DEFAULT'));
            Product::addFeatureProductImport($product->id, 9, $id_feature_value);
        }

        if ($line['year']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(10, $line['year'], null, Configuration::get('PS_LANG_DEFAULT'));
            Product::addFeatureProductImport($product->id, 10, $id_feature_value);
        }

        if ($line['paperback']) {
            Product::addFeatureProductImport($product->id, 11, 1); //1 - id значения "переплёт" у харакатеристики "Переплёт"
        }

        if ($line['pages']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(12, $line['pages'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 12, $id_feature_value);
        }

        /*
        if ($line['weight']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(4, $line['weight'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 4, $id_feature_value);
        }
        */

        if ($line['isbn']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(13, $line['isbn'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 13, $id_feature_value);
        }

        if ($line['publishing']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(14, $line['publishing'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 14, $id_feature_value);
        }

        $location = WarehouseProductLocation::getIdByProductAndWarehouse($product->id, 0, $line['warehouse']);
        $location = new WarehouseProductLocation($location);
        $location->id_product = $product->id;
        $location->id_product_attribute = 0;
        $location->id_warehouse = $line['warehouse'];
        $location->save();

        $stock = DB::getInstance()->getValue("SELECT `id_stock` FROM `" . _DB_PREFIX_ . "stock` WHERE `id_product` = {$product->id} AND `id_warehouse` = {$line['warehouse']}");
        $stock = new Stock($stock);
        $stock->id_product = $product->id;
        $stock->id_product_attribute = 0;
        $stock->id_warehouse = $line['warehouse'];
        $stock->physical_quantity = $line['count'];
        $stock->usable_quantity = $line['count'];
        $stock->price_te = 0;
        $stock->save();

        $available = DB::getInstance()->getValue("SELECT `id_stock_available` FROM `". _DB_PREFIX_ . "stock_available` WHERE `id_product` = {$product->id} AND `id_shop` = " . Context::getContext()->shop->id);
        $available = new StockAvailable($available);
        $available->id_product = $product->id;
        $available->id_product_attribute = 0;
        //$available->id_shop = Context::getContext()->shop->id;
        $available->quantity = StockManagerFactory::getManager()->getProductPhysicalQuantities($product->id, 0);
        $available->save();

        StockAvailable::setProductDependsOnStock($product->id, true, null);
        StockAvailable::setProductOutOfStock($product->id, 1, null); //allow

        while(strlen($line['reference']) < 9) {
            $line['reference'] = '0' . $line['reference'];
        }

        if (file_exists(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg')) {
            $product->deleteImages();

            $image = new Image();
            $image->id_product = $product->id;
            $image->cover = 1;
            $image->position = 0;
            $image->save();

            $name = $image->getPathForCreation();

            copy(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg', $name.'.'.$image->image_format);

            $types = ImageType::getImagesTypes('products');
            foreach ($types as $type) {
                ImageManager::resize(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg', $name . '-' . $type['name'] . '.' . $image->image_format, $type['width'], $type['height'], $image->image_format);
            }
        }

        /*
        Db::getInstance()->update('stock_available', array(
           'depends_on_stock' => (int)1, //1 - доступное количество на основе ASM. 0 - указывается вручную
            'out_of_stock' => (int)1, //1-allow
        ), 'id_product='.$product->id.'');
         $affrows = Db::getInstance()->Affected_Rows();
         var_dump($affrows);
         */

        //var_dump($product->reference);
        //echo "<br/><br/><br/><br/>";
    }
Example #2
0
 /**
  * Update categorie
  */
 private function eeweeProductUpdateCateg()
 {
     $p = new Product($this->id_product);
     $p->updateCategories(array(2, 3));
 }
 public function handleConfirm($update = false)
 {
     global $currentIndex, $cookie, $smarty;
     $products_to_import = array();
     $defaultLanguageId = (int) Configuration::get('PS_LANG_DEFAULT');
     $file_path = Tools::getValue('current_file');
     $overwrite_imgs = Tools::getValue("overwrite_imgs");
     //$file_path = '/Users/rohit/webroot/indusdiva/admin12/product-uploads/upload_sheet_1.csv';
     $f = fopen($file_path, 'r');
     $file_error = false;
     if ($f) {
         //discard header
         $line = fgetcsv($f);
         while ($line = fgetcsv($f)) {
             //ignore empty lines
             if (empty($line)) {
                 continue;
             }
             //trim data
             foreach ($line as $key => $value) {
                 $line[$key] = trim($value);
             }
             $id_product = $line[0];
             $images = $line[1];
             $product_name = $line[2];
             $fabric = $line[3];
             $color = $line[4];
             $mrp = $line[5];
             $supplier_code = $line[6];
             $reference = $line[7];
             $location = $line[8];
             $length = $line[9];
             $width = $line[10];
             $blouse_length = $line[11];
             $garment_type = $line[12];
             $work_type = $line[13];
             $weight = $line[14];
             $description = $line[15];
             $other_info = $line[16];
             $wash_care = $line[17];
             $shipping_estimate = $line[18];
             $supplier_price = $line[19];
             $manufacturer = $line[20];
             $categories = explode(",", $line[21]);
             $tax_rule = $line[22];
             $quantity = $line[23];
             $active = $line[24];
             $discount = $line[25];
             $tags = $line[26];
             $kameez_style = $line[27];
             $salwar_style = $line[28];
             $sleeves = $line[29];
             $customizable = $line[30];
             $generic_color = $line[31];
             $skirt_length = $line[32];
             $dupatta_length = $line[33];
             $stone = $line[34];
             $plating = $line[35];
             $material = $line[36];
             $dimensions = $line[37];
             $look = $line[38];
             $as_shown = isset($line[39]) && !empty($line[39]) ? intval($line[39]) : 0;
             $id_sizechart = isset($line[40]) && !empty($line[40]) ? intval($line[40]) : 0;
             $is_exclusive = isset($line[41]) && !empty($line[41]) ? intval($line[41]) : 0;
             $handbag_occasion = isset($line[42]) && !empty($line[42]) ? $line[42] : null;
             $handbag_style = isset($line[43]) && !empty($line[43]) ? $line[43] : null;
             $handbag_material = isset($line[44]) && !empty($line[44]) ? $line[44] : null;
             $images = explode(",", $images);
             $error = false;
             //validate fields
             if (!Validate::isFloat($mrp)) {
                 $error = 'MRP should be a number: ' . trim($reference);
             } elseif (!Validate::isFloat($supplier_price)) {
                 $error = 'Supplier Price should be a number: ' . trim($reference);
             }
             $importCategories = array();
             if (is_array($categories)) {
                 $categories = array_unique($categories);
                 foreach ($categories as $category) {
                     $category = intval(trim($category));
                     if (empty($category)) {
                         continue;
                     }
                     if (!is_numeric($category) || !Category::categoryExists($category)) {
                         $error = 'Category does not exist: ' . $category;
                     }
                     $importCategories[] = $category;
                 }
             } else {
                 $error = 'Atleast one category required: ' . trim($reference);
             }
             if (!Validate::isFloat($weight)) {
                 $error = 'Weight has to be a number: ' . trim($reference);
             }
             if (!empty($manufacturer) && (!is_numeric($manufacturer) || !Manufacturer::manufacturerExists((int) $manufacturer))) {
                 $error = 'Manufacturer does not exist';
             }
             if ($quantity && !is_numeric($quantity) || $discount && !is_numeric($discount)) {
                 $error = 'Quantity and discount should be numbers: ' . trim($reference);
             }
             if (!Validate::isLoadedObject(new TaxRulesGroup($tax_rule))) {
                 $error = 'Tax rate invalid: ' . trim($reference);
             }
             if (!$update) {
                 $sql = "SELECT `reference`\n\t\t\t\t\t\t\tFROM " . _DB_PREFIX_ . "product p\n\t\t\t\t\t\t\tWHERE p.`reference` = '" . $reference . "'";
                 $row = Db::getInstance()->getRow($sql);
                 if (isset($row['reference'])) {
                     $error = "Duplicate indusdiva code : " . trim($reference);
                 }
             }
             //check for souring price
             if ($supplier_price > $mrp / 1.2) {
                 $error = "MRP too low : " . trim($reference);
             }
             //check for images
             if (!$update || $overwrite_imgs == "on") {
                 foreach ($images as $image_name) {
                     $image_name = trim($image_name);
                     $image_path = IMAGE_UPLOAD_PATH . $image_name;
                     if (!empty($image_name) && !file_exists($image_path)) {
                         $error = "Image not found for: " . trim($reference) . ", Image Name: " . $image_name;
                         break;
                     }
                 }
             }
             $vendor_code = substr($reference, 0, 6);
             $sql = "select id_supplier from ps_supplier where code = '{$vendor_code}'";
             $row = Db::getInstance()->getRow($sql);
             if (!isset($row['id_supplier'])) {
                 $error = "Vendor Details not found for : " . trim($reference);
             } else {
                 $id_supplier = $row['id_supplier'];
             }
             //For sudarshan, supplier_code (vendor product code) is mandatory
             if (false) {
                 //(int) $id_supplier === 2 ) {
                 if (empty($supplier_code)) {
                     $error = "Reference: {$reference} -- Supplier Code is Mandatory for Vendor {$vendor_code}";
                 } else {
                     if (strpos("::", ${$supplier_code}) === false) {
                         $error = "Reference: {$reference} -- Supplier Code:{$supplier_code} is not in DESIGN_NO::ITEM_CODE format for Vendor {$vendor_code}";
                     }
                 }
             }
             if (!$error) {
                 if ($update && !empty($id_product)) {
                     $product = new Product((int) $id_product);
                     if (!Validate::isLoadedObject($product)) {
                         $error = "Error loading the product: " . $id_product;
                         return;
                     }
                 } elseif (!$update) {
                     $product = new Product();
                 }
                 $product->id_tax_rules_group = $tax_rule;
                 $product->reference = $reference;
                 $product->id_supplier = $id_supplier;
                 $product->location = $location;
                 $product->tax_rate = TaxRulesGroup::getTaxesRate((int) $product->id_tax_rules_group, Configuration::get('PS_COUNTRY_DEFAULT'), 0, 0);
                 if (isset($manufacturer) and is_numeric($manufacturer) and Manufacturer::manufacturerExists((int) $manufacturer)) {
                     $product->id_manufacturer = $manufacturer;
                 }
                 $product->price = (double) $mrp;
                 $product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', '');
                 $product->id_category = $importCategories;
                 $product->id_category_default = 1;
                 $product->name = array();
                 $product->name[$defaultLanguageId] = $product_name;
                 $product->description_short = array();
                 $product->description_short[$defaultLanguageId] = $style_tips;
                 $product->description = array();
                 $product->description[$defaultLanguageId] = $description;
                 $link_rewrite = Tools::link_rewrite($product->name[$defaultLanguageId]);
                 $product->link_rewrite = array();
                 $product->link_rewrite[$defaultLanguageId] = $link_rewrite;
                 $product->quantity = $quantity ? intval($quantity) : 0;
                 if ($discount && is_numeric($discount)) {
                     $product->discount = $discount;
                 }
                 if (!empty($tags)) {
                     $product->tags = $tags;
                 }
                 $product->weight = is_numeric($weight) ? $weight : 0;
                 $product->width = is_numeric($width) ? $width : 0;
                 $product->height = is_numeric($length) ? $length : 0;
                 $product->supplier_reference = $supplier_code;
                 $product->wholesale_price = $supplier_price ? (double) $supplier_price : 0;
                 $product->active = $active == 1 ? 1 : 0;
                 $product->images = $images;
                 $product->fabric = $fabric;
                 $product->color = $color;
                 $product->generic_color = $generic_color;
                 $product->garment_type = $garment_type;
                 $product->work_type = $work_type;
                 $product->blouse_length = $blouse_length ? $blouse_length : ' ';
                 $product->wash_care = $wash_care ? $wash_care : ' ';
                 $product->other_info = $other_info ? $other_info : ' ';
                 $product->shipping_estimate = $shipping_estimate ? $shipping_estimate : ' ';
                 $product->is_customizable = $customizable == 1 ? 1 : 0;
                 $product->kameez_style = $kameez_style;
                 $product->salwar_style = $salwar_style;
                 $product->sleeves = $sleeves;
                 $product->skirt_length = $skirt_length;
                 $product->dupatta_length = $dupatta_length;
                 $product->stone = $stone;
                 $product->plating = $plating;
                 $product->material = $material;
                 $product->dimensions = $dimensions;
                 $product->look = $look;
                 $product->as_shown = $as_shown;
                 $product->id_sizechart = $id_sizechart;
                 $product->is_exclusive = $is_exclusive;
                 $product->handbag_occasion = $handbag_occasion;
                 $product->handbag_style = $handbag_style;
                 $product->handbag_material = $handbag_material;
                 $product->indexed = 0;
                 $products_to_import[] = $product;
             } else {
                 $smarty->assign('error', $error);
                 return;
                 $file_error = true;
             }
         }
         if (!$file_error) {
             $added_product_ids = array();
             foreach ($products_to_import as $product) {
                 $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true);
                 $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true);
                 if ($fieldError === true and $langFieldError === true) {
                     // check quantity
                     if ($product->quantity == NULL) {
                         $product->quantity = 0;
                     }
                     // If no id_product or update failed
                     if ($update && $product->id) {
                         $res = $product->update();
                     } else {
                         $res = $product->add();
                     }
                     $added_product_ids[] = $product->id;
                 }
                 if (isset($product->discount) && $product->discount > 0) {
                     SpecificPrice::deleteByProductId((int) $product->id);
                     $specificPrice = new SpecificPrice();
                     $specificPrice->id_product = (int) $product->id;
                     $specificPrice->id_shop = (int) Shop::getCurrentShop();
                     $specificPrice->id_currency = 0;
                     $specificPrice->id_country = 0;
                     $specificPrice->id_group = 0;
                     $specificPrice->from_quantity = 1;
                     $specificPrice->reduction = $product->discount / 100;
                     $specificPrice->reduction_type = 'percentage';
                     $specificPrice->from = '2012-01-01 00:00:00';
                     $specificPrice->to = '2016-01-01 00:00:00';
                     $specificPrice->price = $product->price;
                     $specificPrice->add();
                 }
                 if (isset($product->tags) and !empty($product->tags)) {
                     // Delete tags for this id product, for no duplicating error
                     Tag::deleteTagsForProduct($product->id);
                     $tag = new Tag();
                     $tag->addTags($defaultLanguageId, $product->id, $tags);
                 }
                 if (isset($product->images) and is_array($product->images) and sizeof($product->images) and !$update || $overwrite_imgs == "on") {
                     $product->deleteImages();
                     $first_image = true;
                     foreach ($product->images as $image_name) {
                         $image_name = trim($image_name);
                         $image_path = IMAGE_UPLOAD_PATH . $image_name;
                         if (!empty($image_name)) {
                             $image = new Image();
                             $image->id_product = (int) $product->id;
                             $image->position = Image::getHighestPosition($product->id) + 1;
                             $image->cover = $first_image;
                             $image->legend[$defaultLanguageId] = $product->name[$defaultLanguageId];
                             if (($fieldError = $image->validateFields(false, true)) === true and ($langFieldError = $image->validateFieldsLang(false, true)) === true and $image->add()) {
                                 if (!self::copyImg($product->id, $image->id, $image_path)) {
                                     $_warnings[] = Tools::displayError('Error copying image: ') . $image_path;
                                 } else {
                                     //delete the original image
                                     @unlink($image_path);
                                 }
                             } else {
                                 $_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                                 $_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
                             }
                         }
                         $first_image = false;
                     }
                 }
                 if (isset($product->id_category)) {
                     $product->updateCategories(array_map('intval', $product->id_category));
                 }
                 $this->addFeature($product->id, 'fabric', $product->fabric);
                 $this->addFeature($product->id, 'color', $product->color);
                 $this->addFeature($product->id, 'garment_type', $product->garment_type);
                 $this->addFeature($product->id, 'work_type', $product->work_type);
                 $this->addFeature($product->id, 'blouse_length', $product->blouse_length);
                 $this->addFeature($product->id, 'wash_care', $product->wash_care);
                 $this->addFeature($product->id, 'other_info', $product->other_info);
                 // to avoid type errors in the catalog sheet - construct the string here again
                 $shipping_sla = (int) preg_replace('/\\D/', '', $product->shipping_estimate);
                 $shipping_estimate_str = "";
                 if ($shipping_sla > 0) {
                     $shipping_estimate_str = $shipping_sla === 1 ? "Ready to be shipped in 1 day" : "Ready to be shipped in {$shipping_sla} days";
                 }
                 $this->addFeature($product->id, 'shipping_estimate', $shipping_estimate_str);
                 $this->addFeature($product->id, 'kameez_style', $product->kameez_style);
                 $this->addFeature($product->id, 'salwar_style', $product->salwar_style);
                 $this->addFeature($product->id, 'sleeves', $product->sleeves);
                 $this->addFeature($product->id, 'generic_color', $product->generic_color);
                 $this->addFeature($product->id, 'skirt_length', $product->skirt_length);
                 $this->addFeature($product->id, 'dupatta_length', $product->dupatta_length);
                 $this->addFeature($product->id, 'stone', $product->stone);
                 $this->addFeature($product->id, 'plating', $product->plating);
                 $this->addFeature($product->id, 'material', $product->material);
                 $this->addFeature($product->id, 'dimensions', $product->dimensions);
                 $this->addFeature($product->id, 'look', $product->look);
                 $this->addFeature($product->id, 'handbag_occasion', $product->handbag_occasion);
                 $this->addFeature($product->id, 'handbag_style', $product->handbag_style);
                 $this->addFeature($product->id, 'handbag_material', $product->handbag_material);
             }
             $smarty->assign("products_affected", $products_to_import);
             //reindex the products
             SolrSearch::updateProducts($added_product_ids);
             $smarty->assign("is_update", $update);
         } else {
             $smarty->assign('file_error', 1);
         }
     } else {
         $smarty->assign('error_reading', 1);
     }
 }
Example #4
0
<script type="text/javascript" src="<?php 
echo $_tmconfig['tm_js_dir'];
?>
fileuploader.js"></script>
<script type="text/javascript" src="<?php 
echo $_tmconfig['tm_js_dir'];
?>
jquery/jquery.tablednd_0_5.js"></script>
<!--//head-->
<div class="col-md-12">
<?php 
if (isset($_POST['sveProduct']) && Tools::getRequest('sveProduct') == 'add') {
    $product = new Product();
    $product->copyFromPost();
    if ($product->add()) {
        if (!$product->updateCategories($_POST['categoryBox']) or !$product->addToTags($_POST['tags']) or !$product->updateAttribute($_POST['attribute_items'])) {
            $product->_errors = '添加产品内容时发生了一个错误';
        }
    }
    if (is_array($product->_errors) and count($product->_errors) > 0) {
        $errors = $product->_errors;
    } else {
        $_GET['id'] = $product->id;
        echo '<div class="conf">添加产品成功</div>';
    }
}
if (isset($_GET['id'])) {
    $id = (int) $_GET['id'];
    $obj = new Product($id);
    $images = $obj->getImages($id);
}
Example #5
0
 private function upsert($line)
 {
     $product = DB::getInstance()->getValue("SELECT `id_product` FROM `" . _DB_PREFIX_ . "product` WHERE `reference` LIKE '{$line['reference']}'");
     $product = new Product($product);
     $product->reference = $line['reference'];
     $product->name = array('1' => $line['name'], '2' => $line['name'], '3' => $line['name']);
     $product->description = array('1' => $line['description'], '2' => $line['description'], '3' => $line['description']);
     $product->description_short = array('1' => $line['description_short'], '2' => $line['description_short'], '3' => $line['description_short']);
     $product->link_rewrite = array('1' => Tools::link_rewrite($line['name']), '2' => Tools::link_rewrite($line['name']), '3' => Tools::link_rewrite($line['name']));
     $product->available_now = array('1' => "Есть в наличии", '2' => "Есть в наличии", '3' => "Есть в наличии");
     $product->id_category_default = $line['category'];
     $product->quantity = (int) $line['count'];
     $product->advanced_stock_management = 1;
     //использовать Advanced Stock management
     $product->depends_on_stock = 1;
     //1 - доступное количество на основе ASM. 0 - указывается вручную
     $product->out_of_stock = 1;
     //2 - как в Preferences product. 1 - allow (Как в Preferences - не дает заказать товар на сайте)
     $product->price = $line['price'];
     $product->weight = $line['weight'] / 1000;
     $product->id_tax_rules_group = $line['id_tax'];
     $product->save();
     $product->updateCategories(array($line['category']));
     $product->deleteFeatures();
     if ($line['author']) {
         $id_feature_value = FeatureValue::addFeatureValueImport(9, $line['author'], null, Configuration::get('PS_LANG_DEFAULT'));
         Product::addFeatureProductImport($product->id, 9, $id_feature_value);
     }
     if ($line['year']) {
         $id_feature_value = FeatureValue::addFeatureValueImport(10, $line['year'], null, Configuration::get('PS_LANG_DEFAULT'));
         Product::addFeatureProductImport($product->id, 10, $id_feature_value);
     }
     if ($line['paperback']) {
         if ($line['paperback'] == 1) {
             $id_feature_value = 1;
         } else {
             $id_feature_value = 2;
         }
         //$id_feature_value = FeatureValue::addFeatureValueImport(11, $line['paperback'], null, Configuration::get('PS_LANG_DEFAULT'));
         Product::addFeatureProductImport($product->id, 11, $id_feature_value);
         //1 - id значения "твёрдый переплёт" у харакатеристики "Переплёт", 149226 - мягкая обложка
     }
     if ($line['pages']) {
         $id_feature_value = FeatureValue::addFeatureValueImport(12, $line['pages'], null, Configuration::get('PS_LANG_DEFAULT'), true);
         Product::addFeatureProductImport($product->id, 12, $id_feature_value);
     }
     /*
     if ($line['weight']) {
         $id_feature_value = FeatureValue::addFeatureValueImport(4, $line['weight'], null, Configuration::get('PS_LANG_DEFAULT'), true);
         Product::addFeatureProductImport($product->id, 4, $id_feature_value);
     }
     */
     if ($line['isbn']) {
         $id_feature_value = FeatureValue::addFeatureValueImport(13, $line['isbn'], null, Configuration::get('PS_LANG_DEFAULT'), true);
         Product::addFeatureProductImport($product->id, 13, $id_feature_value);
     }
     if ($line['publishing']) {
         $id_feature_value = FeatureValue::addFeatureValueImport(14, $line['publishing'], null, Configuration::get('PS_LANG_DEFAULT'), true);
         Product::addFeatureProductImport($product->id, 14, $id_feature_value);
     }
     $location = WarehouseProductLocation::getIdByProductAndWarehouse($product->id, 0, $line['warehouse']);
     $location = new WarehouseProductLocation($location);
     $location->id_product = $product->id;
     $location->id_product_attribute = 0;
     $location->id_warehouse = $line['warehouse'];
     $location->save();
     /*
     $stock = DB::getInstance()->getValue("SELECT `id_stock` FROM `" . _DB_PREFIX_ . "stock` WHERE `id_product` = {$product->id} AND `id_warehouse` = {$line['warehouse']}");
     $stock = new Stock($stock);
     $stock->id_product = $product->id;
     $stock->id_product_attribute = 0;
     $stock->id_warehouse = $line['warehouse'];
     $stock->physical_quantity = $line['count'];
     $stock->usable_quantity = $line['count'];
     $stock->price_te = 0;
     $stock->save();
     */
     //установить зависимость количества товара от остатка на складе для каждого магазинеа
     //исходим из того, что в настройках Мультимагазина остаток НЕ единый на все магазины
     $id_product = $product->id;
     $depends_on_stock = true;
     $out_of_stock = 1;
     //2 - как в Preferences product. 1 - allow (ставь 1, т.к. 2 (как в Preferences) не дает заказать товар на сайте)
     for ($id_shop = 1; $id_shop <= 4; $id_shop++) {
         StockAvailable::setProductDependsOnStock($id_product, $depends_on_stock, $id_shop);
     }
     //разрешить или запретить продажу товара без остатка
     /*  для магазина 2,3 запретить продажу, если нет в наличии. out_of_stock = 0
             2   Second shop Gelikon 
             3   First shop Gelikon
         */
     $out_of_stock = 0;
     StockAvailable::setProductOutOfStock($id_product, $out_of_stock, 2);
     StockAvailable::setProductOutOfStock($id_product, $out_of_stock, 3);
     /*
         Для online и заказов по телефону разрешить заказ товара, которого нет в наличии
         1 Gelikon DE online
         4 Заказы по телефону
     */
     $out_of_stock = 1;
     StockAvailable::setProductOutOfStock($id_product, $out_of_stock, 1);
     StockAvailable::setProductOutOfStock($id_product, $out_of_stock, 4);
     //Добавить партию товара на склад с записью в журнал движения товаров
     $id_product = $product->id;
     $id_product_attribute = 0;
     $id_stock_mvt_reason = 1;
     $price = 1;
     $id_currency = 1;
     $id_warehouse = $line['warehouse'];
     $warehouse = new Warehouse($id_warehouse);
     $stock_manager = StockManagerFactory::getManager();
     if ($line['count'] != 0) {
         //echo "добавляем товар ";
         $is_usable = true;
         $quantity = $line['count'];
         // add stock
         if ($stock_manager->addProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $price, $is_usable)) {
             StockAvailable::synchronize($id_product);
         } else {
             $errors[] = Tools::displayError('An error occurred. No stock was added.');
         }
     }
     /*
             $available = DB::getInstance()->getValue("SELECT `id_stock_available` FROM `". _DB_PREFIX_ . "stock_available` WHERE `id_product` = {$product->id} AND `id_shop` = " . Context::getContext()->shop->id);
             $available = new StockAvailable($available);
             $available->id_product = $product->id;
             $available->id_product_attribute = 0;
             //$available->id_shop = Context::getContext()->shop->id;
             $available->quantity = StockManagerFactory::getManager()->getProductPhysicalQuantities($product->id, 0);
             $available->save();
     
             StockAvailable::setProductDependsOnStock($product->id, true, null);
             StockAvailable::setProductOutOfStock($product->id, 1, null); //allow
     */
     while (strlen($line['reference']) < 9) {
         $line['reference'] = '0' . $line['reference'];
     }
     if (file_exists(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg')) {
         $product->deleteImages();
         $image = new Image();
         $image->id_product = $product->id;
         $image->cover = 1;
         $image->position = 0;
         $image->save();
         $name = $image->getPathForCreation();
         copy(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg', $name . '.' . $image->image_format);
         $types = ImageType::getImagesTypes('products');
         foreach ($types as $type) {
             ImageManager::resize(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg', $name . '-' . $type['name'] . '.' . $image->image_format, $type['width'], $type['height'], $image->image_format);
         }
     }
     /*
             Db::getInstance()->update('stock_available', array(
       'depends_on_stock' => (int)1, //1 - доступное количество на основе ASM. 0 - указывается вручную
        'out_of_stock' => (int)1, //1-allow
             ), 'id_product='.$product->id.'');
     $affrows = Db::getInstance()->Affected_Rows();
     var_dump($affrows);
     */
     //var_dump($product->reference);
     //echo "<br/><br/><br/><br/>";
 }
Example #6
0
    private function upsert($line) {
        $product = DB::getInstance()->getValue("SELECT `id_product` FROM `" . _DB_PREFIX_ . "product` WHERE `reference` LIKE '{$line['reference']}'");
        $product = new Product($product);

        $product->reference = $line['reference'];
        $product->name = array(
            '1' => $line['name'],
            '2' => $line['name'],
            '3' => $line['name']
        );
        $product->description = array(
            '1' => $line['description'],
            '2' => $line['description'],
            '3' => $line['description']
        );
        $product->description_short = array(
            '1' => $line['description_short'],
            '2' => $line['description_short'],
            '3' => $line['description_short']
        );
        $product->link_rewrite = array(
            '1' => Tools::link_rewrite($line['name']),
            '2' => Tools::link_rewrite($line['name']),
            '3' => Tools::link_rewrite($line['name'])
        );
        $product->available_now = array(
            '1' => "Есть в наличии",
            '2' => "Есть в наличии",
            '3' => "Есть в наличии"
        );
        $product->id_category_default = $line['category'];
        $product->quantity = $line['count'];
        $product->price = $line['price'];
        $product->weight = $line['weight'] / 1000;
        $product->id_tax_rules_group = $line['id_tax'];

        $product->save();

        $product->updateCategories(array($line['category']));

        $product->deleteFeatures();
        if ($line['author']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(9, $line['author'], null, Configuration::get('PS_LANG_DEFAULT'));
            Product::addFeatureProductImport($product->id, 9, $id_feature_value);
        }

        if ($line['year']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(10, $line['year'], null, Configuration::get('PS_LANG_DEFAULT'));
            Product::addFeatureProductImport($product->id, 10, $id_feature_value);
        }

        if ($line['paperback']) {
            Product::addFeatureProductImport($product->id, 11, 70);
        }

        if ($line['pages']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(12, $line['pages'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 12, $id_feature_value);
        }

        /*
        if ($line['weight']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(4, $line['weight'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 4, $id_feature_value);
        }
        */

        if ($line['isbn']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(13, $line['isbn'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 13, $id_feature_value);
        }

        if ($line['publishing']) {
            $id_feature_value = FeatureValue::addFeatureValueImport(14, $line['publishing'], null, Configuration::get('PS_LANG_DEFAULT'), true);
            Product::addFeatureProductImport($product->id, 14, $id_feature_value);
        }

        $stock = DB::getInstance()->getValue("SELECT `id_stock` FROM `" . _DB_PREFIX_ . "stock` WHERE `id_product` = {$product->id} AND `id_warehouse` = {$line['warehouse']}");
        $stock = new Stock($stock);
        $stock->id_product = $product->id;
        $stock->id_product_attribute = 0;
        $stock->id_warehouse = $line['warehouse'];
        $stock->physical_quantity = $line['count'];
        $stock->usable_quantity = $line['count'];
        $stock->price_te = 0;
        $stock->save();

        $available = DB::getInstance()->getValue("SELECT `id_stock_available` FROM `". _DB_PREFIX_ . "stock_available` WHERE `id_product` = {$product->id} AND `id_shop` = " . Context::getContext()->shop->id);
        $available = new StockAvailable($available);
        $available->id_product = $product->id;
        $available->id_product_attribute = 0;
        $available->id_shop = Context::getContext()->shop->id;
        $available->quantity = $line['count'];
        $available->save();

        $location = WarehouseProductLocation::getIdByProductAndWarehouse($product->id, 0, $line['warehouse']);
        $location = new WarehouseProductLocation($location);
        $location->id_product = $product->id;
        $location->id_product_attribute = 0;
        $location->id_warehouse = $line['warehouse'];
        $location->save();

        while(strlen($line['reference']) < 9) {
            $line['reference'] = '0' . $line['reference'];
        }

        if (file_exists(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg')) {
            $product->deleteImages();

            $image = new Image();
            $image->id_product = $product->id;
            $image->cover = 1;
            $image->position = 0;
            $image->save();

            $name = $image->getPathForCreation();

            copy(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg', $name.'.'.$image->image_format);

            $types = ImageType::getImagesTypes('products');
            foreach ($types as $type) {
                ImageManager::resize(_PS_ROOT_DIR_ . '/upload/import/' . $line['reference'] . '.jpg', $name . '-' . $type['name'] . '.' . $image->image_format, $type['width'], $type['height'], $image->image_format);
            }
        }
    }
Example #7
0
 public function loadProduct($id_category, $fields)
 {
     $product = new Product();
     $product->copyFromPost();
     $product->id_category_default = (int) end($id_category);
     $product->id_brand = (int) $fields['id_brand'];
     $product->price = (double) $fields['price'];
     $product->special_price = (double) $fields['old_price'];
     $product->ean13 = pSQL($fields['ean13']);
     $product->weight = 1;
     $product->in_stock = 1;
     $product->quantity = (double) $fields['quantity'];
     $product->name = pSQL($fields['name']);
     $product->rewrite = pSQL(preg_replace("/[^-0-9a-zA-Z]+/", "", str_replace(' ', '-', trim($fields['name']))));
     $product->description = pSQL($fields['description'], true);
     $product->active = 1;
     if ($product->add()) {
         $product->updateCategories($id_category);
         return (int) $product->id;
     } else {
         print_r($product->_errors);
     }
 }