2019-03-20 22:18:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Admin (Dashboard) Order Milestones Note Provider.
|
|
|
|
*
|
|
|
|
* Adds a note to the merchant's inbox when certain order milestones are reached.
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Admin_Notes_Order_Milestones
|
|
|
|
*/
|
|
|
|
class WC_Admin_Notes_Order_Milestones {
|
|
|
|
/**
|
|
|
|
* Name of the "first order" note.
|
|
|
|
*/
|
|
|
|
const FIRST_ORDER_NOTE_NAME = 'wc-admin-first-order';
|
|
|
|
|
2019-03-20 22:42:05 +00:00
|
|
|
/**
|
|
|
|
* Name of the "ten orders" note.
|
|
|
|
*/
|
|
|
|
const TEN_ORDERS_NOTE_NAME = 'wc-admin-ten-orders';
|
|
|
|
|
2019-03-21 17:27:39 +00:00
|
|
|
/**
|
|
|
|
* Name of the "other milestones" note.
|
|
|
|
*/
|
|
|
|
const ORDERS_MILESTONE_NOTE_NAME = 'wc-admin-orders-milestone';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Option key name to store last order milestone.
|
|
|
|
*/
|
|
|
|
const LAST_ORDER_MILESTONE_OPTION_KEY = 'wc_admin_last_orders_milestone';
|
|
|
|
|
2019-03-21 17:59:43 +00:00
|
|
|
/**
|
|
|
|
* Hook to process order milestones.
|
|
|
|
*/
|
|
|
|
const PROCESS_ORDERS_MILESTONE_HOOK = 'wc_admin_process_orders_milestone';
|
|
|
|
|
2019-03-20 22:18:00 +00:00
|
|
|
/**
|
|
|
|
* Allowed order statuses for calculating milestones.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $allowed_statuses = array(
|
|
|
|
'pending',
|
|
|
|
'processing',
|
|
|
|
'completed',
|
|
|
|
);
|
|
|
|
|
2019-03-20 22:42:05 +00:00
|
|
|
/**
|
|
|
|
* Orders count cache.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $orders_count = null;
|
|
|
|
|
2019-03-20 22:18:00 +00:00
|
|
|
/**
|
2019-03-21 17:27:39 +00:00
|
|
|
* Further order milestone thresholds.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $milestones = array(
|
|
|
|
100,
|
|
|
|
250,
|
|
|
|
500,
|
|
|
|
1000,
|
|
|
|
5000,
|
|
|
|
10000,
|
|
|
|
500000,
|
|
|
|
1000000,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delay hook attachment until after the WC post types have been registered.
|
|
|
|
*
|
|
|
|
* This is required for retrieving the order count.
|
2019-03-20 22:18:00 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2019-03-26 15:37:48 +00:00
|
|
|
/**
|
|
|
|
* Filter Order statuses that will count towards milestones.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param array $allowed_statuses Order statuses that will count towards milestones.
|
|
|
|
*/
|
|
|
|
$this->allowed_statuses = apply_filters( 'woocommerce_admin_order_milestone_statuses', $this->allowed_statuses );
|
|
|
|
|
2019-03-21 17:27:39 +00:00
|
|
|
add_action( 'woocommerce_after_register_post_type', array( $this, 'init' ) );
|
2019-04-08 19:10:39 +00:00
|
|
|
register_deactivation_hook( WC_ADMIN_PLUGIN_FILE, array( $this, 'clear_scheduled_event' ) );
|
2019-03-21 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook everything up.
|
|
|
|
*/
|
|
|
|
public function init() {
|
2019-03-21 17:59:43 +00:00
|
|
|
if ( ! wp_next_scheduled( self::PROCESS_ORDERS_MILESTONE_HOOK ) ) {
|
|
|
|
wp_schedule_event( time(), 'hourly', self::PROCESS_ORDERS_MILESTONE_HOOK );
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:27:39 +00:00
|
|
|
add_action( 'wc_admin_installed', array( $this, 'backfill_last_milestone' ) );
|
|
|
|
|
2019-04-02 14:34:22 +00:00
|
|
|
if ( $this->get_orders_count() <= 10 ) {
|
2019-03-20 22:42:05 +00:00
|
|
|
add_action( 'woocommerce_new_order', array( $this, 'first_two_milestones' ) );
|
|
|
|
}
|
2019-03-21 17:27:39 +00:00
|
|
|
|
2019-03-21 17:59:43 +00:00
|
|
|
add_action( self::PROCESS_ORDERS_MILESTONE_HOOK, array( $this, 'other_milestones' ) );
|
2019-03-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 19:10:39 +00:00
|
|
|
/**
|
|
|
|
* Clear out our hourly milestone hook upon plugin deactivation.
|
|
|
|
*/
|
|
|
|
public function clear_scheduled_event() {
|
|
|
|
wp_clear_scheduled_hook( self::PROCESS_ORDERS_MILESTONE_HOOK );
|
|
|
|
}
|
|
|
|
|
2019-03-20 22:18:00 +00:00
|
|
|
/**
|
|
|
|
* Get the total count of orders (in the allowed statuses).
|
|
|
|
*
|
2019-04-02 14:35:28 +00:00
|
|
|
* @param bool $no_cache Optional. Skip cache.
|
2019-03-20 22:18:00 +00:00
|
|
|
* @return int Total orders count.
|
|
|
|
*/
|
2019-04-02 14:35:28 +00:00
|
|
|
public function get_orders_count( $no_cache = false ) {
|
|
|
|
if ( $no_cache || is_null( $this->orders_count ) ) {
|
2019-04-10 08:49:00 +00:00
|
|
|
$status_counts = array_map( 'wc_orders_count', $this->allowed_statuses );
|
|
|
|
$this->orders_count = array_sum( $status_counts );
|
2019-03-20 22:42:05 +00:00
|
|
|
}
|
2019-03-20 22:18:00 +00:00
|
|
|
|
2019-03-20 22:42:05 +00:00
|
|
|
return $this->orders_count;
|
2019-03-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 22:42:05 +00:00
|
|
|
* Add a milestone notes for the store's first order and first 10 orders.
|
2019-03-20 22:18:00 +00:00
|
|
|
*
|
|
|
|
* @param int $order_id WC_Order ID.
|
|
|
|
*/
|
2019-03-20 22:42:05 +00:00
|
|
|
public function first_two_milestones( $order_id ) {
|
2019-03-20 22:18:00 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
|
|
|
|
|
|
|
// Make sure this is the first pending/processing/completed order.
|
|
|
|
if ( ! in_array( $order->get_status(), $this->allowed_statuses ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:35:28 +00:00
|
|
|
// Retrieve the orders count, forcing a cache refresh.
|
|
|
|
$orders_count = $this->get_orders_count( true );
|
2019-03-20 22:18:00 +00:00
|
|
|
|
|
|
|
if ( 1 === $orders_count ) {
|
|
|
|
// Add the first order note.
|
|
|
|
$note = new WC_Admin_Note();
|
|
|
|
$note->set_title( __( 'First order', 'woocommerce-admin' ) );
|
|
|
|
$note->set_content(
|
|
|
|
__( 'Congratulations on getting your first order from a customer! Learn how to manage your orders.', 'woocommerce-admin' )
|
|
|
|
);
|
|
|
|
$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
|
|
|
$note->set_icon( 'trophy' );
|
|
|
|
$note->set_name( self::FIRST_ORDER_NOTE_NAME );
|
|
|
|
$note->set_source( 'woocommerce-admin' );
|
|
|
|
$note->add_action( 'learn-more', __( 'Learn more', 'woocommerce-admin' ), 'https://docs.woocommerce.com/document/managing-orders/' );
|
|
|
|
$note->save();
|
|
|
|
}
|
2019-03-20 22:42:05 +00:00
|
|
|
|
|
|
|
if ( 10 === $orders_count ) {
|
|
|
|
// Add the first ten orders note.
|
|
|
|
$note = new WC_Admin_Note();
|
2019-03-21 17:27:39 +00:00
|
|
|
$note->set_title(
|
|
|
|
sprintf(
|
|
|
|
/* translators: Number of orders processed. */
|
|
|
|
__( 'Congratulations on processing %s orders!', 'woocommerce-admin' ),
|
|
|
|
wc_format_decimal( 10 )
|
|
|
|
)
|
|
|
|
);
|
2019-03-20 22:42:05 +00:00
|
|
|
$note->set_content(
|
|
|
|
__( "You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration.", 'woocommerce-admin' )
|
|
|
|
);
|
|
|
|
$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
|
|
|
$note->set_icon( 'trophy' );
|
|
|
|
$note->set_name( self::TEN_ORDERS_NOTE_NAME );
|
|
|
|
$note->set_source( 'woocommerce-admin' );
|
|
|
|
$note->add_action( 'browse', __( 'Browse', 'woocommerce-admin' ), 'https://woocommerce.com/success-stories/' );
|
|
|
|
$note->save();
|
|
|
|
}
|
2019-03-20 22:18:00 +00:00
|
|
|
}
|
2019-03-21 17:27:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Backfill the store's current milestone.
|
|
|
|
*
|
|
|
|
* Used to avoid celebrating milestones that were reached before plugin activation.
|
|
|
|
*/
|
|
|
|
public function backfill_last_milestone() {
|
|
|
|
$this->set_last_milestone( $this->get_current_milestone() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the store's last milestone.
|
|
|
|
*
|
|
|
|
* @return int Last milestone reached.
|
|
|
|
*/
|
|
|
|
public function get_last_milestone() {
|
|
|
|
return get_option( self::LAST_ORDER_MILESTONE_OPTION_KEY, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the last reached milestone.
|
|
|
|
*
|
|
|
|
* @param int $milestone Last milestone reached.
|
|
|
|
*/
|
|
|
|
public function set_last_milestone( $milestone ) {
|
|
|
|
update_option( self::LAST_ORDER_MILESTONE_OPTION_KEY, $milestone );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the current orders milestone.
|
|
|
|
*
|
|
|
|
* Based on the threshold values in $this->milestones.
|
|
|
|
*
|
|
|
|
* @return int Current orders milestone.
|
|
|
|
*/
|
|
|
|
public function get_current_milestone() {
|
|
|
|
$milestone_reached = 0;
|
|
|
|
$orders_count = $this->get_orders_count();
|
|
|
|
|
|
|
|
foreach ( $this->milestones as $milestone ) {
|
|
|
|
if ( $milestone <= $orders_count ) {
|
|
|
|
$milestone_reached = $milestone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $milestone_reached;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add milestone notes for other significant thresholds.
|
|
|
|
*/
|
|
|
|
public function other_milestones() {
|
|
|
|
$last_milestone = $this->get_last_milestone();
|
|
|
|
$current_milestone = $this->get_current_milestone();
|
|
|
|
|
|
|
|
if ( $current_milestone <= $last_milestone ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->set_last_milestone( $current_milestone );
|
|
|
|
|
2019-03-21 17:59:43 +00:00
|
|
|
// We only want one milestone note at any time.
|
|
|
|
WC_Admin_Notes::delete_notes_with_name( self::ORDERS_MILESTONE_NOTE_NAME );
|
|
|
|
|
2019-03-21 17:27:39 +00:00
|
|
|
// Add the milestone note.
|
|
|
|
$note = new WC_Admin_Note();
|
|
|
|
$note->set_title(
|
|
|
|
sprintf(
|
|
|
|
/* translators: Number of orders processed. */
|
|
|
|
__( 'Congratulations on processing %s orders!', 'woocommerce-admin' ),
|
|
|
|
wc_format_decimal( $current_milestone )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$note->set_content(
|
2019-03-22 15:10:13 +00:00
|
|
|
__( 'Another order milestone! Take a look at your Orders Report to review your orders to date.', 'woocommerce-admin' )
|
2019-03-21 17:27:39 +00:00
|
|
|
);
|
|
|
|
$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
|
|
|
$note->set_icon( 'trophy' );
|
|
|
|
$note->set_name( self::ORDERS_MILESTONE_NOTE_NAME );
|
|
|
|
$note->set_source( 'woocommerce-admin' );
|
2019-03-22 15:10:13 +00:00
|
|
|
$note->add_action( 'review-orders', __( 'Review your orders', 'woocommerce-admin' ), '?page=wc-admin#/analytics/orders' );
|
2019-03-21 17:27:39 +00:00
|
|
|
$note->save();
|
|
|
|
}
|
2019-03-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new WC_Admin_Notes_Order_Milestones();
|