Changeset 2174597
- Timestamp:
- 10/16/2019 11:45:16 PM (6 years ago)
- Location:
- wootomation/trunk
- Files:
-
- 3 edited
-
includes/class-ai.php (modified) (1 diff)
-
includes/class-sales.php (modified) (2 diffs)
-
wootomation.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wootomation/trunk/includes/class-ai.php
r2172583 r2174597 6 6 * 7 7 * @package Wootomation 8 * @version 1.0. 08 * @version 1.0.2 9 9 */ 10 10 -
wootomation/trunk/includes/class-sales.php
r2173650 r2174597 6 6 * 7 7 * @package Wootomation 8 * @version 1.0. 08 * @version 1.0.2 9 9 */ 10 10 … … 90 90 91 91 /** 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 /** 92 110 * Sort out data by most frequent 93 111 */ -
wootomation/trunk/wootomation.php
r2173650 r2174597 4 4 * Plugin URI: https://www.dragosmicu.co.uk/wootomation 5 5 * 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 7 8 * Author: Dragos Micu 8 9 * Author URI: https://www.dragosmicu.co.uk/ … … 23 24 public $plugin_name; 24 25 public $ajax_url; 26 public $suggestions_per_page; 25 27 26 28 … … 28 30 $this->plugin_name = plugin_basename( __FILE__ ); 29 31 $this->ajax_url = admin_url('admin-ajax.php'); 32 $this->suggestions_per_page = 3; 30 33 31 34 // Add admin notices … … 160 163 require __DIR__ . '/includes/class-sales.php'; 161 164 165 // get all products in cart 162 166 $products_in_cart = Wootomation_Sales::get_cart_items(); 163 167 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 } 165 182 166 183 if( $suggestions ){ 184 // order suggestions by most frequest first 167 185 $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 ); 169 195 } else { 170 $ sorted_data= array();196 $final_predictions = array(); 171 197 $number_of_suggestions = 0; 172 198 } 173 199 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; 192 240 } 193 241 } 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; 196 263 } 197 264 265 266 /** 267 * Creates final display query using all found predictions 268 * Uses default WooCommerce product template 269 */ 198 270 $args = array( 199 271 'post_type' => 'product', 200 272 'post_status' => 'publish', 201 'post__in' => $ sorted_data,273 'post__in' => $final_predictions, 202 274 'orderby' => 'post__in', 203 'posts_per_page' => 3, 204 275 'posts_per_page' => $this->suggestions_per_page, 205 276 ); 206 277 207 278 $display_products = new WP_Query($args); 208 209 $count = $display_products->found_posts;210 279 211 280 if( $display_products->have_posts() ): ?>
Note: See TracChangeset
for help on using the changeset viewer.