Skip to content

Commit

Permalink
Add onboarding task incentive badge (#7132)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaeldcom authored Sep 6, 2023
1 parent fc451c5 commit 7f796aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog/add-incentive-task-badge
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add onboarding task incentive badge.
2 changes: 2 additions & 0 deletions client/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ declare global {
id: string;
description: string;
tc_url: string;
task_header_content?: string;
task_badge?: string;
};
isWooPayStoreCountryAvailable: boolean;
};
Expand Down
18 changes: 18 additions & 0 deletions includes/class-wc-payments-incentives-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct( Database_Cache $database_cache ) {

add_action( 'admin_menu', [ $this, 'add_payments_menu_badge' ] );
add_filter( 'woocommerce_admin_allowed_promo_notes', [ $this, 'allowed_promo_notes' ] );
add_filter( 'woocommerce_admin_woopayments_onboarding_task_badge', [ $this, 'onboarding_task_badge' ] );
}

/**
Expand Down Expand Up @@ -77,6 +78,23 @@ public function allowed_promo_notes( $promo_notes = [] ): array {
return $promo_notes;
}

/**
* Adds the WooPayments incentive badge to the onboarding task.
*
* @param string $badge Current badge.
*
* @return string
*/
public function onboarding_task_badge( string $badge ): string {
$incentive = $this->get_cached_connect_incentive();
// Return early if there is no eligible incentive.
if ( empty( $incentive['id'] ) ) {
return $badge;
}

return $incentive['task_badge'] ?? $badge;
}

/**
* Gets and caches eligible connect incentive from the server.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test-class-wc-payments-incentives-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function tear_down() {
public function test_filters_registered_properly() {
$this->assertNotFalse( has_action( 'admin_menu', [ $this->incentives_service, 'add_payments_menu_badge' ] ) );
$this->assertNotFalse( has_filter( 'woocommerce_admin_allowed_promo_notes', [ $this->incentives_service, 'allowed_promo_notes' ] ) );
$this->assertNotFalse( has_filter( 'woocommerce_admin_woopayments_onboarding_task_badge', [ $this->incentives_service, 'onboarding_task_badge' ] ) );
}

public function test_add_payments_menu_badge_without_incentive() {
Expand Down Expand Up @@ -111,6 +112,32 @@ public function test_allowed_promo_notes_with_incentive() {
$this->assertContains( $this->mock_incentive_data['incentive']['id'], $promo_notes );
}

public function test_onboarding_task_badge_without_incentive() {
$this->mock_database_cache_with();

$badge = $this->incentives_service->onboarding_task_badge( '' );

$this->assertEmpty( $badge );
}

public function test_onboarding_task_badge_with_incentive_no_task_badge() {
$this->mock_database_cache_with( $this->mock_incentive_data );

$badge = $this->incentives_service->onboarding_task_badge( '' );

$this->assertEmpty( $badge );
}

public function test_onboarding_task_badge_with_incentive_and_task_badge() {
$incentive_data = $this->mock_incentive_data;
$incentive_data['incentive']['task_badge'] = 'task_badge';
$this->mock_database_cache_with( $incentive_data );

$badge = $this->incentives_service->onboarding_task_badge( '' );

$this->assertEquals( $badge, 'task_badge' );
}

public function test_get_cached_connect_incentive_non_supported_country() {
add_filter(
'woocommerce_countries_base_country',
Expand Down

0 comments on commit 7f796aa

Please sign in to comment.