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

Free Listings + Paid Ads: Update product feed API to return the bestselling product #1681

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
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