Query tweaks

This commit is contained in:
Mike Jolley 2019-01-11 16:48:05 +00:00
parent 4bda1c9cad
commit da80c154fc
1 changed files with 59 additions and 33 deletions

View File

@ -1055,7 +1055,41 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
public function find_matching_product_variation( $product, $match_attributes = array() ) { public function find_matching_product_variation( $product, $match_attributes = array() ) {
global $wpdb; global $wpdb;
$matched_variation_id = 0; $meta_attribute_names = array();
// Get attributes to match in meta.
foreach ( $product->get_attributes() as $attribute ) {
if ( ! $attribute->get_variation() ) {
continue;
}
$attribute_field_name = 'attribute_' . sanitize_title( $attribute->get_name() );
if ( ! isset( $match_attributes[ $attribute_field_name ] ) ) {
return 0;
}
$meta_attribute_names[] = $attribute_field_name;
}
// Get the attributes of the variations.
$query = $wpdb->prepare(
"
SELECT post_id, meta_key, meta_value FROM {$wpdb->prefix}postmeta
WHERE post_id IN (
SELECT ID FROM {$wpdb->prefix}posts
WHERE {$wpdb->prefix}posts.post_parent = %d
AND {$wpdb->prefix}posts.post_status = 'publish'
AND {$wpdb->prefix}posts.post_type = 'product_variation'
ORDER BY menu_order ASC, ID ASC
)
",
$product->get_id()
);
$query .= ' AND meta_key IN ( "' . implode( '","', array_map( 'esc_sql', $meta_attribute_names ) ) . '" );';
$attributes = $wpdb->get_results( $query ); // phpcs:ignore
// Get any variations of the main product. // Get any variations of the main product.
$variation_ids = wp_parse_id_list( $variation_ids = wp_parse_id_list(
@ -1074,18 +1108,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
) )
); );
if ( $variation_ids ) { if ( ! $attributes ) {
// Get the attributes of the variations. return 0;
$variation_ids_string = implode( ',', $variation_ids ); }
$query = "SELECT * FROM {$wpdb->prefix}postmeta WHERE post_id IN($variation_ids_string) AND meta_key LIKE %s";
$attributes = $wpdb->get_results(
$wpdb->prepare(
$query, // phpcs:ignore
$wpdb->esc_like( 'attribute_' ) . '%'
)
);
if ( $attributes ) {
$sorted_meta = array(); $sorted_meta = array();
foreach ( $attributes as $m ) { foreach ( $attributes as $m ) {
@ -1097,6 +1123,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* *
* Note: Not all meta fields will be set which is why we check existance. * Note: Not all meta fields will be set which is why we check existance.
*/ */
$matched_variation_id = 0;
foreach ( $sorted_meta as $variation_id => $variation ) { foreach ( $sorted_meta as $variation_id => $variation ) {
$match = true; $match = true;
@ -1113,8 +1141,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
break; break;
} }
} }
}
}
return $matched_variation_id; return $matched_variation_id;
} }