Skip to content

Commit

Permalink
Merge pull request #1681 from woocommerce/update/1610-order-by-total-…
Browse files Browse the repository at this point in the history
…sales-in-product-feed-api

Free Listings + Paid Ads: Update product feed API to return the bestselling product
  • Loading branch information
ianlin authored Sep 21, 2022
2 parents 694d25d + 7e3caaf commit 046f8fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/API/Site/Controllers/MerchantCenter/ProductFeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,37 @@ protected function get_schema_properties(): array {
'items' => [
'type' => 'object',
'properties' => [
'id' => [
'id' => [
'type' => 'numeric',
'description' => __( 'Product ID.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'title' => [
'title' => [
'type' => 'string',
'description' => __( 'Product title.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'visible' => [
'visible' => [
'type' => 'boolean',
'description' => __( 'Whether the product is set to be visible in the Merchant Center', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'status' => [
'status' => [
'type' => 'string',
'description' => __( 'The current sync status of the product.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'errors' => [
'image_url' => [
'type' => 'string',
'description' => __( 'The image url of the product.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'price' => [
'type' => 'string',
'description' => __( 'The price of the product.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'errors' => [
'type' => 'array',
'description' => __( 'Errors preventing the product from being synced to the Merchant Center.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
Expand Down
1 change: 1 addition & 0 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ protected function get_assets(): array {
'enableReports' => $this->enableReports(),
'dateFormat' => get_option( 'date_format' ),
'timeFormat' => get_option( 'time_format' ),
'siteLogoUrl' => wp_get_attachment_image_url( get_theme_mod( 'custom_logo' ), 'full' ),

]
);
Expand Down
23 changes: 17 additions & 6 deletions src/DB/ProductFeedQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class ProductFeedQueryHelper implements ContainerAwareInterface, Service {
*/
protected $product_repository;

/**
* Meta key for total sales.
*/
protected const META_KEY_TOTAL_SALES = 'total_sales';

/**
* ProductFeedQueryHelper constructor.
*
Expand Down Expand Up @@ -88,11 +93,13 @@ public function get( WP_REST_Request $request ): array {
$errors = $product_helper->get_validation_errors( $product );

$products[ $id ] = [
'id' => $id,
'title' => $product->get_name(),
'visible' => $product_helper->get_channel_visibility( $product ) !== ChannelVisibility::DONT_SYNC_AND_SHOW,
'status' => $product_helper->get_mc_status( $product ) ?: $product_helper->get_sync_status( $product ),
'errors' => array_values( $errors ),
'id' => $id,
'title' => $product->get_name(),
'visible' => $product_helper->get_channel_visibility( $product ) !== ChannelVisibility::DONT_SYNC_AND_SHOW,
'status' => $product_helper->get_mc_status( $product ) ?: $product_helper->get_sync_status( $product ),
'image_url' => wp_get_attachment_image_url( $product->get_image_id(), 'full' ),
'price' => $product->get_price(),
'errors' => array_values( $errors ),
];
}

Expand Down Expand Up @@ -165,8 +172,12 @@ protected function prepare_query_args(): array {
$args['meta_key'] = $this->prefix_meta_key( ProductMetaHandler::KEY_MC_STATUS );
$args['orderby'] = [ 'meta_value' => $this->get_order() ] + $args['orderby'];
break;
case 'total_sales':
$args['meta_key'] = self::META_KEY_TOTAL_SALES;
$args['orderby'] = [ 'meta_value_num' => $this->get_order() ] + $args['orderby'];
break;
default:
throw InvalidValue::not_in_allowed_list( 'orderby', [ 'title', 'id', 'visible', 'status' ] );
throw InvalidValue::not_in_allowed_list( 'orderby', [ 'title', 'id', 'visible', 'status', 'total_sales' ] );
}

return $args;
Expand Down

0 comments on commit 046f8fe

Please sign in to comment.