parent
057e118a34
commit
007bd21a35
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: update
|
||||||
|
|
||||||
|
Clean up Purchase task
|
|
@ -1,203 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
|
|
||||||
|
|
||||||
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProducts;
|
|
||||||
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes;
|
|
||||||
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
|
|
||||||
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Purchase Task
|
|
||||||
*/
|
|
||||||
class Purchase extends Task {
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
* @param TaskList $task_list Parent task list.
|
|
||||||
*/
|
|
||||||
public function __construct( $task_list ) {
|
|
||||||
parent::__construct( $task_list );
|
|
||||||
add_action( 'update_option_woocommerce_onboarding_profile', array( $this, 'clear_dismissal' ), 10, 2 );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear dismissal on onboarding product type changes.
|
|
||||||
*
|
|
||||||
* @param array $old_value Old value.
|
|
||||||
* @param array $new_value New value.
|
|
||||||
*/
|
|
||||||
public function clear_dismissal( $old_value, $new_value ) {
|
|
||||||
$product_types = isset( $new_value['product_types'] ) ? (array) $new_value['product_types'] : array();
|
|
||||||
$previous_product_types = isset( $old_value['product_types'] ) ? (array) $old_value['product_types'] : array();
|
|
||||||
|
|
||||||
if ( empty( array_diff( $product_types, $previous_product_types ) ) ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->undo_dismiss();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the task arguments.
|
|
||||||
* ID.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function get_id() {
|
|
||||||
return 'purchase';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Title.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function get_title() {
|
|
||||||
$products = $this->get_paid_products_and_themes();
|
|
||||||
$first_product = count( $products['purchaseable'] ) >= 1 ? $products['purchaseable'][0] : false;
|
|
||||||
|
|
||||||
if ( ! $first_product ) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$product_label = isset( $first_product['label'] ) ? $first_product['label'] : $first_product['title'];
|
|
||||||
$additional_count = count( $products['purchaseable'] ) - 1;
|
|
||||||
|
|
||||||
if ( $this->get_parent_option( 'use_completed_title' ) && $this->is_complete() ) {
|
|
||||||
return count( $products['purchaseable'] ) === 1
|
|
||||||
? sprintf(
|
|
||||||
/* translators: %1$s: a purchased product name */
|
|
||||||
__(
|
|
||||||
'You added %1$s',
|
|
||||||
'woocommerce'
|
|
||||||
),
|
|
||||||
$product_label
|
|
||||||
)
|
|
||||||
: sprintf(
|
|
||||||
/* translators: %1$s: a purchased product name, %2$d the number of other products purchased */
|
|
||||||
_n(
|
|
||||||
'You added %1$s and %2$d other product',
|
|
||||||
'You added %1$s and %2$d other products',
|
|
||||||
$additional_count,
|
|
||||||
'woocommerce'
|
|
||||||
),
|
|
||||||
$product_label,
|
|
||||||
$additional_count
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return count( $products['purchaseable'] ) === 1
|
|
||||||
? sprintf(
|
|
||||||
/* translators: %1$s: a purchaseable product name */
|
|
||||||
__(
|
|
||||||
'Add %s to my store',
|
|
||||||
'woocommerce'
|
|
||||||
),
|
|
||||||
$product_label
|
|
||||||
)
|
|
||||||
: sprintf(
|
|
||||||
/* translators: %1$s: a purchaseable product name, %2$d the number of other products to purchase */
|
|
||||||
_n(
|
|
||||||
'Add %1$s and %2$d more product to my store',
|
|
||||||
'Add %1$s and %2$d more products to my store',
|
|
||||||
$additional_count,
|
|
||||||
'woocommerce'
|
|
||||||
),
|
|
||||||
$product_label,
|
|
||||||
$additional_count
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Content.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function get_content() {
|
|
||||||
$products = $this->get_paid_products_and_themes();
|
|
||||||
|
|
||||||
if ( count( $products['remaining'] ) === 1 ) {
|
|
||||||
return isset( $products['purchaseable'][0]['description'] ) ? $products['purchaseable'][0]['description'] : $products['purchaseable'][0]['excerpt'];
|
|
||||||
}
|
|
||||||
return sprintf(
|
|
||||||
/* translators: %1$s: list of product names comma separated, %2%s the last product name */
|
|
||||||
__(
|
|
||||||
'Good choice! You chose to add %1$s and %2$s to your store.',
|
|
||||||
'woocommerce'
|
|
||||||
),
|
|
||||||
implode( ', ', array_slice( $products['remaining'], 0, -1 ) ) . ( count( $products['remaining'] ) > 2 ? ',' : '' ),
|
|
||||||
end( $products['remaining'] )
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Action label.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function get_action_label() {
|
|
||||||
return __( 'Purchase & install now', 'woocommerce' );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Time.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function get_time() {
|
|
||||||
return __( '2 minutes', 'woocommerce' );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Task completion.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function is_complete() {
|
|
||||||
$products = $this->get_paid_products_and_themes();
|
|
||||||
return count( $products['remaining'] ) === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dismissable.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function is_dismissable() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Task visibility.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function can_view() {
|
|
||||||
$products = $this->get_paid_products_and_themes();
|
|
||||||
return count( $products['purchaseable'] ) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get purchaseable and remaining products.
|
|
||||||
*
|
|
||||||
* @return array purchaseable and remaining products and themes.
|
|
||||||
*/
|
|
||||||
public static function get_paid_products_and_themes() {
|
|
||||||
$relevant_products = OnboardingProducts::get_relevant_products();
|
|
||||||
|
|
||||||
$profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() );
|
|
||||||
$theme = isset( $profiler_data['theme'] ) ? $profiler_data['theme'] : null;
|
|
||||||
$paid_theme = $theme ? OnboardingThemes::get_paid_theme_by_slug( $theme ) : null;
|
|
||||||
if ( $paid_theme ) {
|
|
||||||
|
|
||||||
$relevant_products['purchaseable'][] = $paid_theme;
|
|
||||||
|
|
||||||
if ( isset( $paid_theme['is_installed'] ) && false === $paid_theme['is_installed'] ) {
|
|
||||||
$relevant_products['remaining'][] = $paid_theme['title'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $relevant_products;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,186 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* Test the TaskList class.
|
|
||||||
*
|
|
||||||
* @package WooCommerce\Admin\Tests\OnboardingTasks
|
|
||||||
*/
|
|
||||||
|
|
||||||
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes;
|
|
||||||
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
|
|
||||||
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskList;
|
|
||||||
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Purchase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class WC_Admin_Tests_OnboardingTasks_TaskList
|
|
||||||
*/
|
|
||||||
class WC_Admin_Tests_OnboardingTasks_Task_Purchase extends WC_Unit_Test_Case {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Task list.
|
|
||||||
*
|
|
||||||
* @var Task|null
|
|
||||||
*/
|
|
||||||
protected $task = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup test data. Called before every test.
|
|
||||||
*/
|
|
||||||
public function setUp(): void {
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
$this->task = new Purchase( new TaskList() );
|
|
||||||
set_transient(
|
|
||||||
OnboardingThemes::THEMES_TRANSIENT,
|
|
||||||
array(
|
|
||||||
'free' => array(
|
|
||||||
'slug' => 'free',
|
|
||||||
'is_installed' => false,
|
|
||||||
),
|
|
||||||
'paid' => array(
|
|
||||||
'slug' => 'paid',
|
|
||||||
'id' => 12312,
|
|
||||||
'price' => '$79.00',
|
|
||||||
'title' => 'theme title',
|
|
||||||
'is_installed' => false,
|
|
||||||
),
|
|
||||||
'paid_installed' => array(
|
|
||||||
'slug' => 'paid_installed',
|
|
||||||
'id' => 12312,
|
|
||||||
'price' => '$79.00',
|
|
||||||
'title' => 'theme title',
|
|
||||||
'is_installed' => true,
|
|
||||||
),
|
|
||||||
'free_with_price' => array(
|
|
||||||
'slug' => 'free_with_price',
|
|
||||||
'id' => 12312,
|
|
||||||
'price' => '$0.00',
|
|
||||||
'title' => 'theme title',
|
|
||||||
'is_installed' => false,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tear down.
|
|
||||||
*/
|
|
||||||
public function tearDown(): void {
|
|
||||||
parent::tearDown();
|
|
||||||
delete_transient( OnboardingThemes::THEMES_TRANSIENT );
|
|
||||||
delete_option( OnboardingProfile::DATA_OPTION );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test is_complete function of Purchase task.
|
|
||||||
*/
|
|
||||||
public function test_is_complete_if_no_remaining_products() {
|
|
||||||
update_option( OnboardingProfile::DATA_OPTION, array( 'product_types' => array( 'physical' ) ) );
|
|
||||||
$this->assertEquals( true, $this->task->is_complete() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test is_complete function of Purchase task.
|
|
||||||
*/
|
|
||||||
public function test_is_not_complete_if_remaining_paid_products() {
|
|
||||||
update_option( OnboardingProfile::DATA_OPTION, array( 'product_types' => array( 'memberships' ) ) );
|
|
||||||
$this->assertEquals( false, $this->task->is_complete() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test is_complete function of Purchase task.
|
|
||||||
*/
|
|
||||||
public function test_is_complete_if_no_paid_themes() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array(),
|
|
||||||
'theme' => 'free',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( true, $this->task->is_complete() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test is_complete function of Purchase task.
|
|
||||||
*/
|
|
||||||
public function test_is_not_complete_if_paid_theme_that_is_not_installed() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array(),
|
|
||||||
'theme' => 'paid',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( false, $this->task->is_complete() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test is_complete function of Purchase task.
|
|
||||||
*/
|
|
||||||
public function test_is_complete_if_paid_theme_that_is_installed() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array(),
|
|
||||||
'theme' => 'paid_installed',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( true, $this->task->is_complete() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test is_complete function of Purchase task.
|
|
||||||
*/
|
|
||||||
public function test_is_complete_if_free_theme_with_set_price() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array(),
|
|
||||||
'theme' => 'free_with_price',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( true, $this->task->is_complete() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the task title for a single paid item.
|
|
||||||
*/
|
|
||||||
public function test_get_title_if_single_paid_item() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array(),
|
|
||||||
'theme' => 'paid',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( 'Add theme title to my store', $this->task->get_title() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the task title if 2 paid items exist.
|
|
||||||
*/
|
|
||||||
public function test_get_title_if_multiple_paid_themes() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array( 'memberships' ),
|
|
||||||
'theme' => 'paid',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( 'Add Memberships and 1 more product to my store', $this->task->get_title() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the task title if multiple additional paid items exist.
|
|
||||||
*/
|
|
||||||
public function test_get_title_if_multiple_paid_products() {
|
|
||||||
update_option(
|
|
||||||
OnboardingProfile::DATA_OPTION,
|
|
||||||
array(
|
|
||||||
'product_types' => array( 'memberships', 'bookings' ),
|
|
||||||
'theme' => 'paid',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEquals( 'Add Memberships and 2 more products to my store', $this->task->get_title() );
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue