2018-10-18 00:24:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Admin (Dashboard) New Sales Record Note Provider.
|
|
|
|
*
|
|
|
|
* Adds a note to the merchant's inbox when the previous day's sales are a new record.
|
|
|
|
*/
|
|
|
|
|
2019-07-31 19:47:32 +00:00
|
|
|
namespace Automattic\WooCommerce\Admin\Notes;
|
|
|
|
|
2018-10-18 00:24:19 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
2020-09-28 04:35:10 +00:00
|
|
|
* New_Sales_Record
|
2018-10-18 00:24:19 +00:00
|
|
|
*/
|
2020-10-28 17:12:14 +00:00
|
|
|
class NewSalesRecord {
|
2020-05-22 13:48:40 +00:00
|
|
|
/**
|
|
|
|
* Note traits.
|
|
|
|
*/
|
|
|
|
use NoteTraits;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the note for use in the database.
|
|
|
|
*/
|
|
|
|
const NOTE_NAME = 'wc-admin-new-sales-record';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Option name for the sales record date in ISO 8601 (YYYY-MM-DD) date.
|
|
|
|
*/
|
|
|
|
const RECORD_DATE_OPTION_KEY = 'woocommerce_sales_record_date';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Option name for the sales record amount.
|
|
|
|
*/
|
2018-10-18 00:24:19 +00:00
|
|
|
const RECORD_AMOUNT_OPTION_KEY = 'woocommerce_sales_record_amount';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the total of yesterday's sales.
|
|
|
|
*
|
|
|
|
* @param string $date Date for sales to sum (i.e. YYYY-MM-DD).
|
|
|
|
* @return floatval
|
|
|
|
*/
|
2018-10-18 23:41:43 +00:00
|
|
|
public static function sum_sales_for_date( $date ) {
|
2019-07-31 19:47:32 +00:00
|
|
|
$order_query = new \WC_Order_Query( array( 'date_created' => $date ) );
|
2018-10-18 00:24:19 +00:00
|
|
|
$orders = $order_query->get_orders();
|
|
|
|
$total = 0;
|
|
|
|
|
|
|
|
foreach ( (array) $orders as $order ) {
|
|
|
|
$total += $order->get_total();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Possibly add a sales record note.
|
|
|
|
*/
|
2020-05-22 13:48:40 +00:00
|
|
|
public static function possibly_add_note() {
|
2019-06-11 15:12:16 +00:00
|
|
|
/**
|
|
|
|
* Filter to allow for disabling sales record milestones.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @param boolean default true
|
|
|
|
*/
|
|
|
|
$sales_record_notes_enabled = apply_filters( 'woocommerce_admin_sales_record_milestone_enabled', true );
|
|
|
|
|
|
|
|
if ( ! $sales_record_notes_enabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-22 15:06:14 +00:00
|
|
|
$yesterday = gmdate( 'Y-m-d', current_time( 'timestamp', 0 ) - DAY_IN_SECONDS );
|
2018-10-18 23:41:43 +00:00
|
|
|
$total = self::sum_sales_for_date( $yesterday );
|
2018-10-18 00:24:19 +00:00
|
|
|
|
|
|
|
// No sales yesterday? Bail.
|
2018-10-18 22:52:55 +00:00
|
|
|
if ( 0 >= $total ) {
|
2018-10-18 00:24:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:52:55 +00:00
|
|
|
$record_date = get_option( self::RECORD_DATE_OPTION_KEY, '' );
|
|
|
|
$record_amt = floatval( get_option( self::RECORD_AMOUNT_OPTION_KEY, 0 ) );
|
2018-10-18 00:24:19 +00:00
|
|
|
|
|
|
|
// No previous entry? Just enter what we have and return without generating a note.
|
|
|
|
if ( empty( $record_date ) ) {
|
2018-10-18 22:52:55 +00:00
|
|
|
update_option( self::RECORD_DATE_OPTION_KEY, $yesterday );
|
|
|
|
update_option( self::RECORD_AMOUNT_OPTION_KEY, $total );
|
|
|
|
return;
|
2018-10-18 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if yesterdays total bested the record, update AND generate a note.
|
|
|
|
if ( $total > $record_amt ) {
|
2018-10-18 22:52:55 +00:00
|
|
|
update_option( self::RECORD_DATE_OPTION_KEY, $yesterday );
|
|
|
|
update_option( self::RECORD_AMOUNT_OPTION_KEY, $total );
|
2018-10-18 00:24:19 +00:00
|
|
|
|
2019-11-22 15:06:14 +00:00
|
|
|
$formatted_yesterday = gmdate( 'F jS', strtotime( $yesterday ) );
|
2018-10-25 22:06:54 +00:00
|
|
|
$formatted_total = html_entity_decode( wp_strip_all_tags( wc_price( $total ) ) );
|
2019-11-22 15:06:14 +00:00
|
|
|
$formatted_record_date = gmdate( 'F jS', strtotime( $record_date ) );
|
2018-10-25 22:06:54 +00:00
|
|
|
$formatted_record_amt = html_entity_decode( wp_strip_all_tags( wc_price( $record_amt ) ) );
|
2018-10-18 00:24:19 +00:00
|
|
|
|
2018-10-18 22:52:55 +00:00
|
|
|
$content = sprintf(
|
|
|
|
/* translators: 1 and 4: Date (e.g. October 16th), 2 and 3: Amount (e.g. $160.00) */
|
2021-08-13 18:54:24 +00:00
|
|
|
__( 'Woohoo, %1$s was your record day for sales! Net sales was %2$s beating the previous record of %3$s set on %4$s.', 'woocommerce-admin' ),
|
2018-10-18 00:24:19 +00:00
|
|
|
$formatted_yesterday,
|
|
|
|
$formatted_total,
|
|
|
|
$formatted_record_amt,
|
|
|
|
$formatted_record_date
|
|
|
|
);
|
|
|
|
|
2018-10-18 22:52:55 +00:00
|
|
|
$content_data = (object) array(
|
|
|
|
'old_record_date' => $record_date,
|
|
|
|
'old_record_amt' => $record_amt,
|
|
|
|
'new_record_date' => $yesterday,
|
|
|
|
'new_record_amt' => $total,
|
|
|
|
);
|
|
|
|
|
|
|
|
// We only want one sales record note at any time in the inbox, so we delete any other first.
|
2020-09-28 04:35:10 +00:00
|
|
|
Notes::delete_notes_with_name( self::NOTE_NAME );
|
2018-10-18 22:52:55 +00:00
|
|
|
|
2019-07-01 08:15:46 +00:00
|
|
|
$report_url = '?page=wc-admin&path=/analytics/revenue&period=custom&compare=previous_year&after=' . $yesterday . '&before=' . $yesterday;
|
2019-06-12 02:37:45 +00:00
|
|
|
|
2018-10-18 22:52:55 +00:00
|
|
|
// And now, create our new note.
|
2020-09-28 04:35:10 +00:00
|
|
|
$note = new Note();
|
2019-03-13 17:14:02 +00:00
|
|
|
$note->set_title( __( 'New sales record!', 'woocommerce-admin' ) );
|
2018-10-18 22:52:55 +00:00
|
|
|
$note->set_content( $content );
|
|
|
|
$note->set_content_data( $content_data );
|
2020-09-28 04:35:10 +00:00
|
|
|
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
2018-10-23 17:30:33 +00:00
|
|
|
$note->set_name( self::NOTE_NAME );
|
2019-03-12 13:13:20 +00:00
|
|
|
$note->set_source( 'woocommerce-admin' );
|
2019-06-12 02:37:45 +00:00
|
|
|
$note->add_action( 'view-report', __( 'View report', 'woocommerce-admin' ), $report_url );
|
2018-10-18 00:24:19 +00:00
|
|
|
$note->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|