Script to import products and categories in Prestashop

By | October 4, 2017

Script to import products and categories in Prestashop,Advanced csv export is an easy to use but powerful tool for export products, orders, categories, suppliers, manufaturers, newsletters in csv format. You can save

 

I am in the middle of developing a virtual store with Prestashop for a customer in the hardware sector. The client has its product management application on the company server and the database of these must be synchronized with the e-commerce automatically so that any changes in it are updated in the database of Prestashop.

Initially I had thought of the possibility of making a script that synchronized the two databases connecting directly with them but since the Prestashop has an import module from CSV files, I decided to use it.

Script to import products and categories in Prestashop

In this architecture there are two parts, the part of the client located in the local server of the company and the part of the virtual store located in a web hosting, which among other things requires to register rules in the firewall to allow to connect to Mysql since an external IP, which we will not finally need.

In the client side I will program a script that exports in CSV format, expected by Prestashop, both categories and products – in this case the only thing I need to export – and upload both files to the server where the store is located.

In the part of the virtual store requires a script that is able to launch the import process and is the one I will describe below.

There was the possibility, in this second part, to simulate the navigation using CURL, making the login and subsequent import process, but it complicates things much more when what we need is simply to call the AdminImport class and use its functionality.

Here I leave the script code that, located in the part of the store, launches an import of a CSV file products (pro.csv) or categories (cat.csv), as directed by GET [‘entity’]

CSV files to import must be in the / admin / import directory while our script must be located in the / admin directory of the Prestashop.

<? php define ( 'PS_ADMIN_DIR' , getcwd ( ) ) ; include ( PS_ADMIN_DIR . '/../config/config.inc.php' ) ; include ( PS_ADMIN_DIR . '/functions.php' ) ; include_once './tabs/AdminImport.php' ; if ( ! isset ( $ _GET [ 'entity' ] ) ) die ( ) ; $ import = New AdminImport ( ) ; switch ( $ _GET [ 'entity' ] ) { case 0 : loadCategoriesPost ( ) ; $ import -> categoryImport ( ) ;  
    break ;
 
  case  1 : 
    loadProductsPost ( ) ; 
    $ import -> productImport ( ) ; 
    break ;
 
  default : 
    die ( ) ; 
    break ; 
}
 
function loadCategoriesPost ( )  { 
  $ _POST  =  array  ( 
    'tab'  =>  'AdminImport' , 
    'skip'  =>  '0' , 
    'csv'  =>  'cat.csv' , 
    'convert'  =>  '' , 
    'entity'  =>  '0' , 
    'separator'  =>  ';' , 
    'multiple_value_separator'  =>  '' , 
    'import'  =>  'Import CSV data' , 
    'type_value '  => 
    array  ( 
      0  =>  ' id ', 
      1  =>  'active' , 
      2  =>  'name' , 
      3  =>  'parent' , 
      4  =>  'description' , 
      5  =>  'meta_title' , 
      6  =>  'meta_keywords' , 
      7  =>  'meta_description' , 
      8  =>  'link_rewrite' , 
      9  =>  'image' , 
    ) , 
  ) ; 
}
 
function loadProductsPost ( )  { 
  $ _POST  =  array  ( 
    'tab'  =>  'AdminImport' , 
    'skip'  =>  '0' , 
    'csv'  =>  'pro.csv' , 
    'convert'  =>  '' , 
    'entity'  =>  '1' , 
    'separator'  =>  ';' , 
    'multiple_value_separator'  =>  '' , 
    'import'  =>  'Import CSV data' , 
    'type_value '  => 
    array  ( 
      0  =>  ' id ', 
      1  =>  'active' , 
      2  =>  'name' , 
      3  =>  'category' , 
      4  =>  'price_tex' , 
      5  =>  'tax_rate' , 
      6  =>  'wholesale_price' , 
      7  =>  'on_sale' , 
      8  =>  'reduction_price' , 
      9  =>  'reduction_percent' , 
      10  =>  'reduction_from' , 
      11  =>  'reduction_to' ,
      12  =>  'reference' , 
      13 =>  'Supplier_reference' , 
      14  =>  'supplier' , 
      15  =>  'manufacturer' , 
      16  =>  'ean13' , 
      17  =>  'ecotax' , 
      18  =>  'weight' , 
      19  =>  'quantity' , 
      20  = >  'description_short' , 
      21  =>  'description' , 
      22  =>  'tags' , 
      23  =>  ' meta_title ' , 
      24  =>  'meta_keywords' , 
      25  => 'meta_description' , 
      26  =>  'available_now' , 
      27  =>  'available_later' , 
      28  =>  'image' , 
      29  =>  'no' , 
    ) , 
  ) ; 
} 
?>

Leave a Reply

Your email address will not be published. Required fields are marked *