diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 986f43f009b..832eb22a597 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -130,6 +130,7 @@ function() { $this->container->get( DraftOrders::class )->init(); $this->container->get( CreateAccount::class )->init(); $this->container->get( ShippingController::class )->init(); + $this->container->get( JetpackWooCommerceAnalytics::class )->init(); // Load assets in admin and on the frontend. if ( ! $is_rest ) { @@ -138,7 +139,6 @@ function() { $this->container->get( AssetsController::class ); $this->container->get( Installer::class )->init(); $this->container->get( GoogleAnalytics::class )->init(); - $this->container->get( JetpackWooCommerceAnalytics::class )->init(); } // Load assets unless this is a request specifically for the store API. diff --git a/src/Domain/Services/JetpackWooCommerceAnalytics.php b/src/Domain/Services/JetpackWooCommerceAnalytics.php index 465d2c5a8c3..aba675d16e8 100644 --- a/src/Domain/Services/JetpackWooCommerceAnalytics.php +++ b/src/Domain/Services/JetpackWooCommerceAnalytics.php @@ -57,8 +57,12 @@ public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_ */ public function init() { add_action( 'init', array( $this, 'check_compatibility' ) ); - add_action( 'init', array( $this, 'init_if_compatible' ), 20 ); add_action( 'rest_pre_serve_request', array( $this, 'track_local_pickup' ), 10, 4 ); + + $is_rest = wc()->is_rest_api_request(); + if ( ! $is_rest ) { + add_action( 'init', array( $this, 'init_if_compatible' ), 20 ); + } } /** @@ -340,9 +344,9 @@ private function get_nested_blocks( $blocks, $exclude = array() ) { /** * Track local pickup settings changes via Store API * - * @param bool $served - * @param \WP_REST_Response $result - * @param \WP_REST_Request $request + * @param bool $served Whether the request has already been served. + * @param \WP_REST_Response $result The response object. + * @param \WP_REST_Request $request The request object. * @return bool */ public function track_local_pickup( $served, $result, $request ) { @@ -365,8 +369,8 @@ public function track_local_pickup( $served, $result, $request ) { $data = array( 'local_pickup_enabled' => 'yes' === $settings['enabled'] ? true : false, - 'title' => $settings['title'], - 'price' => '' === $settings['cost'], + 'title' => __( 'Local Pickup', 'woo-gutenberg-products-block' ) === $settings['title'], + 'price' => '' === $settings['cost'] ? true : false, 'cost' => '' === $settings['cost'] ? 0 : $settings['cost'], 'taxes' => $settings['tax_status'], 'total_pickup_locations' => count( $locations ), @@ -374,7 +378,7 @@ public function track_local_pickup( $served, $result, $request ) { array_filter( $locations, function( $location ) { - return 'yes' === $location['enabled']; } + return $location['enabled']; } ) ), );