Jump to content

[Prestashop 1.7.5]Import product into prestashop's database


anis42

Recommended Posts

Hi,

This is my problem, I would like to import into my Prestashop's database, my products that I get from my supplier’s API, I get all this back in JSON, I managed to import data into a test database on phpmyadmin without a problem, the problem with the Prestashop bdd is that then I make inserts into ps_product they don’t appear in either the BO or the FO, after several searches I realized that I had to insert data into several tables, but I don’t know which ones.

I also found out that Prestashop had a db class that could make it easier for me to insert data into my database, but I don’t understand how it works.

Well, that’s a lot, I hope I’ve explained myself well, otherwise don’t hesitate to tell me I’ll rephrase.
And sorry for my bad english.

Thanks.
Link to comment
Share on other sites

There are some free modules in the https://www.prestashop.com/forums/forum/144-free-modules-themes/ section, not sure if they import json but if worth a look.

The imports has to be performed using PS classes as either the default CSV import  or these scrips do, not direct insert into the DB.

There are multiple tables affected, linked with indexes so a direct import won't do any good.

  • Like 1
Link to comment
Share on other sites

Thanks ! It help a lot, i used ps classe and it's working, now I have a another problem with the classes, i don't know how to add image to a product with PS classe.

    if(isset($_POST['buttonImportProductFromApi'])){
                $url = 'put your api link here';
                $raw = file_get_contents($url);
                $products = json_decode($raw);
                foreach($products as $oneproduct){
                    
                    $name = $oneproduct->REFART;
                    $quantity = $oneproduct->QUANTITE;
                    $price = $oneproduct->PRIX_PUBLIC;
                    $link_rewrite = $oneproduct->LINK_REWRITE;

                    
                    $default_lang = Configuration::get('PS_LANG_DEFAULT');
                    $product = new Product();

                    
                    $product->name = [$default_lang => $name];
                    $product->quantity = $quantity;
                    $product->price = $price;
                    $product->link_rewrite = [$default_lang => $link_rewrite];
                    //////////
                    $product->id_category = [3,4]; //3 is the main categorie and 4 the sub categorie, exemple : 3 = clothes and 4 = man
                    $product->id_category_default = 3; //main categorie
                    //////////
                    if($product->add())
                    {
                        $product->updateCategories($product->id_category);
                        StockAvailable::setQuantity((int)$product->id, 0, $product->quantity,Context::getContext()->shop->id);
                    }
                }
    }

 

Link to comment
Share on other sites

i found this, but i don't understand where i have to put my image's link/variable,

	//adding images for the product
	$image = new Image();
	$image->id_product = $id_product;
	$image->position = Image::getHighestPosition($id_product) + 1;
	$image->cover = true; // or false;
	if (($image->validateFields(false, true)) === true &&
	($image->validateFieldsLang(false, true)) === true && $image->add())
	{
		$image->associateTo($shops);		
	}

 

Link to comment
Share on other sites

f (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add())
{
      if (!AdminImportController::copyImg($product->id_product, $image->id, 'http://i.imgur.com/jLThaBj.jpg', 'products', false))
      {
            $image->delete();
      }
}
AdminImportController::copyImg is protected so either you override it or copy it to your code.
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...