Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: product connection sorting #522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions includes/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ function() {
$resolver->set_query_arg( 'orderby', 'post__in' );
}

$resolver = self::set_ordering_query_args( $resolver, $args );

return $resolver->get_connection();
},
)
Expand All @@ -216,6 +218,8 @@ function() {
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'product' );
$resolver->set_query_arg( 'p', $source->parent_id );

$resolver = self::set_ordering_query_args( $resolver, $args );

return $resolver->one_to_one()->get_connection();
},
)
Expand All @@ -234,6 +238,8 @@ function() {
);
$resolver->set_query_arg( 'tax_query', $tax_query );

$resolver = self::set_ordering_query_args( $resolver, $args );

return $resolver->get_connection();
};

Expand Down Expand Up @@ -294,6 +300,8 @@ function() {
$resolver->set_query_arg( 'post__in', $variation_ids );
$resolver->set_query_arg( 'post_type', 'product_variation' );

$resolver = self::set_ordering_query_args( $resolver, $args );

return $resolver->get_connection();
},
)
Expand All @@ -319,31 +327,54 @@ public static function get_connection_config( $args = array() ): array {
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'product' );

if ( ! empty( $args['where']['orderby'] ) ) {
foreach ( $args['where']['orderby'] as $orderby_input ) {
switch ( $orderby_input['field'] ) {
case '_price':
case '_regular_price':
case '_sale_price':
case '_wc_rating_count':
case '_wc_average_rating':
case '_sale_price_dates_from':
case '_sale_price_dates_to':
case 'total_sales':
$resolver->set_query_arg( 'orderby', array( 'meta_value_num' => $orderby_input['order'] ) );
$resolver->set_query_arg( 'meta_key', esc_sql( $orderby_input['field'] ) );
$resolver->set_query_arg( 'meta_type', 'NUMERIC' );
break 2;
}
}
}
$resolver = self::set_ordering_query_args( $resolver, $args );

return $resolver->get_connection();
},
),
$args
);
}

/**
* Undocumented function
*
* @param PostObjectConnectionResolver $resolver Connection resolver instance.
* @param array $args Connection provided args.
*
* @return PostObjectConnectionResolver
*/
public static function set_ordering_query_args( $resolver, $args ) {
$backward = isset( $args['last'] ) ? true : false;

if ( ! empty( $args['where']['orderby'] ) ) {
foreach ( $args['where']['orderby'] as $orderby_input ) {
switch ( $orderby_input['field'] ) {
case '_price':
case '_regular_price':
case '_sale_price':
case '_wc_rating_count':
case '_wc_average_rating':
case '_sale_price_dates_from':
case '_sale_price_dates_to':
case 'total_sales':
$order = $orderby_input['order'];

if ( $backward ) {
$order = 'ASC' === $order ? 'DESC' : 'ASC';
}

$resolver->set_query_arg( 'orderby', array( 'meta_value_num' => $order ) );
$resolver->set_query_arg( 'meta_key', esc_sql( $orderby_input['field'] ) );
$resolver->set_query_arg( 'meta_type', 'NUMERIC' );
break 2;
}
}
}

return $resolver;
}

/**
* Returns array of where args.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/type/object/class-cart-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public static function register_cart_item_connections() {

return $attributes;
},
)
),
),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$id = $source['product_id'];
Expand Down