Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add local pickup tracking #10841

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/Domain/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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.
Expand Down
55 changes: 54 additions & 1 deletion src/Domain/Services/JetpackWooCommerceAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\BlockTemplatesController;
use Automattic\WooCommerce\Blocks\Package;
use WC_Tracks;

/**
* Service class to integrate Blocks with the Jetpack WooCommerce Analytics extension,
Expand Down Expand Up @@ -56,7 +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 );
}
}

/**
Expand Down Expand Up @@ -334,4 +340,51 @@ private function get_nested_blocks( $blocks, $exclude = array() ) {
}
return $additional_blocks;
}

/**
* Track local pickup settings changes via Store API
*
* @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 ) {
if ( '/wp/v2/settings' !== $request->get_route() ) {
return $served;
}
// Param name here comes from the show_in_rest['name'] value when registering the setting.
if ( ! $request->get_param( 'pickup_location_settings' ) && ! $request->get_param( 'pickup_locations' ) ) {
return $served;
}

if ( ! $this->is_compatible ) {
return $served;
}

$event_name = 'wcadmin_local_pickup_save_changes';
senadir marked this conversation as resolved.
Show resolved Hide resolved

$settings = $request->get_param( 'pickup_location_settings' );
$locations = $request->get_param( 'pickup_locations' );

$data = array(
'local_pickup_enabled' => 'yes' === $settings['enabled'] ? true : false,
'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 ),
'pickup_locations_enabled' => count(
array_filter(
$locations,
function( $location ) {
return $location['enabled']; }
)
),
);

WC_Tracks::record_event( $event_name, $data );

return $served;
}
}