Query tweaks
This commit is contained in:
parent
4bda1c9cad
commit
da80c154fc
|
@ -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,46 +1108,38 @@ 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 ) {
|
||||||
$sorted_meta[ $m->post_id ][ $m->meta_key ] = $m->meta_value; // phpcs:ignore
|
$sorted_meta[ $m->post_id ][ $m->meta_key ] = $m->meta_value; // phpcs:ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check each variation to find the one that matches the $match_attributes.
|
* Check each variation to find the one that matches the $match_attributes.
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
foreach ( $sorted_meta as $variation_id => $variation ) {
|
$matched_variation_id = 0;
|
||||||
$match = true;
|
|
||||||
|
|
||||||
foreach ( $match_attributes as $attribute_key => $attribute_value ) {
|
foreach ( $sorted_meta as $variation_id => $variation ) {
|
||||||
if ( array_key_exists( $attribute_key, $variation ) ) {
|
$match = true;
|
||||||
if ( $variation[ $attribute_key ] !== $attribute_value && ! empty( $variation[ $attribute_key ] ) ) {
|
|
||||||
$match = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( true === $match ) {
|
foreach ( $match_attributes as $attribute_key => $attribute_value ) {
|
||||||
$matched_variation_id = $variation_id;
|
if ( array_key_exists( $attribute_key, $variation ) ) {
|
||||||
break;
|
if ( $variation[ $attribute_key ] !== $attribute_value && ! empty( $variation[ $attribute_key ] ) ) {
|
||||||
|
$match = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( true === $match ) {
|
||||||
|
$matched_variation_id = $variation_id;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $matched_variation_id;
|
return $matched_variation_id;
|
||||||
|
|
Loading…
Reference in New Issue