Jump to content

How to add Product through script in prestashop


Recommended Posts

Instead of copying someone else's script, my suggestion would be to have a complete look at the Product class. You can find out all the properties it accepts, fill them out, and then use the add() method to add it to Prestashop :)

You can also have a look at AdminProductsCOntroller.php to see what happens when you hit 'save' in the back office, and mimic the functionality ;)

Link to comment
Share on other sites

Hello Nemo1,

 

 

I have try to do same but not propertly working

through this I can not able to fill name,image or more.

here is my script,please look at this and let me know any suggestion.

 

<?php
echo dirname(__FILE__);
include_once 'config/config.inc.php';
include(dirname(__FILE__).'/init.php');
$defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));  
/* Add a new product */
$languages = Language::getLanguages();
foreach ($languages as $language) {
         $object->name[$language['id_lang']] = 'ssssssssssssssss';
          $object->meta_keywords[$language['id_lang']] = 'sssssssss';
}
$object = new Product();
$object->price = 22;
$object->id_tax_rules_group = 1;
//$object->name[Configuration::get('PS_LANG_DEFAULT')] = 'ssssssssss';
$object->reference= 'hbvvh';
$object->quantity = 100;
$object->id_manufacturer = 0;
$object->id_supplier = 0;
$object->quantity = 1;
$object->minimal_quantity = 1;
$object->additional_shipping_cost = 0;
$object->wholesale_price = 0;
$object->ecotax = 0;
$object->width = 0;
$object->height = 0;
$object->depth = 0;
$object->weight = 0;
$object->out_of_stock = 0;
$object->active = 1;
//$object->id_category_default = 18;
//$object->category = 18;
$object->available_for_order = 0;
$object->show_price = 1;
$object->on_sale = 0;
$object->online_only = 1;
$object->meta_keywords = 'test';
//if($object->save())
    $object->add();
echo "produit ajouté";

exit;
?>

 

run this script and check in admin.price is updated

 

Thanks
 

Link to comment
Share on other sites

For all translatable fields, use this very same procedure

foreach ($languages as $language) {
         $object->name[$language['id_lang']] = 'ssssssssssssssss';
          $object->meta_keywords[$language['id_lang']] = 'sssssssss';
}

As for images, it's a bit more complex, and it involves using something like this (url is the image url):

$url = str_replace(' ', '%20', $url);

					$image = new Image();
					$image->id_product = (int)$object->id;
					$image->position = Image::getHighestPosition($object->id) + 1;
					$image->cover = false;
					// file_exists doesn't work with HTTP protocol
					if (@fopen($url, 'r') == false)
						$error = true;
					else if (($field_error = $image->validateFields(false, true)) === true &&
						($lang_field_error = $image->validateFieldsLang(false, true)) === true && $image->add())
					{
						// associate image to selected shops
						$image->associateTo($shops);
						if (!self::copyImg($object->id, $image->id, $url))
						{
							$image->delete();
							$this->errors[] = sprintf(Tools::displayError('Error copying image: %s'), $url);
						}
					}
					else
						$error = true;

And the method:

	protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products')
	{
		$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;
		}
		$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 make the import slower.
		// Just hide the warning, the traitment will be the same.
		if (@copy($url, $tmpfile))
		{
			ImageManager::resize($tmpfile, $path.'.jpg');
			$images_types = ImageType::getImagesTypes($entity);
			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;
	}
Link to comment
Share on other sites

×
×
  • Create New...