', esc_html( $tool ), array(
'response' => 200,
) );
}
/** Short descriptions of what Pro adds, shown instead of settings. */
private function pro_ui( $part, $s ) {
$text = array(
'sorting_note' => 'Sorting by product name is included. Pro adds sorting rules by category, SKU and shelf location.',
'products' => 'Pro lets you leave out whole product categories and show a shelf location column read from a product field.',
'employees' => 'Pro splits the picking between several employees: balance the products, divide by category, or give each employee whole orders. Every employee gets their own sheet, and a live preview shows the result.',
'layout' => 'Pro can print two product columns per page to save paper.',
'invoice' => 'Pro lets you choose the invoice fields and product table columns, add VAT, SSN and tracking numbers, a barcode and your own texts and footer.',
'handover' => 'Pro prints a carrier handover sheet for the courier, groups shipping names into companies and filters orders by carrier.',
'barcode' => 'Pro adds Barcode Picking and Orders Review: scan products to confirm each pick, with sound and vibration feedback.',
);
if ( !isset( $text[$part] ) ) {
return;
}
echo '
After you buy, download the Pro version from your account and install it on this site. Then open this tab and paste your license key here.' . (( $account ? ' Open account' : '' )) . '
';
}
}
$sections = array(
array('Sorting and product list', 'Sorting rules on the product name; leave out virtual and downloadable products.', array('Sorting rules by product category (a parent category also matches its sub-categories), by SKU (including SKU length and digit order) and by shelf location, so the list follows your walking route.', 'Leave out whole product categories.', 'A "Shelf location" column read from a product field of your choice.')),
array('Picking report', 'Choose the columns, tick boxes, stock-shortage box, warnings and CSV export.', array('Two product columns per page to save paper.')),
array('Employees', '', array('Split one picking job between several employees: balance the products, divide by category, or give each employee whole orders.', 'Every employee gets a separate section on the report and in the CSV, with a live preview of the split.')),
array('Print invoice', 'One standard invoice per order with your logo and company name.', array('Choose the invoice fields and the product table columns.', 'Add VAT, SSN and tracking numbers from your own order fields, a barcode and a PAID stamp.', 'Your own number label, from address and footer text.')),
array('Carrier handover sheet', '', array('One printable sheet per shipping company for the courier, with customer data from billing or shipping.', 'Group shipping names into companies and filter the orders by carrier on the dashboard.')),
array('Barcode picking and order review', '', array('Scan each product to confirm the pick, with sound and vibration feedback. Progress is saved in the browser.', 'Match scans with the SKU, a GTIN / UPC / EAN field or your own product field.', 'Review an order before it is packed.'))
);
$h .= '
What is free and what Pro adds, section by section
';
foreach ( $sections as $section ) {
$h .= '
' . esc_html( $section[0] ) . '
';
if ( '' !== $section[1] ) {
$h .= '
Free ' . esc_html( $section[1] ) . '
';
}
$h .= '
Pro
';
foreach ( $section[2] as $point ) {
$h .= '
' . esc_html( $point ) . '
';
}
$h .= '
';
}
$h .= '
Sweepick only reads your orders. It never changes statuses, stock or any order data, so there is nothing to clean up if you cancel. Without a license, the free features keep working.
';
return $h;
}
/** Licence status box with the buttons to upgrade, start the trial or enter a licence key. */
private function license_card() {
if ( !function_exists( 'pplbpfw_fs' ) || !current_user_can( 'manage_woocommerce' ) ) {
return '';
}
$fs = pplbpfw_fs();
$has = function ( $m ) use($fs) {
return method_exists( $fs, $m );
};
$account = ( $has( 'get_account_url' ) ? $fs->get_account_url() : '' );
$registered = ( $has( 'is_registered' ) ? $fs->is_registered( true ) : false );
$anonymous = $has( 'is_anonymous' ) && $fs->is_anonymous();
$btn = function ( $url, $label, $primary = false ) {
return ( $url ? '' . esc_html( $label ) . ' ' : '' );
};
if ( $this->is_pro() ) {
$trial = $has( 'is_trial' ) && $fs->is_trial();
$html = '' . (( $trial ? 'Pro trial is active.' : 'Sweepick Pro is active.' )) . ' ';
$html .= $btn( $account, ( $trial ? 'Choose a plan' : 'Manage license' ) );
if ( $trial ) {
$html = str_replace( 'Choose a plan', 'Account', $html ) . $btn( $this->upgrade_url(), 'Upgrade to Pro', true );
}
return '
';
}
/** Stop with an upgrade message when a Pro-only tool is opened from the free edition. */
/** One-time migration: move settings to the store-wide option and remove data older versions wrote on orders. */
public function maybe_upgrade() {
if ( !current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( self::VERSION === get_option( self::DB_VERSION_OPTION ) ) {
return;
}
if ( !is_array( get_option( self::OPTION, null ) ) ) {
update_option( self::OPTION, $this->get_settings(), false );
}
global $wpdb;
$keys = array('_pickflow_status', '_pickflow_in_report');
foreach ( $keys as $key ) {
$wpdb->delete( $wpdb->postmeta, array(
'meta_key' => $key,
) );
}
$hpos_meta = $wpdb->prefix . 'wc_orders_meta';
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $hpos_meta ) ) ) === $hpos_meta ) {
foreach ( $keys as $key ) {
$wpdb->delete( $hpos_meta, array(
'meta_key' => $key,
) );
}
}
update_option( self::DB_VERSION_OPTION, self::VERSION, false );
}
/* ------------------------------------------------------------------
* Admin menu, assets, bulk actions
* ---------------------------------------------------------------- */
private function can_use() {
return current_user_can( 'edit_shop_orders' ) || current_user_can( 'manage_woocommerce' );
}
public function admin_menu() {
add_menu_page(
'Sweepick',
'Sweepick',
'edit_shop_orders',
self::MENU_SLUG,
array($this, 'dashboard_page'),
'dashicons-clipboard',
56
);
add_submenu_page(
self::MENU_SLUG,
'Dashboard',
'Dashboard',
'edit_shop_orders',
self::MENU_SLUG,
array($this, 'dashboard_page')
);
add_submenu_page(
self::MENU_SLUG,
'Settings',
'Settings',
'manage_woocommerce',
'pickflow-settings',
array($this, 'settings_page')
);
}
public function admin_assets( $hook ) {
if ( false === strpos( (string) $hook, self::MENU_SLUG ) ) {
return;
}
wp_enqueue_style(
'pickflow-admin',
plugin_dir_url( __FILE__ ) . 'assets/admin.css',
array(),
self::VERSION
);
wp_enqueue_script(
'pickflow-preview',
plugin_dir_url( __FILE__ ) . 'assets/preview.js',
array(),
self::VERSION,
true
);
$key_js = $this->pro_key_js();
if ( '' !== $key_js ) {
wp_add_inline_script( 'pickflow-preview', $key_js );
}
wp_enqueue_script(
'pickflow-settings',
plugin_dir_url( __FILE__ ) . 'assets/settings.js',
array(),
self::VERSION,
true
);
wp_localize_script( 'pickflow-settings', 'PickFlowSettings', array(
'sortFields' => $this->sort_field_options(),
) );
$this->pro_enqueue_assets();
}
/** Fields a sorting rule can use, as value/label pairs for the settings script. */
private function sort_field_options() {
$out = array(array(
'value' => 'name',
'label' => 'Product Name',
));
foreach ( $this->pro_sort_fields() as $value => $label ) {
$out[] = array(
'value' => $value,
'label' => $label,
);
}
return $out;
}
/** Open Sweepick bulk actions in a new tab. */
public function orders_new_tab_script() {
$screen = ( function_exists( 'get_current_screen' ) ? get_current_screen() : null );
if ( !$screen || !in_array( $screen->id, array('edit-shop_order', 'woocommerce_page_wc-orders'), true ) ) {
return;
}
wp_enqueue_script(
'pickflow-orders',
plugin_dir_url( __FILE__ ) . 'assets/orders.js',
array(),
self::VERSION,
true
);
wp_localize_script( 'pickflow-orders', 'PickFlowOrders', array(
'actions' => array(
self::ACTION,
self::BARCODE_ACTION,
self::REVIEW_ACTION,
self::SHEETS_ACTION,
self::MANIFEST_ACTION
),
) );
}
public function add_bulk_action( $actions ) {
$actions[self::ACTION] = 'Sweepick: print picking report';
$actions[self::REVIEW_ACTION] = 'Sweepick: review selected orders' . $this->pro_badge();
$actions[self::SHEETS_ACTION] = 'Sweepick: print invoices';
$actions[self::MANIFEST_ACTION] = 'Sweepick: print carrier handover sheet' . $this->pro_badge();
$settings = $this->get_settings();
if ( $this->pro_barcode_action( $settings ) ) {
$actions[self::BARCODE_ACTION] = 'Sweepick: barcode picking' . $this->pro_badge();
}
return $actions;
}
public function handle_bulk_action( $redirect, $action, $order_ids ) {
if ( !in_array( $action, array(
self::ACTION,
self::BARCODE_ACTION,
self::REVIEW_ACTION,
self::SHEETS_ACTION,
self::MANIFEST_ACTION
), true ) ) {
return $redirect;
}
$ids = array_values( array_filter( array_map( 'absint', (array) $order_ids ) ) );
if ( empty( $ids ) ) {
return $redirect;
}
return $this->view_url( $action, $ids );
}
/* ------------------------------------------------------------------
* Dashboard
* ---------------------------------------------------------------- */
private function order_status_labels() {
return ( function_exists( 'wc_get_order_statuses' ) ? wc_get_order_statuses() : array() );
}
/** Orders matching the selected statuses and optional creation-date range (oldest first). */
private function get_matching_order_ids(
$statuses,
$from_ts = 0,
$to_ts = 0,
$limit = self::DASHBOARD_ORDER_LIMIT
) {
$available = array_keys( $this->order_status_labels() );
$statuses = array_values( array_intersect( $available, (array) $statuses ) );
if ( empty( $statuses ) ) {
return array();
}
$query_statuses = array_map( function ( $status ) {
return ( 0 === strpos( $status, 'wc-' ) ? substr( $status, 3 ) : $status );
}, $statuses );
$args = array(
'status' => $query_statuses,
'limit' => $limit,
'orderby' => 'date',
'order' => 'ASC',
'return' => 'ids',
);
if ( $from_ts && $to_ts ) {
$args['date_created'] = $from_ts . '...' . $to_ts;
} elseif ( $from_ts ) {
$args['date_created'] = '>=' . $from_ts;
} elseif ( $to_ts ) {
$args['date_created'] = '<=' . $to_ts;
}
return array_map( 'absint', wc_get_orders( $args ) );
}
/** Parse a YYYY-MM-DD POST field into a timestamp in the store's timezone (start or end of that day). */
private function parse_date_field( $value, $end_of_day ) {
if ( !preg_match( '/^(\\d{4})-(\\d{2})-(\\d{2})$/', $value, $m ) || !checkdate( (int) $m[2], (int) $m[3], (int) $m[1] ) ) {
return array('', 0);
}
$date = date_create_immutable( $value . (( $end_of_day ? ' 23:59:59' : ' 00:00:00' )), wp_timezone() );
return array($value, ( $date ? $date->getTimestamp() : 0 ));
}
public function dashboard_page() {
if ( !$this->can_use() ) {
return;
}
$labels = $this->order_status_labels();
?>
Sweepick Dashboard
Choose orders and open the tool you need
v
license_card(), $this->allowed_html() );
?>
Pick, print, close the tab. Sweepick only reads your orders: it never changes order statuses, stock or any order data. Run it again whenever you need a fresh report.
Sweepick processes up to ' . esc_html( $limit ) . ' orders per batch. Your selection has more than that.
' . '
You can continue with the oldest ' . esc_html( $limit ) . ' orders and then run Sweepick again for the rest, or close this tab and narrow the selection with a date range.
' . '';
wp_die( wp_kses( $html, $this->allowed_html() ), 'Sweepick', array(
'response' => 200,
) );
}
/* ------------------------------------------------------------------
* Order data helpers
* ---------------------------------------------------------------- */
/** Link to one of the read-only report pages, protected by a nonce. */
private function view_url( $action, array $ids ) {
return add_query_arg( array(
'action' => $action,
'order_ids' => implode( ',', $ids ),
'pickflow_nonce' => wp_create_nonce( 'pickflow_view' ),
), admin_url( 'admin-post.php' ) );
}
private function parse_order_ids() {
if ( !isset( $_GET['pickflow_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['pickflow_nonce'] ) ), 'pickflow_view' ) ) {
wp_die( 'This link has expired. Go back and start again.', 'Sweepick', array(
'response' => 403,
) );
}
$raw = ( isset( $_GET['order_ids'] ) ? sanitize_text_field( wp_unslash( $_GET['order_ids'] ) ) : '' );
$ids = array_values( array_unique( array_filter( array_map( 'absint', explode( ',', $raw ) ) ) ) );
return array_slice( $ids, 0, self::URL_ORDER_LIMIT );
}
/** Ordered quantity minus any quantity already refunded. */
private function net_item_qty( $order, $item_id, $item ) {
return (float) $item->get_quantity() - abs( (float) $order->get_qty_refunded_for_item( $item_id ) );
}
private function order_units( $order ) {
$units = 0;
foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) {
$qty = $this->net_item_qty( $order, $item_id, $item );
if ( $qty > 0 ) {
$units += $qty;
}
}
return $units;
}
/** Category data for a product (variations use their parent product's categories). */
private function category_info( $product ) {
$parent_id = $product->get_parent_id();
$pid = ( $parent_id ? $parent_id : $product->get_id() );
if ( isset( $this->cat_cache[$pid] ) ) {
return $this->cat_cache[$pid];
}
$source = $product;
if ( $parent_id ) {
$parent = wc_get_product( $parent_id );
if ( $parent ) {
$source = $parent;
}
}
$ids = array_map( 'absint', (array) $source->get_category_ids() );
$names = array();
foreach ( $ids as $id ) {
$term = get_term( $id, 'product_cat' );
if ( $term && !is_wp_error( $term ) ) {
$names[] = $term->name;
}
}
$this->cat_cache[$pid] = array(
'ids' => $ids,
'display' => implode( ', ', $names ),
);
return $this->cat_cache[$pid];
}
/** Which products are left out of reports (virtual/downloadable products and chosen categories). */
private function exclusion() {
$s = $this->get_settings();
return array_merge( array(
'virtual' => !empty( $s['exclude_virtual'] ),
), $this->pro_exclusion( $s ) );
}
private function is_excluded( $product, array $ex ) {
if ( $ex['virtual'] && ($product->is_virtual() || $product->is_downloadable()) ) {
return true;
}
return $this->pro_is_excluded( $product, $ex );
}
/** Order line name, with the variation attributes added when WooCommerce left them out of the stored name. */
private function line_name( $item, $product ) {
$name = (string) $item->get_name();
if ( $product && $product->get_parent_id() && function_exists( 'wc_get_formatted_variation' ) ) {
$attrs = trim( wp_strip_all_tags( wc_get_formatted_variation( $product, true, false ) ) );
if ( '' !== $attrs ) {
$missing = false;
foreach ( array_map( 'trim', explode( ',', $attrs ) ) as $part ) {
$value = trim( (string) substr( $part, (int) strpos( $part, ':' ) + 1 ) );
if ( '' !== $value && false === stripos( $name, $value ) ) {
$missing = true;
break;
}
}
if ( $missing ) {
$name .= ' (' . $attrs . ')';
}
}
}
return $name;
}
private function skipped_for( $number ) {
return array_filter( $this->skipped, function ( $line ) use($number) {
return (string) $line['number'] === (string) $number;
} );
}
/** Box listing selected orders that have nothing to pick, with the reason (so no selected order disappears silently). */
private function empty_orders_html( $class ) {
if ( empty( $this->empty_orders ) ) {
return '';
}
$bits = array();
foreach ( array_slice( $this->empty_orders, 0, 30 ) as $o ) {
$bits[] = '#' . esc_html( $o['number'] ) . (( '' !== $o['status'] ? ' (' . esc_html( $o['status'] ) . ')' : '' )) . ': ' . esc_html( $o['reason'] ) . '';
}
return '
sorting rule(s) were not saved because they were invalid (for example, a SKU Length rule whose Digit Order is not a full set of positions from 1 to the length).
license_card(), $this->allowed_html() );
?>
Store-wide settings. These settings are shared by everyone who prints Sweepick reports on this store.
Sweepick is an independent WooCommerce tool. Return to the Sweepick Dashboard to select orders and launch reports or review.