Guest User

Untitled

a guest
Jul 14th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1.  
  2. add_filter('woocommerce_product_default_attributes', 'setup_swatches', 10, 1);
  3.  
  4. function setup_swatches($selected_attributes) {
  5. $varation_names = wpbo_get_variation_values();
  6. $start_vals = wpbo_get_variation_start_values( $varation_names,$selected_attributes);
  7.  
  8. if(!empty($start_vals)) {
  9. return $start_vals;
  10. } else {
  11. return $selected_attributes;
  12. }
  13. }
  14.  
  15. /*
  16. * Returns an array of variations related to a product
  17. *
  18. * @access public
  19. * @subpackage Product
  20. * @return array variation_names
  21. *
  22. */
  23. function wpbo_get_variation_values() {
  24. global $product;
  25.  
  26. // Create an array of possible variations
  27. $available_variations = $product->get_variation_attributes();
  28. $varation_names = array();
  29.  
  30. foreach ( $available_variations as $key => $variations ) {
  31. array_push( $varation_names, $key );
  32. }
  33.  
  34. return $varation_names;
  35. }
  36.  
  37. /*
  38. * Returns an array of variations related to a product
  39. *
  40. * @access public
  41. * @subpackage Product
  42. * @param array variation_names
  43. * @return array start_vals
  44. *
  45. */
  46. function wpbo_get_variation_start_values( $varation_names,$selected_attributes ) {
  47. global $product;
  48.  
  49. $all_variations = $product->get_variation_attributes();
  50. $_GET_lower = array_change_key_case($_GET, CASE_LOWER);
  51.  
  52. // Check to see if any of the attributes are in $_GET vars
  53. $start_vals = array();
  54.  
  55. foreach ( $varation_names as $name ) {
  56.  
  57.  
  58. // Get the lower case name and remove the pa_ if they have it
  59. $lower_name = strtolower( $name );
  60. $clean_name = str_replace( 'pa_', '', $lower_name );
  61. $flag = false;
  62.  
  63. // Grab the right variation based on the full name
  64. if ( isset( $_GET_lower[ $lower_name ] ) ) {
  65.  
  66. foreach( $all_variations[ $name ] as $val ) {
  67. if ( strtolower( $val ) == strtolower( $_GET_lower[ $lower_name ] ) ) {
  68. $flag = true;
  69. }
  70. }
  71.  
  72. if ( $flag == true ) {
  73. $start_vals[ $lower_name ] = $_GET_lower[ $lower_name ];
  74. }
  75.  
  76. // Grab the right variation if they attribute has a pa_ infronnt of it
  77. } elseif ( isset( $_GET_lower[ $clean_name ] ) ) {
  78.  
  79. foreach( $all_variations[ $name ] as $val ) {
  80. if ( strtolower( $val ) == strtolower( $_GET_lower[ $clean_name ] ) ) {
  81. $flag = true;
  82. }
  83. }
  84.  
  85. if ( $flag == true ) {
  86. $start_vals[ $lower_name ] = $_GET_lower[ $clean_name ];
  87. }
  88. } else {
  89.  
  90. foreach ($selected_attributes as $default_attribute => $attribute_value) {
  91. if ($default_attribute == $name) {
  92. $start_vals[ $lower_name ] = $attribute_value;
  93. }
  94. }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. return $start_vals;
  101. }
Add Comment
Please, Sign In to add comment