CustomizeStoreWithBlocks.php
2 years ago
CustomizingProductCatalog.php
1 year ago
EUVATNumber.php
2 years ago
EditProductsOnTheMove.php
2 years ago
EmailImprovements.php
1 year ago
FirstProduct.php
1 year ago
GivingFeedbackNotes.php
3 years ago
InstallJPAndWCSPlugins.php
4 weeks ago
LaunchChecklist.php
2 years ago
MagentoMigration.php
2 years ago
ManageOrdersOnTheGo.php
2 years ago
MarketingJetpack.php
4 weeks ago
MigrateFromShopify.php
2 years ago
MobileApp.php
2 years ago
NewSalesRecord.php
3 years ago
NoteActionForbiddenException.php
4 weeks ago
OnboardingPayments.php
2 years ago
OnlineClothingStore.php
2 years ago
OrderMilestones.php
2 years ago
PaymentsMoreInfoNeeded.php
5 months ago
PaymentsRemindMeLater.php
5 months ago
PerformanceOnMobile.php
2 years ago
PersonalizeStore.php
3 years ago
RealTimeOrderAlerts.php
2 years ago
ScheduledUpdatesPromotion.php
5 months ago
SellingOnlineCourses.php
2 years ago
TrackingOptIn.php
1 year ago
UnsecuredReportFiles.php
2 years ago
WooCommercePayments.php
2 years ago
WooCommerceSubscriptions.php
2 years ago
WooSubscriptionsNotes.php
1 year ago
OrderMilestones.php
370 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin (Dashboard) Order Milestones Note Provider. |
| 4 | * |
| 5 | * Adds a note to the merchant's inbox when certain order milestones are reached. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin\Notes; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 13 | use Automattic\WooCommerce\Admin\Notes\Notes; |
| 14 | /** |
| 15 | * Order_Milestones |
| 16 | */ |
| 17 | class OrderMilestones { |
| 18 | /** |
| 19 | * Name of the "other milestones" note. |
| 20 | */ |
| 21 | const NOTE_NAME = 'wc-admin-orders-milestone'; |
| 22 | |
| 23 | /** |
| 24 | * Option key name to store last order milestone. |
| 25 | */ |
| 26 | const LAST_ORDER_MILESTONE_OPTION_KEY = 'woocommerce_admin_last_orders_milestone'; |
| 27 | |
| 28 | /** |
| 29 | * Hook to process order milestones. |
| 30 | */ |
| 31 | const PROCESS_ORDERS_MILESTONE_HOOK = 'wc_admin_process_orders_milestone'; |
| 32 | |
| 33 | /** |
| 34 | * Allowed order statuses for calculating milestones. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $allowed_statuses = array( |
| 39 | 'pending', |
| 40 | 'processing', |
| 41 | 'completed', |
| 42 | ); |
| 43 | |
| 44 | /** |
| 45 | * Orders count cache. |
| 46 | * |
| 47 | * @var int |
| 48 | */ |
| 49 | protected $orders_count = null; |
| 50 | |
| 51 | /** |
| 52 | * Further order milestone thresholds. |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | protected $milestones = array( |
| 57 | 1, |
| 58 | 10, |
| 59 | 100, |
| 60 | 250, |
| 61 | 500, |
| 62 | 1000, |
| 63 | 5000, |
| 64 | 10000, |
| 65 | 500000, |
| 66 | 1000000, |
| 67 | ); |
| 68 | |
| 69 | /** |
| 70 | * Delay hook attachment until after the WC post types have been registered. |
| 71 | * |
| 72 | * This is required for retrieving the order count. |
| 73 | */ |
| 74 | public function __construct() { |
| 75 | /** |
| 76 | * Filter Order statuses that will count towards milestones. |
| 77 | * |
| 78 | * @since 3.5.0 |
| 79 | * |
| 80 | * @param array $allowed_statuses Order statuses that will count towards milestones. |
| 81 | */ |
| 82 | $this->allowed_statuses = apply_filters( 'woocommerce_admin_order_milestone_statuses', $this->allowed_statuses ); |
| 83 | |
| 84 | add_action( 'woocommerce_after_register_post_type', array( $this, 'init' ) ); |
| 85 | register_deactivation_hook( WC_PLUGIN_FILE, array( $this, 'clear_scheduled_event' ) ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Hook everything up. |
| 90 | */ |
| 91 | public function init() { |
| 92 | if ( ! wp_next_scheduled( self::PROCESS_ORDERS_MILESTONE_HOOK ) ) { |
| 93 | wp_schedule_event( time(), 'hourly', self::PROCESS_ORDERS_MILESTONE_HOOK ); |
| 94 | } |
| 95 | |
| 96 | add_action( 'wc_admin_installed', array( $this, 'backfill_last_milestone' ) ); |
| 97 | |
| 98 | add_action( self::PROCESS_ORDERS_MILESTONE_HOOK, array( $this, 'possibly_add_note' ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Clear out our hourly milestone hook upon plugin deactivation. |
| 103 | */ |
| 104 | public function clear_scheduled_event() { |
| 105 | wp_clear_scheduled_hook( self::PROCESS_ORDERS_MILESTONE_HOOK ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the total count of orders (in the allowed statuses). |
| 110 | * |
| 111 | * @param bool $no_cache Optional. Skip cache. |
| 112 | * @return int Total orders count. |
| 113 | */ |
| 114 | public function get_orders_count( $no_cache = false ) { |
| 115 | if ( $no_cache || is_null( $this->orders_count ) ) { |
| 116 | $status_counts = array_map( 'wc_orders_count', $this->allowed_statuses ); |
| 117 | $this->orders_count = array_sum( $status_counts ); |
| 118 | } |
| 119 | |
| 120 | return $this->orders_count; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Backfill the store's current milestone. |
| 125 | * |
| 126 | * Used to avoid celebrating milestones that were reached before plugin activation. |
| 127 | */ |
| 128 | public function backfill_last_milestone() { |
| 129 | // If the milestone notes have been disabled via filter, bail. |
| 130 | if ( ! $this->are_milestones_enabled() ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | $this->set_last_milestone( $this->get_current_milestone() ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the store's last milestone. |
| 139 | * |
| 140 | * @return int Last milestone reached. |
| 141 | */ |
| 142 | public function get_last_milestone() { |
| 143 | return get_option( self::LAST_ORDER_MILESTONE_OPTION_KEY, 0 ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Update the last reached milestone. |
| 148 | * |
| 149 | * @param int $milestone Last milestone reached. |
| 150 | */ |
| 151 | public function set_last_milestone( $milestone ) { |
| 152 | update_option( self::LAST_ORDER_MILESTONE_OPTION_KEY, $milestone ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Calculate the current orders milestone. |
| 157 | * |
| 158 | * Based on the threshold values in $this->milestones. |
| 159 | * |
| 160 | * @return int Current orders milestone. |
| 161 | */ |
| 162 | public function get_current_milestone() { |
| 163 | $milestone_reached = 0; |
| 164 | $orders_count = $this->get_orders_count(); |
| 165 | |
| 166 | foreach ( $this->milestones as $milestone ) { |
| 167 | if ( $milestone <= $orders_count ) { |
| 168 | $milestone_reached = $milestone; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return $milestone_reached; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the appropriate note title for a given milestone. |
| 177 | * |
| 178 | * @param int $milestone Order milestone. |
| 179 | * @return string Note title for the milestone. |
| 180 | */ |
| 181 | public static function get_note_title_for_milestone( $milestone ) { |
| 182 | switch ( $milestone ) { |
| 183 | case 1: |
| 184 | return __( 'First order received', 'woocommerce' ); |
| 185 | case 10: |
| 186 | case 100: |
| 187 | case 250: |
| 188 | case 500: |
| 189 | case 1000: |
| 190 | case 5000: |
| 191 | case 10000: |
| 192 | case 500000: |
| 193 | case 1000000: |
| 194 | return sprintf( |
| 195 | /* translators: Number of orders processed. */ |
| 196 | __( 'Congratulations on processing %s orders!', 'woocommerce' ), |
| 197 | wc_format_decimal( $milestone ) |
| 198 | ); |
| 199 | default: |
| 200 | return ''; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get the appropriate note content for a given milestone. |
| 206 | * |
| 207 | * @param int $milestone Order milestone. |
| 208 | * @return string Note content for the milestone. |
| 209 | */ |
| 210 | public static function get_note_content_for_milestone( $milestone ) { |
| 211 | switch ( $milestone ) { |
| 212 | case 1: |
| 213 | return __( 'Congratulations on getting your first order! Now is a great time to learn how to manage your orders.', 'woocommerce' ); |
| 214 | case 10: |
| 215 | return __( "You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration.", 'woocommerce' ); |
| 216 | case 100: |
| 217 | case 250: |
| 218 | case 500: |
| 219 | case 1000: |
| 220 | case 5000: |
| 221 | case 10000: |
| 222 | case 500000: |
| 223 | case 1000000: |
| 224 | return __( 'Another order milestone! Take a look at your Orders Report to review your orders to date.', 'woocommerce' ); |
| 225 | default: |
| 226 | return ''; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get the appropriate note action for a given milestone. |
| 232 | * |
| 233 | * @param int $milestone Order milestone. |
| 234 | * @return array Note actoion (name, label, query) for the milestone. |
| 235 | */ |
| 236 | public static function get_note_action_for_milestone( $milestone ) { |
| 237 | switch ( $milestone ) { |
| 238 | case 1: |
| 239 | return array( |
| 240 | 'name' => 'learn-more', |
| 241 | 'label' => __( 'Learn more', 'woocommerce' ), |
| 242 | 'query' => 'https://woocommerce.com/document/managing-orders/?utm_source=inbox&utm_medium=product', |
| 243 | ); |
| 244 | case 10: |
| 245 | return array( |
| 246 | 'name' => 'browse', |
| 247 | 'label' => __( 'Browse', 'woocommerce' ), |
| 248 | 'query' => 'https://woocommerce.com/success-stories/?utm_source=inbox&utm_medium=product', |
| 249 | ); |
| 250 | case 100: |
| 251 | case 250: |
| 252 | case 500: |
| 253 | case 1000: |
| 254 | case 5000: |
| 255 | case 10000: |
| 256 | case 500000: |
| 257 | case 1000000: |
| 258 | return array( |
| 259 | 'name' => 'review-orders', |
| 260 | 'label' => __( 'Review your orders', 'woocommerce' ), |
| 261 | 'query' => '?page=wc-admin&path=/analytics/orders', |
| 262 | ); |
| 263 | default: |
| 264 | return array( |
| 265 | 'name' => '', |
| 266 | 'label' => '', |
| 267 | 'query' => '', |
| 268 | ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Convenience method to see if the milestone notes are enabled. |
| 274 | * |
| 275 | * @return boolean True if milestone notifications are enabled. |
| 276 | */ |
| 277 | public function are_milestones_enabled() { |
| 278 | /** |
| 279 | * Filter to allow for disabling order milestones. |
| 280 | * |
| 281 | * @since 3.7.0 |
| 282 | * |
| 283 | * @param boolean default true |
| 284 | */ |
| 285 | $milestone_notes_enabled = apply_filters( 'woocommerce_admin_order_milestones_enabled', true ); |
| 286 | |
| 287 | return $milestone_notes_enabled; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Get the note. This is used for localizing the note. |
| 292 | * |
| 293 | * @return Note |
| 294 | */ |
| 295 | public static function get_note() { |
| 296 | $note = Notes::get_note_by_name( self::NOTE_NAME ); |
| 297 | if ( ! $note ) { |
| 298 | return false; |
| 299 | } |
| 300 | $content_data = $note->get_content_data(); |
| 301 | if ( ! isset( $content_data->current_milestone ) ) { |
| 302 | return false; |
| 303 | } |
| 304 | return self::get_note_by_milestone( |
| 305 | $content_data->current_milestone |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Get the note by milestones. |
| 311 | * |
| 312 | * @param int $current_milestone Current milestone. |
| 313 | * |
| 314 | * @return Note |
| 315 | */ |
| 316 | public static function get_note_by_milestone( $current_milestone ) { |
| 317 | $content_data = (object) array( |
| 318 | 'current_milestone' => $current_milestone, |
| 319 | ); |
| 320 | |
| 321 | $note = new Note(); |
| 322 | $note->set_title( self::get_note_title_for_milestone( $current_milestone ) ); |
| 323 | $note->set_content( self::get_note_content_for_milestone( $current_milestone ) ); |
| 324 | $note->set_content_data( $content_data ); |
| 325 | $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); |
| 326 | $note->set_name( self::NOTE_NAME ); |
| 327 | $note->set_source( 'woocommerce-admin' ); |
| 328 | $note_action = self::get_note_action_for_milestone( $current_milestone ); |
| 329 | $note->add_action( $note_action['name'], $note_action['label'], $note_action['query'] ); |
| 330 | return $note; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Checks if a note can and should be added. |
| 335 | * |
| 336 | * @return bool |
| 337 | */ |
| 338 | public function can_be_added() { |
| 339 | // If the milestone notes have been disabled via filter, bail. |
| 340 | if ( ! $this->are_milestones_enabled() ) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | $last_milestone = $this->get_last_milestone(); |
| 345 | $current_milestone = $this->get_current_milestone(); |
| 346 | |
| 347 | if ( $current_milestone <= $last_milestone ) { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Add milestone notes for other significant thresholds. |
| 356 | */ |
| 357 | public function possibly_add_note() { |
| 358 | if ( ! self::can_be_added() ) { |
| 359 | return; |
| 360 | } |
| 361 | $current_milestone = $this->get_current_milestone(); |
| 362 | $this->set_last_milestone( $current_milestone ); |
| 363 | |
| 364 | // We only want one milestone note at any time. |
| 365 | Notes::delete_notes_with_name( self::NOTE_NAME ); |
| 366 | $note = $this->get_note_by_milestone( $current_milestone ); |
| 367 | $note->save(); |
| 368 | } |
| 369 | } |
| 370 |