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
NewSalesRecord.php
180 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin (Dashboard) New Sales Record Note Provider. |
| 4 | * |
| 5 | * Adds a note to the merchant's inbox when the previous day's sales are a new record. |
| 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 | use Automattic\WooCommerce\Admin\Notes\NoteTraits; |
| 15 | |
| 16 | /** |
| 17 | * New_Sales_Record |
| 18 | */ |
| 19 | class NewSalesRecord { |
| 20 | /** |
| 21 | * Note traits. |
| 22 | */ |
| 23 | use NoteTraits; |
| 24 | |
| 25 | /** |
| 26 | * Name of the note for use in the database. |
| 27 | */ |
| 28 | const NOTE_NAME = 'wc-admin-new-sales-record'; |
| 29 | |
| 30 | /** |
| 31 | * Option name for the sales record date in ISO 8601 (YYYY-MM-DD) date. |
| 32 | */ |
| 33 | const RECORD_DATE_OPTION_KEY = 'woocommerce_sales_record_date'; |
| 34 | |
| 35 | /** |
| 36 | * Option name for the sales record amount. |
| 37 | */ |
| 38 | const RECORD_AMOUNT_OPTION_KEY = 'woocommerce_sales_record_amount'; |
| 39 | |
| 40 | /** |
| 41 | * Returns the total of yesterday's sales. |
| 42 | * |
| 43 | * @param string $date Date for sales to sum (i.e. YYYY-MM-DD). |
| 44 | * @return floatval |
| 45 | */ |
| 46 | public static function sum_sales_for_date( $date ) { |
| 47 | $order_query = new \WC_Order_Query( array( 'date_created' => $date ) ); |
| 48 | $orders = $order_query->get_orders(); |
| 49 | $total = 0; |
| 50 | |
| 51 | foreach ( (array) $orders as $order ) { |
| 52 | $total += $order->get_total(); |
| 53 | } |
| 54 | |
| 55 | return $total; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Possibly add a sales record note. |
| 60 | */ |
| 61 | public static function possibly_add_note() { |
| 62 | /** |
| 63 | * Filter to allow for disabling sales record milestones. |
| 64 | * |
| 65 | * @since 3.7.0 |
| 66 | * |
| 67 | * @param boolean default true |
| 68 | */ |
| 69 | $sales_record_notes_enabled = apply_filters( 'woocommerce_admin_sales_record_milestone_enabled', true ); |
| 70 | |
| 71 | if ( ! $sales_record_notes_enabled ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $yesterday = gmdate( 'Y-m-d', current_time( 'timestamp', 0 ) - DAY_IN_SECONDS ); |
| 76 | $total = self::sum_sales_for_date( $yesterday ); |
| 77 | |
| 78 | // No sales yesterday? Bail. |
| 79 | if ( 0 >= $total ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $record_date = get_option( self::RECORD_DATE_OPTION_KEY, '' ); |
| 84 | $record_amt = floatval( get_option( self::RECORD_AMOUNT_OPTION_KEY, 0 ) ); |
| 85 | |
| 86 | // No previous entry? Just enter what we have and return without generating a note. |
| 87 | if ( empty( $record_date ) ) { |
| 88 | update_option( self::RECORD_DATE_OPTION_KEY, $yesterday ); |
| 89 | update_option( self::RECORD_AMOUNT_OPTION_KEY, $total ); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Otherwise, if yesterdays total bested the record, update AND generate a note. |
| 94 | if ( $total > $record_amt ) { |
| 95 | update_option( self::RECORD_DATE_OPTION_KEY, $yesterday ); |
| 96 | update_option( self::RECORD_AMOUNT_OPTION_KEY, $total ); |
| 97 | |
| 98 | // We only want one sales record note at any time in the inbox, so we delete any other first. |
| 99 | Notes::delete_notes_with_name( self::NOTE_NAME ); |
| 100 | |
| 101 | $note = self::get_note_with_record_data( $record_date, $record_amt, $yesterday, $total ); |
| 102 | $note->save(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the note with record data. |
| 108 | * |
| 109 | * @param string $record_date record date Y-m-d. |
| 110 | * @param float $record_amt record amount. |
| 111 | * @param string $yesterday yesterday's date Y-m-d. |
| 112 | * @param string $total total sales for yesterday. |
| 113 | * |
| 114 | * @return Note |
| 115 | */ |
| 116 | public static function get_note_with_record_data( $record_date, $record_amt, $yesterday, $total ) { |
| 117 | // Use F jS (March 7th) format for English speaking countries. |
| 118 | if ( substr( get_user_locale(), 0, 2 ) === 'en' ) { |
| 119 | $date_format = 'F jS'; |
| 120 | } else { |
| 121 | // otherwise, fallback to the system date format. |
| 122 | $date_format = get_option( 'date_format' ); |
| 123 | } |
| 124 | |
| 125 | $formatted_yesterday = date_i18n( $date_format, strtotime( $yesterday ) ); |
| 126 | $formatted_total = html_entity_decode( wp_strip_all_tags( wc_price( $total ) ) ); |
| 127 | $formatted_record_date = date_i18n( $date_format, strtotime( $record_date ) ); |
| 128 | $formatted_record_amt = html_entity_decode( wp_strip_all_tags( wc_price( $record_amt ) ) ); |
| 129 | |
| 130 | $content = sprintf( |
| 131 | /* translators: 1 and 4: Date (e.g. October 16th), 2 and 3: Amount (e.g. $160.00) */ |
| 132 | __( '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' ), |
| 133 | $formatted_yesterday, |
| 134 | $formatted_total, |
| 135 | $formatted_record_amt, |
| 136 | $formatted_record_date |
| 137 | ); |
| 138 | |
| 139 | $content_data = (object) array( |
| 140 | 'old_record_date' => $record_date, |
| 141 | 'old_record_amt' => $record_amt, |
| 142 | 'new_record_date' => $yesterday, |
| 143 | 'new_record_amt' => $total, |
| 144 | ); |
| 145 | |
| 146 | $report_url = '?page=wc-admin&path=/analytics/revenue&period=custom&compare=previous_year&after=' . $yesterday . '&before=' . $yesterday; |
| 147 | |
| 148 | // And now, create our new note. |
| 149 | $note = new Note(); |
| 150 | $note->set_title( __( 'New sales record!', 'woocommerce' ) ); |
| 151 | $note->set_content( $content ); |
| 152 | $note->set_content_data( $content_data ); |
| 153 | $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); |
| 154 | $note->set_name( self::NOTE_NAME ); |
| 155 | $note->set_source( 'woocommerce-admin' ); |
| 156 | $note->add_action( 'view-report', __( 'View report', 'woocommerce' ), $report_url ); |
| 157 | |
| 158 | return $note; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the note. This is used for localizing the note. |
| 163 | * |
| 164 | * @return Note |
| 165 | */ |
| 166 | public static function get_note() { |
| 167 | $note = Notes::get_note_by_name( self::NOTE_NAME ); |
| 168 | if ( ! $note ) { |
| 169 | return false; |
| 170 | } |
| 171 | $content_data = $note->get_content_data(); |
| 172 | return self::get_note_with_record_data( |
| 173 | $content_data->old_record_date, |
| 174 | $content_data->old_record_amt, |
| 175 | $content_data->new_record_date, |
| 176 | $content_data->new_record_amt |
| 177 | ); |
| 178 | } |
| 179 | } |
| 180 |