daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest May 27th, 2017 200 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *   Output the variable product add to cart area.
  3. *
  4. *   @access public
  5. *   @subpackage  Product
  6. *   @return void
  7. */
  8.  
  9. //add_filter('woocommerce_product_default_attributes', 'setup_swatches', 10, 1);
  10. add_filter('woocommerce_product_get_default_attributes', 'setup_swatches', 10, 1);
  11. function setup_swatches($selected_attributes) {
  12.     $varation_names = wpbo_get_variation_values();
  13.     $start_vals = wpbo_get_variation_start_values( $varation_names );
  14.    
  15.     if(!empty($start_vals)) {
  16.         return $start_vals;
  17.     } else {
  18.         return $selected_attributes;
  19.     }
  20. }
  21.  
  22. /*
  23. *   Returns an array of variations related to a product
  24. *
  25. *   @access         public
  26. *   @subpackage     Product
  27. *   @return array   variation_names
  28. *
  29. */     
  30. function wpbo_get_variation_values() {
  31.     global $product;
  32.    
  33.     // Create an array of possible variations
  34.    
  35. if($product != null)  
  36. {  
  37.  
  38.     $available_variations = $product->get_variation_attributes();
  39.  
  40.     $varation_names = array();
  41.    
  42.     foreach ( $available_variations as $key => $variations ) {
  43.         array_push( $varation_names, $key );
  44.     }
  45.    
  46.     return $varation_names;
  47.    
  48. } else {
  49. $varation_names = '';
  50. }
  51.  
  52. }
  53.  
  54. /*
  55. *   Returns an array of variations related to a product
  56. *
  57. *   @access         public
  58. *   @subpackage     Product
  59. *   @param  array   variation_names
  60. *   @return array   start_vals
  61. *
  62. */ 
  63. function wpbo_get_variation_start_values( $varation_names ) {
  64.     global $product;
  65.  
  66. if($product != null)  
  67. {  
  68.     $all_variations = $product->get_variation_attributes();
  69.  
  70.    
  71.     $_GET_lower = array_change_key_case($_GET, CASE_LOWER);
  72.  
  73.     // Check to see if any of the attributes are in $_GET vars
  74.     $start_vals = array();
  75.  
  76.     foreach ( $varation_names as $name ) {
  77.    
  78.         // Get the lower case name and remove the pa_ if they have it
  79.         $lower_name = strtolower( $name );
  80.         $clean_name = str_replace( 'pa_', '', $lower_name );
  81.         $flag = false;
  82.        
  83.         // Grab the right variation based on the full name
  84.         if ( isset( $_GET_lower[ $lower_name ] ) ) {
  85.        
  86.             foreach( $all_variations[ $name ] as $val ) {      
  87.                 if ( strtolower( $val ) == strtolower( $_GET_lower[ $lower_name ] ) ) {
  88.                     $flag = true;
  89.                 }          
  90.             }
  91.  
  92.             if ( $flag == true ) {
  93.                 $start_vals[ $lower_name ] = $_GET_lower[ $lower_name ];
  94.             }
  95.        
  96.         // Grab the right variation if they attribute has a pa_ infronnt of it
  97.         } elseif ( isset( $_GET_lower[ $clean_name ] ) ) {
  98.        
  99.             foreach( $all_variations[ $name ] as $val ) {      
  100.                 if ( strtolower( $val ) == strtolower( $_GET_lower[ $clean_name ] ) ) {
  101.                     $flag = true;
  102.                 }          
  103.             }
  104.  
  105.             if ( $flag == true ) {
  106.                 $start_vals[ $lower_name ] = $_GET_lower[ $clean_name ];
  107.             }
  108.         }
  109.     }
  110.    
  111.     return $start_vals;
  112.    
  113. } //endif
  114.  
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top