Plugin Directory

Changeset 2174597


Ignore:
Timestamp:
10/16/2019 11:45:16 PM (6 years ago)
Author:
dragosmicu
Message:

Improved recommendation engine and added better fallback.

Location:
wootomation/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wootomation/trunk/includes/class-ai.php

    r2172583 r2174597  
    66 *
    77 * @package Wootomation
    8  * @version 1.0.0
     8 * @version 1.0.2
    99 */
    1010
  • wootomation/trunk/includes/class-sales.php

    r2173650 r2174597  
    66 *
    77 * @package Wootomation
    8  * @version 1.0.0
     8 * @version 1.0.2
    99 */
    1010
     
    9090
    9191    /**
     92     * Get all possible combinations
     93     */
     94    public static function get_combinations($products){
     95        // initialize by adding the empty set
     96        $results = array(array( ));
     97
     98        foreach ($products as $product){
     99            foreach ($results as $combination){
     100                array_push($results, array_merge(array($product), $combination));
     101            }
     102        }
     103
     104        array_shift($results);
     105
     106        return $results;
     107    }
     108
     109    /**
    92110     * Sort out data by most frequent
    93111     */
  • wootomation/trunk/wootomation.php

    r2173650 r2174597  
    44 * Plugin URI: https://www.dragosmicu.co.uk/wootomation
    55 * Description: 🤖 Increase the sales of your WooCommerce shop by suggesting the right products to your customers, with the help of Machine Learning Artificial Intelligence. To take advantage of its power, just install and activate, it works out of the box.
    6  * Version: 1.0.0
     6 * Version: 1.0.2
     7 * Stable tag: 1.0.2
    78 * Author: Dragos Micu
    89 * Author URI: https://www.dragosmicu.co.uk/
     
    2324        public $plugin_name;
    2425        public $ajax_url;
     26        public $suggestions_per_page;
    2527
    2628
     
    2830            $this->plugin_name = plugin_basename( __FILE__ );
    2931            $this->ajax_url = admin_url('admin-ajax.php');
     32            $this->suggestions_per_page = 3;
    3033
    3134            // Add admin notices
     
    160163            require __DIR__ . '/includes/class-sales.php';
    161164
     165            // get all products in cart
    162166            $products_in_cart = Wootomation_Sales::get_cart_items();
    163167
    164             $suggestions = Wootomation_AI::predict($associator, $products_in_cart);
     168            // get all combinations of products
     169            $all_combinations = Wootomation_Sales::get_combinations($products_in_cart);
     170
     171            // get all suggestions based off all combinations
     172            $suggestions = array();
     173           
     174            foreach ($all_combinations as $combination) {
     175                // for each combination AI predict 
     176                $prediction = Wootomation_AI::predict($associator, $combination);
     177                // add prediction to suggestions array
     178                foreach ($prediction as $predict) {
     179                    $suggestions[] = $predict;
     180                }
     181            }
    165182
    166183            if( $suggestions ){
     184                // order suggestions by most frequest first
    167185                $sorted_data = Wootomation_Sales::get_most_frequent($suggestions);
    168                 $number_of_suggestions = count( $sorted_data );
     186
     187                // remove products from suggestions that are already in cart
     188                $final_predictions = array();
     189                foreach ($sorted_data as $data) {
     190                    if( !in_array($data, $products_in_cart) ){
     191                        $final_predictions[] = $data;
     192                    }
     193                }
     194                $number_of_suggestions = count( $final_predictions );
    169195            } else {
    170                 $sorted_data = array();
     196                $final_predictions = array();
    171197                $number_of_suggestions = 0;
    172198            }
    173199
    174             if( $number_of_suggestions < 3 ){
    175                 $needed_sugestions = 3 - $number_of_suggestions;
    176                 if( $needed_sugestions > count($products_in_cart) ){
    177                     $needed_sugestions = count($products_in_cart);
    178                 }
    179 
    180                 for ($i=0; $i < $needed_sugestions; $i++) {
    181                     $each_item_in_cart = array();
    182                     $each_item_in_cart[] = $products_in_cart[$i];
    183                     $suggestions = Wootomation_AI::predict($associator, $each_item_in_cart);
    184                     $secondary_data = Wootomation_Sales::get_most_frequent($suggestions);
    185 
    186                     if( $secondary_data ){
    187                         foreach ($secondary_data as $each_data) {
    188                             if( !in_array($each_data, $products_in_cart) ){
    189                                 array_push($sorted_data, $each_data);
    190                             }
    191                         }
     200
     201            // if( $number_of_suggestions < $this->suggestions_per_page ){
     202            //  $needed_sugestions = $this->suggestions_per_page - $number_of_suggestions;
     203            //  if( $needed_sugestions > count($products_in_cart) ){
     204            //      $needed_sugestions = count($products_in_cart);
     205            //  }
     206
     207            //  for ($i=0; $i < $needed_sugestions; $i++) {
     208            //      $each_item_in_cart = array();
     209            //      $each_item_in_cart[] = $products_in_cart[$i];
     210            //      $suggestions = Wootomation_AI::predict($associator, $each_item_in_cart);
     211            //      $secondary_data = Wootomation_Sales::get_most_frequent($suggestions);
     212
     213            //      if( $secondary_data ){
     214            //          foreach ($secondary_data as $each_data) {
     215            //              if( !in_array($each_data, $products_in_cart) ){
     216            //                  array_push($final_predictions, $each_data);
     217            //              }
     218            //          }
     219            //      }
     220            //  }
     221
     222            //  $final_predictions = array_unique($final_predictions);
     223            // }
     224            //
     225           
     226            /**
     227             * If number of suggestions not sufficient
     228             * then get remaining number of products
     229             * randomly, based off related categories
     230             */
     231            if( $number_of_suggestions < $this->suggestions_per_page ){
     232                $needed_sugestions = $this->suggestions_per_page - $number_of_suggestions;
     233
     234                $all_categories = array();
     235                foreach ($products_in_cart as $product_in_cart) {
     236                    $categories = get_the_terms( $product_in_cart, 'product_cat' );
     237
     238                    foreach ($categories as $cat) {
     239                        $all_categories[] = $cat->slug;
    192240                    }
    193241                }
    194 
    195                 $sorted_data = array_unique($sorted_data);
     242                array_unique($all_categories);
     243               
     244                $args = array(
     245                    'post_type'      => 'product',
     246                    'post_status'    => 'publish',
     247                    'posts_per_page' => $needed_sugestions,
     248                    'tax_query' => array(
     249                        array (
     250                            'taxonomy' => 'product_cat',
     251                            'field' => 'slug',
     252                            'terms' => $all_categories,
     253                        )
     254                    ),
     255                );
     256
     257                $fallback_products = new WP_Query($args);
     258                if( $fallback_products->have_posts() ):
     259                    while( $fallback_products->have_posts() ): $fallback_products->the_post();
     260                        $final_predictions[] = get_the_ID();
     261                    endwhile;
     262                endif;
    196263            }
    197264
     265
     266            /**
     267             * Creates final display query using all found predictions
     268             * Uses default WooCommerce product template
     269             */
    198270            $args = array(
    199271                'post_type'      => 'product',
    200272                'post_status'    => 'publish',
    201                 'post__in'       => $sorted_data,
     273                'post__in'       => $final_predictions,
    202274                'orderby'        => 'post__in',
    203                 'posts_per_page' => 3,
    204 
     275                'posts_per_page' => $this->suggestions_per_page,
    205276            );
    206277
    207278            $display_products = new WP_Query($args);
    208 
    209             $count = $display_products->found_posts;
    210279
    211280            if( $display_products->have_posts() ): ?>
Note: See TracChangeset for help on using the changeset viewer.