Skip to content

Commit

Permalink
Update settings sync and handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JPry committed Mar 2, 2021
1 parent 78d68da commit f6eb5bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
35 changes: 32 additions & 3 deletions src/API/Google/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\ShippingTimeQuery as TimeQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Google_Service_ShoppingContent as ShoppingService;
use Google_Service_ShoppingContent_BusinessDayConfig;
use Google_Service_ShoppingContent_DeliveryTime;
use Google_Service_ShoppingContent_Price;
use Google_Service_ShoppingContent_RateGroup as RateGroup;
use Google_Service_ShoppingContent_Service;
use Google_Service_ShoppingContent_ShippingSettings;
use Google_Service_ShoppingContent_Value;
use Psr\Container\ContainerInterface;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -63,7 +65,11 @@ public function sync_shipping() {
$service = new Google_Service_ShoppingContent_Service();
$service->setActive( true );
$service->setDeliveryCountry( $country );
$service->setCurrency( $shipping_rate['currency'] );
$service->setRateGroups(
[
$this->create_rate_group_object( $shipping_rate['currency'], $shipping_rate['rate'] ),
]
);

if ( array_key_exists( $country, $times ) ) {
$service->setDeliveryTime( $this->create_time_object( intval( $times[ $country ] ) ) );
Expand All @@ -76,7 +82,8 @@ public function sync_shipping() {

/** @var ShoppingService $content_service */
$content_service = $this->container->get( ShoppingService::class );
$result = $content_service->shippingsettings->update(

return $content_service->shippingsettings->update(
$merchant_id,
$account_id,
$settings
Expand Down Expand Up @@ -110,4 +117,26 @@ protected function create_time_object( int $delivery_days ): Google_Service_Shop

return $time;
}

/**
* Create a rate group object for the shopping settings.
*
* @param string $currency
* @param mixed $rate
*
* @return RateGroup
*/
protected function create_rate_group_object( string $currency, $rate ): RateGroup {
$price = new Google_Service_ShoppingContent_Price();
$price->setCurrency( $currency );
$price->setValue( $rate );

$value = new Google_Service_ShoppingContent_Value();
$value->setFlatRate( $price );

$rate_group = new RateGroup();
$rate_group->setSingleValue( $value );

return $rate_group;
}
}
31 changes: 23 additions & 8 deletions src/API/Site/Controllers/MerchantCenter/SettingsSyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Settings;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers\BaseController;
use Automattic\WooCommerce\GoogleListingsAndAds\API\TransportMethods;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\WPErrorTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\RESTServer;
use Exception;
use WP_REST_Request as Request;
use WP_REST_Response as Response;

Expand All @@ -19,6 +21,8 @@
*/
class SettingsSyncController extends BaseController {

use WPErrorTrait;

/** @var Settings */
protected $settings;

Expand Down Expand Up @@ -56,15 +60,26 @@ public function register_routes() {
*/
protected function get_sync_endpoint_callback(): callable {
return function( Request $request ) {
$this->settings->sync_shipping();
try {
$this->settings->sync_shipping();

return new Response(
[
'status' => 'success',
'message' => __( 'Successfully synchronized settings with Google.', 'google-listings-and-ads' ),
],
201
);
return new Response(
[
'status' => 'success',
'message' => __( 'Successfully synchronized settings with Google.', 'google-listings-and-ads' ),
],
201
);
} catch ( Exception $e ) {
return $this->error_from_exception(
$e,
'gla_setting_sync_error',
[
'status' => 500,
'message' => $e->getMessage(),
]
);
}
};
}

Expand Down

0 comments on commit f6eb5bf

Please sign in to comment.