Jump to content

Using Classes standalone


Chrille

Recommended Posts

Is it possible to use the classes stand-alone? If so, how?
I'm making a product import system
As far as I can see, I just started using and developing in Prestashop for a couple of months, it should be possible. Most of the classes are objects extending core-objects. It is possible to use the classes as stand-alone object just as long as you unbind the class from the existing core-objects or use the core-objects to bind the class to your script
Link to comment
Share on other sites

Yes. I use the DB class all the time. You have to do something like this:
The include of config.inc.php basically includes all the classes.

<?php
 include('/config/config.inc.php');

 $aColumns  = array('surplusPartNumber', 'surplusAltPartNumber', 'surplusCondition', 'surplusQuantity', 'surplusDescription');
 $aTable    = 'tbl_surplus';
 $aKeyField = 'surplusID';

 /* 
  * Paging
 */
 $sLimit = "";
 if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' ) {
   $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
   mysql_real_escape_string( $_GET['iDisplayLength'] );
 }

 /*
  * Ordering
 */
 if ( isset( $_GET['iSortCol_0'] ) ) {
   $sOrder = "ORDER BY  ";
   for ( $i=0 ; $i      if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" ) {
       $sOrder .= $aColumns[ intval( $_GET['iSortCol_0'] ) ]."
          ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
     }
   }

   $sOrder = substr_replace( $sOrder, "", -2 );
   if ( $sOrder == "ORDER BY" ) {
     $sOrder = "";
   }
 }

 /* 
  * Filtering
  * NOTE this does not match the built-in DataTables filtering which does it
  * word by word on any field. It's possible to do here, but concerned about efficiency
  * on very large tables, and MySQL's regex functionality is very limited
  */
 $sWhere = "";

 if ( $_GET['sSearch'] != "" ) {
   /* Individual column filtering */
   for ( $i=0 ; $i      if ( $_GET['bSearchable_'.$i] == "true") { // && $_GET['sSearch_'.$i] != '' ) {
       if ( $sWhere == "" ) {
         $sWhere = "WHERE ";
       } else {
         $sWhere .= " OR ";
       } // if ( $sWhere == "" )
       if(strpos($aColumns[$i], 'Description')===false) {
         $sWhere .= "UPPER(`".$aColumns[$i]."`) LIKE UPPER('".mysql_real_escape_string($_GET['sSearch'])."%') ";
       } else {
         $sWhere .= "UPPER(`".$aColumns[$i]."`) LIKE UPPER('%".mysql_real_escape_string($_GET['sSearch'])."%') ";
       }
     } // if ( $_GET['bSearchable_'.$i]
   } // for ( $i=1 ; $i<3 ; $i++ )
 }  

 /*
  * SQL queries
  * Get data to display
 */
 $sQuery = "SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
            FROM $aTable
            $sWhere
            $sOrder
            $sLimit";

 $rResult = Db::getInstance()->ExecuteS($sQuery);

 /* Data set length after filtering */
 $sQuery = "SELECT FOUND_ROWS() as iFilteredTotal";
 $aResultFilterTotal = Db::getInstance()->ExecuteS($sQuery);
 $iFilteredTotal = $aResultFilterTotal[0]['iFilteredTotal'];

 /* Total data set length */
 $sQuery = "SELECT COUNT(".$aKeyField.") as iTotal FROM ".$aTable;
 $aResultTotal = Db::getInstance()->ExecuteS($sQuery);
 $iTotal = $aResultTotal[0]['iTotal'];

 /* Output */
 $output = array(
   "sEcho"                => intval($_GET['sEcho']),
   "iTotalRecords"        => $iTotal,
   "iTotalDisplayRecords" => $iFilteredTotal,
   "aaData"               => array()
 );

 foreach ($rResult as $aRow) {
   $row = array();
   foreach($aColumns as $name=> $value) {
     $row[] = $aRow[$value];
   }
   $output['aaData'][] = $row;
 }

 echo json_encode( $output );

?>



What this code does is it returns to the caller a JSON encoded output of the called query.
This is used for server-side processing of some external data, not from the store, into the jQuery DataTables control. I use this to create a new page that is called from a module I wrote. This allows me to show a grid of data that is in tables that are not part of Prestashop.

Link to comment
Share on other sites

I’m making a product import system


I have one already that we have used for several sites. we just modify it to do what we want.
It is pretty straightforward and doesn't use the internal PS classes.

If you want it, PM me and I'll send it to you along with notes.

This script was written before I started working here. It took 7 hrs to import 18000 products.
I modified it and now it takes < 1 hr to import 24000 products.

It automatically downloads our vendor file from our vendor, imports each product and gets the product image from the vendor's
website.

If you have more than one vendor, it may not work for you.
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...