Advertisement
mpa4hu

Untitled

Apr 14th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. /**
  2.  * wpsc_variation_combinator class.
  3.  * Produces all combinations of variations selected for this product
  4.  * this class is based off the example code from here:
  5.  * http://www.php.net/manual/en/ref.array.php#94910
  6.  * Thanks, phektus, you are awesome, whoever you are.
  7.  */
  8. class wpsc_variation_combinator {
  9.     var $variation_sets = array();
  10.     var $variation_values = array();
  11.     var $reprocessed_array = array();
  12.     var $combinations= array();
  13.  
  14. function wpsc_variation_combinator($variation_sets) {
  15.     if( $variation_sets ) {
  16.         foreach($variation_sets as $variation_set_id => $variation_set) {
  17.             $this->variation_sets[] = absint($variation_set_id);
  18.             $new_variation_set = array();
  19.             if( $variation_set ) {
  20.                 foreach($variation_set as $variation => $active) {
  21.                     if($active == 1) {
  22.                         $new_variation_set[] = array(absint($variation));
  23.                         $this->variation_values[] = $variation;
  24.                     }
  25.                 }
  26.             }
  27.             $this->reprocessed_array[] = $new_variation_set;
  28.         }
  29.         $this->get_combinations(array(), $this->reprocessed_array, 0);
  30.     }
  31. }
  32.  
  33.  
  34.     function get_combinations($batch, $elements, $i)  {
  35.         if ($i >= count($elements)) {
  36.             $this->combinations[] = $batch;
  37.         } else {
  38.             foreach ($elements[$i] as $element) {
  39.                 $this->get_combinations(array_merge($batch, $element), $elements, $i + 1);
  40.             }
  41.         }
  42.     }
  43.  
  44.     function return_variation_sets() {
  45.         return $this->variation_sets;
  46.     }
  47.  
  48.     function return_variation_values() {
  49.         return $this->variation_values;
  50.     }
  51.  
  52.     function return_combinations() {
  53.         return $this->combinations;
  54.  
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement