Guest Posted March 29, 2018 Share Posted March 29, 2018 (edited) Hi, when I want to programmatically add a product, it will not show me the product name.The product is uploaded, but it does not have the name, url and prices, the number of stock. How do I enter the product name to make it work? How do I write code for multistores? Thanks . ini_set('display_errors', 1); error_reporting(E_ALL); header('Content-Type: text/html; charset=utf-8'); define('_PS_ADMIN_DIR_', getcwd()); include(_PS_ADMIN_DIR_.'/../config/config.inc.php'); require_once('../classes/stock/StockAvailable.php'); include('/init.php'); function NewProduct(){ $product = new Product(); $product->reference = $product_code; // 1234A - 25 $product->name = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $name_product); // Unicode ěščřžý , éůú $product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => Tools::str2url($name_product)); $product->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $sh_description); // unicode $product->description = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $description); // unicode $product->id_category_default = 12; $product->category = 12; $product->redirect_type = '404'; $product->minimal_quantity = 1; $product->show_price = 1; $product->on_sale = 0; $product->online_only = 1; $price = round(($mo_price / '1.25'),6); // '1.25' is VAT $product->price = $price; $product->wholesale_price = '0.000000'; $product->ean13 = $ean13; $product->quantity = $quantity; // 30 $product->Add(); $product->addToCategories(array(2)); $shops = Shop::getShops(true, null, true); $image = new Image(); $image->id_product = $id_product; $image->position = Image::getHighestPosition($id_product) + 1; $image->cover = true; if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add()) { $image->associateTo($shops); if (!copyImg2($id_product, $image->id, $image_url, 'products', false)) { $image->delete(); } } } function copyImg2($id_entity, $id_image, $url, $entity = 'products', $regenerate = true) { $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import'); $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES')); switch ($entity) { default: case 'products': $image_obj = new Image($id_image); $path = $image_obj->getPathForCreation(); break; case 'categories': $path = _PS_CAT_IMG_DIR_ . (int) $id_entity; break; case 'manufacturers': $path = _PS_MANU_IMG_DIR_ . (int) $id_entity; break; case 'suppliers': $path = _PS_SUPP_IMG_DIR_ . (int) $id_entity; break; } $url = str_replace(' ', '%20', trim($url)); // Evaluate the memory required to resize the image: if it's too much, you can't resize it. if (!ImageManager::checkImageMemoryLimit($url)) return false; // 'file_exists' doesn't work on distant file, and getimagesize makes the import slower. // Just hide the warning, the processing will be the same. if (Tools::copy($url, $tmpfile)) { ImageManager::resize($tmpfile, $path . '.jpg'); $images_types = ImageType::getImagesTypes($entity); if ($regenerate) foreach ($images_types as $image_type) { ImageManager::resize($tmpfile, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']); if (in_array($image_type['id_image_type'], $watermark_types)) Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity)); } } else { unlink($tmpfile); return false; } unlink($tmpfile); return true; } Edited March 29, 2018 by Guest (see edit history) Link to comment Share on other sites More sharing options...
selectshop.at Posted March 29, 2018 Share Posted March 29, 2018 Perhaps this helps: https://www.prestashop.com/forums/topic/646555-create-new-products-with-php-with-automatic-filled-multilanguage-mandatory-fields/ Link to comment Share on other sites More sharing options...
Guest Posted March 29, 2018 Share Posted March 29, 2018 The link is not the right solution.Can you repair the code to work? The error is only in this part of the code. Thanks $product->name = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $name_product); // Unicode ěščřžý , éůú $product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => Tools::str2url($name_product)); $product->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $sh_description); // unicode $product->description = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $description); // unicode Link to comment Share on other sites More sharing options...
Guest Posted March 30, 2018 Share Posted March 30, 2018 Ok, solved. I had a created procedure I loaded in XML processing.Unfortunately, empty items were loaded there, causing problems. Now I'm trying to load the product into categories when I do not know the ID, but I know only the name? $product->id_category_default = 'home'; $product->category = ['home','woomen','shirt']; I also need to upload multiple images, I do not know if multiple images can be inserted into $image_url as $image_url = [image1.jpg, image2.jpg, image3.jpg]; $image->associateTo($shops); if (!copyImg2($id_product, $image->id, $image_url, 'products', false)) { $image->delete(); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now