From b80b65bd460f93bf845da81baff0b7890849857a Mon Sep 17 00:00:00 2001 From: Moon Date: Wed, 20 Jan 2021 22:11:53 -0800 Subject: [PATCH] Add a new note a day after adding the first product (https://github.com/woocommerce/woocommerce-admin/pull/6032) * Add a new note a day after adding the first product * Do not add note if store has been active for more than 14 days --- plugins/woocommerce-admin/src/Events.php | 2 + .../src/Notes/CustomizingProductCatalog.php | 80 +++++++++++ ...s-wc-tests-customizing-product-catalog.php | 131 ++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 plugins/woocommerce-admin/src/Notes/CustomizingProductCatalog.php create mode 100644 plugins/woocommerce-admin/tests/notes/class-wc-tests-customizing-product-catalog.php diff --git a/plugins/woocommerce-admin/src/Events.php b/plugins/woocommerce-admin/src/Events.php index a8d375d2291..d27c273990b 100644 --- a/plugins/woocommerce-admin/src/Events.php +++ b/plugins/woocommerce-admin/src/Events.php @@ -11,6 +11,7 @@ defined( 'ABSPATH' ) || exit; use \Automattic\WooCommerce\Admin\Notes\AddingAndManangingProducts; use \Automattic\WooCommerce\Admin\Notes\ChooseNiche; use \Automattic\WooCommerce\Admin\Notes\ChoosingTheme; +use \Automattic\WooCommerce\Admin\Notes\CustomizingProductCatalog; use \Automattic\WooCommerce\Admin\Notes\GivingFeedbackNotes; use \Automattic\WooCommerce\Admin\Notes\InsightFirstProductAndPayment; use \Automattic\WooCommerce\Admin\Notes\MobileApp; @@ -141,6 +142,7 @@ class Events { InsightFirstProductAndPayment::possibly_add_note(); AddFirstProduct::possibly_add_note(); AddingAndManangingProducts::possibly_add_note(); + CustomizingProductCatalog::possibly_add_note(); GettingStartedInEcommerceWebinar::possibly_add_note(); } diff --git a/plugins/woocommerce-admin/src/Notes/CustomizingProductCatalog.php b/plugins/woocommerce-admin/src/Notes/CustomizingProductCatalog.php new file mode 100644 index 00000000000..57b21b9e8f7 --- /dev/null +++ b/plugins/woocommerce-admin/src/Notes/CustomizingProductCatalog.php @@ -0,0 +1,80 @@ + 1, + 'paginate' => true, + 'status' => array( 'publish' ), + 'orderby' => 'post_date', + 'order' => 'DESC', + ) + ); + + $products = $query->get_products(); + + // we need at least 1 product. + if ( 0 === $products->total ) { + return; + } + + $product = $products->products[0]; + $created_timestamp = $product->get_date_created()->getTimestamp(); + $is_a_day_old = ( time() - $created_timestamp ) >= DAY_IN_SECONDS; + + // the product must be at least 1 day old. + if ( ! $is_a_day_old ) { + return; + } + + // store must not been active more than 14 days. + if ( self::wc_admin_active_for( DAY_IN_SECONDS * 14 ) ) { + return; + } + + $note = new Note(); + $note->set_title( __( 'How to customize your product catalog', 'woocommerce-admin' ) ); + $note->set_content( __( 'You want your product catalog and images to look great and align with your brand. This guide will give you all the tips you need to get your products looking great in your store.', 'woocommerce-admin' ) ); + $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); + $note->set_name( self::NOTE_NAME ); + $note->set_content_data( (object) array() ); + $note->set_source( 'woocommerce-admin' ); + $note->add_action( + 'day-after-first-product', + __( 'Learn more', 'woocommerce-admin' ), + 'https://docs.woocommerce.com/document/woocommerce-customizer/?utm_source=inbox' + ); + + return $note; + } +} diff --git a/plugins/woocommerce-admin/tests/notes/class-wc-tests-customizing-product-catalog.php b/plugins/woocommerce-admin/tests/notes/class-wc-tests-customizing-product-catalog.php new file mode 100644 index 00000000000..5ce3a4beb33 --- /dev/null +++ b/plugins/woocommerce-admin/tests/notes/class-wc-tests-customizing-product-catalog.php @@ -0,0 +1,131 @@ +empty_posts(); + $this->instance = new CustomizingProductCatalog(); + } + + /** + * Empty wp_posts table + */ + private function empty_posts() { + global $wpdb; + $wpdb->query( "delete from {$wpdb->prefix}posts" ); + } + + /** + * Given 0 products + * When get_note() is called + * Then it should return null + */ + public function test_it_does_not_add_note_if_product_count_is_zero() { + // Given -- setUp() takes care of it. + // When. + $note = $this->instance->get_note(); + + // Then. + $this->assertNull( $note ); + } + + /** + * Given a fresh product + * When get_note() is called + * Then it should return null + */ + public function test_it_does_not_add_note_if_prouduct_is_less_than_a_day_old() { + // Given. + wp_insert_post( + array( + 'post_title' => 'a product', + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_content' => '', + ) + ); + + // When. + $note = $this->instance->get_note(); + + // Then. + $this->assertNull( $note ); + } + + /** + * Given a store that has been active for 14+ days + * When get_note() is called + * Then it should return null + */ + public function test_it_does_not_add_note_if_store_has_been_active_for_14_days_or_more() { + // Given. + update_option( 'woocommerce_admin_install_timestamp', time() - ( DAY_IN_SECONDS * 14 ) ); + wp_insert_post( + array( + 'post_title' => 'a product', + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_content' => '', + ) + ); + + // When. + $note = $this->instance->get_note(); + + // Then. + $this->assertNull( $note ); + } + + /** + * Given a store that has been active for less than 14 days + * and a product that is older than day + * + * When get_note() is called + * Then it should return an instance of Note + */ + public function test_it_adds_note() { + // Given. + update_option( 'woocommerce_admin_install_timestamp', time() - ( DAY_IN_SECONDS * 13 ) ); + $day_before = gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ); + $product = new \WC_Product(); + $product->set_props( + array( + 'name' => 'test', + ) + ); + $product_id = $product->save(); + wp_update_post( + array( + 'ID' => $product_id, + 'post_date' => $day_before, + 'post_date_gmt' => get_gmt_from_date( $day_before ), + ) + ); + + // When. + $note = $this->instance->get_note(); + // Then. + $this->assertInstanceOf( Note::class, $note ); + } +}