Saturday, May 2, 2015

Prestashop minimum quantity product by customer group

I'm working on prestashop 1.5 that invalidates quantities under 10 for each product for professionals, note that profesionnals are customers linked to groups with ID 4 or 5.

the best solution i'did is:

1- Override CartController under override/controllers/front/CartController
2- Added this little snipet: (where 4 and 5 are groups ids) (target method is processChangeProductInCart then add your controle)
  $arr = $this->context->customer->getGroups();
                $minQuantity = -1;
                if(in_array(4,$arr) || in_array(5,$arr)) $minQuantity=10;
                if($qty_to_check<$minQuantity)
                   $this->errors[] = Tools::displayError('Minimum order quantity is 10 for profesionnals');

3- be careful to add this code out of an if surrount that does not cover all scenarios, in our cas i've added it just after stock controle.
4- you can remark that later in the controller there's another quantity check made by prestashop using var called minimum_quantity, i didn't altered that code, i think it'sb not quiet careful to do it, since custom code have to be clear and separated from original code to avoid unwanted results

i hop it helps

Sunday, April 19, 2015

Wordpress: posts linked to many categories at the same time

in some search cases, there a need to get posts linked to many categories at the same time,
this query is useful
       $whereS = "12,13,22";//Here searched for categories ids
        query_posts( array('category__and'=>preg_split('/[\s,]+/',$whereS)));
next you can use loop to iterate through records
if (have_posts()) : while (have_posts()) : the_post();
  /*custom code*/ 
endWhile;
Same as 'give me posts that ara linked at the same time to categories 12, 13 and 22'
that's an intersection.

Note if you want to search for posts linked to 2 or more categories ; in this case we are talking about union and not intersection, we can use simply this little code
       $whereS = "12,13,22";
        query_posts( 'cat=12,13,22');
Same as 'give me posts in the categories 12, 13 and 22'

Thursday, January 22, 2015

Generic dao class for wordpress

metaDefinition = $metaDefinition;
        $this->tableName = $tableName;
    }
    /**
     * Insert a new row
     * @global type $wpdb
     * @param type $pairs
     * @return type last inserted id
     */
    function insertRow($pairs){
            global $wpdb;          
            $prototypes = array();
             foreach ($pairs as $colName => $dataType) {
                 $prototypes[] = $this->metaDefinition[$colName];
             }
            $wpdb->insert($this->tableName, $pairs,$prototypes);
            return $wpdb->insert_id;
    }
    /**
     * Select all rows
     * @global type $wpdb
     * @param type $orderingExpr
     * @param type $limit
     * @return type
     */
    function  selectAllRows($orderingExpr=" ",$limit=""){
            global $wpdb;
            $sql = "select * from $this->tableName ".$orderingExpr.$limit;
            return $wpdb->get_results($sql);
    }
    /**
     * Query table
     * @global type $wpdb
     * @param type $where
     * @param type $orderingExpr
     * @param type $limit
     * @return type
     */
    function  selectRows($where,$orderingExpr=" ",$limit=""){
            global $wpdb;
            $sql = "select * from $this->tableName where ".$where.' '.$orderingExpr.' '.$limit;
            return $wpdb->get_results($sql);
    }
    /**
     * Select rows count
     * @global type $wpdb
     * @return type
     */
    function selectRowsCount(){
            global $wpdb;
            $sql = "select count(*)as count from $this->tableName ";
            return $wpdb->get_results($sql);
    }
    /**
     * Delete row
     * @global type $wpdb
     * @param type $pairs array of paris (columns, new values)
     * @param type $where
     * @param type $whereFormat
     * @return type
     */
    function updateRow($pairs,$where,$whereFormat=array("%d")){
            global $wpdb;
             $prototypes = array();
             foreach ($pairs as $colName => $dataType) {
                 $prototypes[] = $this->metaDefinition[$colName];
             }
             return $wpdb->update( $this->tableName, $pairs, $where, $prototypes, $whereFormat );
    }
        
    function deleteRow($where,$whereFormat=array("%d")){
            global $wpdb;
            return $wpdb->delete( $this->tableName, $where,$whereFormat);
    }
    

}
?>

An implementation of this genric class is this little code:
Supposing that we have this table :
CREATE  TABLE IF NOT EXISTS `wordpress_categ_type_poste` (
  `ctgtp_id` INT NOT NULL AUTO_INCREMENT ,
  `ctgtp_lib` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`ctgtp_id`) )
ENGINE = InnoDB;
then an extension of this class can be
'%d','ctgtp_lib'=>'%s'),$wpdb->prefix . "categ_type_poste");
    }
  }
?>
once you have finished that class, it's recommended to build your service class exposing some or all functions of the dao class.

Friday, January 9, 2015

Jquery get longitude and latitude from address using google api

A little js code to retrieve long and lat from adress using google api
    function getLtLgFromAdresse(address,apiKey){
         var lng;
         var lt;
         jQuery.ajax({
                    type: "GET",
                    url: "https://maps.googleapis.com/maps/api/geocode/xml?address="+address+"&key="+apiKey,
                    dataType: "xml",
                    success: function(xml) {      
                          if(jQuery(xml).find('status').text() == 'OK'){
                              var locationNode = jQuery(xml).find('result:first'). find('geometry').find('location'); 
                              lng = locationNode.find('lng').text();
                              lt =  locationNode.find('lat').text();
                              alert("lng: "+lng+" lat:"+lt) 
                          }    else{
                                alert('Error!');          
                          }
                       
                    }                
            });
    }
apiKey is your google key, read this for more info
https://developers.google.com/maps/documentation/geocoding/

Note that here we show only the first occurence "using the :first near to the selector result.

Friday, January 2, 2015

Primefaces responsive theme

In order to render a mobile content for primefaces framework bases web application:
just follow these 2 steps:
  1. in faces config.xmln add

      
         
           
             org.primefaces.mobile.application.MobileNavigationHandler
           
         
         
         PRIMEFACES_MOBILE
    "
  2. at the top of your xhtml or jsf or ... whatever extesion choosen for files that will be processed by jsf servlet  add this taglib:
    xmlns:pm="http://primefaces.org/mobile
    "
pm taglib offers many possibilities such as view pagination like :
 
   
 
  

to define inside the same view many pages