| @@ 1017-1046 (lines=30) @@ | ||
| 1014 | * @param WC_Product $product Product instance |
|
| 1015 | * @return int Returns an ID if a valid variation was found for this product |
|
| 1016 | */ |
|
| 1017 | protected function get_variation_id( $product, $variations = array() ) { |
|
| 1018 | $variation_id = null; |
|
| 1019 | $variations_normalized = array(); |
|
| 1020 | ||
| 1021 | if ( $product->is_type( 'variable' ) && $product->has_child() ) { |
|
| 1022 | if ( isset( $variations ) && is_array( $variations ) ) { |
|
| 1023 | // start by normalizing the passed variations |
|
| 1024 | foreach ( $variations as $key => $value ) { |
|
| 1025 | $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); // from get_attributes in class-wc-api-products.php |
|
| 1026 | $variations_normalized[ $key ] = strtolower( $value ); |
|
| 1027 | } |
|
| 1028 | // now search through each product child and see if our passed variations match anything |
|
| 1029 | foreach ( $product->get_children() as $variation ) { |
|
| 1030 | $meta = array(); |
|
| 1031 | foreach ( get_post_meta( $variation ) as $key => $value ) { |
|
| 1032 | $value = $value[0]; |
|
| 1033 | $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); |
|
| 1034 | $meta[ $key ] = strtolower( $value ); |
|
| 1035 | } |
|
| 1036 | // if the variation array is a part of the $meta array, we found our match |
|
| 1037 | if ( $this->array_contains( $variations_normalized, $meta ) ) { |
|
| 1038 | $variation_id = $variation; |
|
| 1039 | break; |
|
| 1040 | } |
|
| 1041 | } |
|
| 1042 | } |
|
| 1043 | } |
|
| 1044 | ||
| 1045 | return $variation_id; |
|
| 1046 | } |
|
| 1047 | ||
| 1048 | /** |
|
| 1049 | * Utility function to see if the meta array contains data from variations. |
|
| @@ 1008-1037 (lines=30) @@ | ||
| 1005 | * @param WC_Product $product Product instance |
|
| 1006 | * @return int Returns an ID if a valid variation was found for this product |
|
| 1007 | */ |
|
| 1008 | public function get_variation_id( $product, $variations = array() ) { |
|
| 1009 | $variation_id = null; |
|
| 1010 | $variations_normalized = array(); |
|
| 1011 | ||
| 1012 | if ( $product->is_type( 'variable' ) && $product->has_child() ) { |
|
| 1013 | if ( isset( $variations ) && is_array( $variations ) ) { |
|
| 1014 | // start by normalizing the passed variations |
|
| 1015 | foreach ( $variations as $key => $value ) { |
|
| 1016 | $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); // from get_attributes in class-wc-api-products.php |
|
| 1017 | $variations_normalized[ $key ] = strtolower( $value ); |
|
| 1018 | } |
|
| 1019 | // now search through each product child and see if our passed variations match anything |
|
| 1020 | foreach ( $product->get_children() as $variation ) { |
|
| 1021 | $meta = array(); |
|
| 1022 | foreach ( get_post_meta( $variation ) as $key => $value ) { |
|
| 1023 | $value = $value[0]; |
|
| 1024 | $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); |
|
| 1025 | $meta[ $key ] = strtolower( $value ); |
|
| 1026 | } |
|
| 1027 | // if the variation array is a part of the $meta array, we found our match |
|
| 1028 | if ( $this->array_contains( $variations_normalized, $meta ) ) { |
|
| 1029 | $variation_id = $variation; |
|
| 1030 | break; |
|
| 1031 | } |
|
| 1032 | } |
|
| 1033 | } |
|
| 1034 | } |
|
| 1035 | ||
| 1036 | return $variation_id; |
|
| 1037 | } |
|
| 1038 | ||
| 1039 | /** |
|
| 1040 | * Utility function to see if the meta array contains data from variations |
|