set_basename( false, __FILE__ ); return; } if ( !function_exists( 'pplbpfw_fs' ) ) { // Helper function for easy SDK access. function pplbpfw_fs() { global $pplbpfw_fs; if ( !isset( $pplbpfw_fs ) ) { require_once dirname( __FILE__ ) . '/vendor/freemius/start.php'; $pplbpfw_fs = fs_dynamic_init( array( 'id' => '44223', 'slug' => 'sweepick', 'premium_slug' => 'sweepickpro', 'type' => 'plugin', 'public_key' => 'pk_d39de0330605baf027655e7fd521d', 'is_premium' => false, 'premium_suffix' => 'pro', 'has_addons' => false, 'has_paid_plans' => true, 'is_org_compliant' => true, 'trial' => array( 'days' => 3, 'is_require_payment' => false, ), 'menu' => array( 'slug' => 'pickflow', 'first-path' => 'admin.php?page=pickflow', 'support' => false, ), 'is_live' => true, ) ); } return $pplbpfw_fs; } pplbpfw_fs(); do_action( 'pplbpfw_fs_loaded' ); /** Removes Sweepick settings when the plugin is deleted. Sweepick never stores data on orders, so there is nothing else to clean up. */ function pplbpfw_fs_uninstall_cleanup() { delete_option( 'pickflow_settings' ); delete_option( 'pickflow_db_version' ); delete_metadata( 'user', 0, '_pickflow_settings', '', true ); } pplbpfw_fs()->add_action( 'after_uninstall', 'pplbpfw_fs_uninstall_cleanup' ); } // Declare High-Performance Order Storage (HPOS) compatibility. Sweepick never stores data on orders. add_action( 'before_woocommerce_init', function () { if ( class_exists( '\\Automattic\\WooCommerce\\Utilities\\FeaturesUtil' ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); } } ); // Pro-only code lives in includes/pro/, which Freemius leaves out of the free edition. The free edition gets harmless stubs. if ( file_exists( __DIR__ . '/includes/pro/features.php' ) ) { require_once __DIR__ . '/includes/pro/features.php'; } else { /** * Free edition: the Pro features are not part of this code. These methods only describe them. * Nothing here checks a licence or limits a feature. */ trait PickFlow_Pro_Features { private function pro_build() { return false; } private function pro_defaults() { return array(); } private function pro_group_heading() { return 'Group'; } private function pro_sections_summary( $count ) { return ''; } private function pro_rule_match( $row, $rule ) { return false; } private function pro_sanitize_rule( $rule, $field, $condition, $value ) { return false; } private function pro_rule_conditions( $rule ) { } private function pro_rule_inputs( $i, $rule ) { } private function pro_is_excluded( $product, array $ex ) { return false; } private function pro_location( $product, array $ex ) { return ''; } private function pro_extra_columns() { return array(); } private function pro_column_bit( $extra, $row ) { return ''; } private function pro_column_value( $c, $row ) { return ''; } private function pro_enqueue_assets() { } private function pro_normalize_keys( $s ) { return $s; } private function pro_exclusion( $s ) { return array(); } private function pro_two_columns( $s ) { return false; } private function pro_barcode_action( $s ) { return true; } private function render_order_sheets_pro() { } private function pro_badge() { return ' (Pro)'; } private function pro_sort_fields() { return array(); } private function pro_columns( array $columns ) { return $columns; } private function pro_filter_settings( $s ) { return $s; } private function pro_collect_settings() { return array(); } private function pro_build_groups( array $ids, array $s, array $rules ) { return null; } private function carrier_raw_names() { return array(); } private function carrier_options() { return array(); } private function filter_by_carrier( array $ids, array $carriers ) { return $ids; } public function render_barcode() { $this->pro_notice( 'Barcode picking' ); } public function render_review() { $this->pro_notice( 'Order review' ); } public function render_manifest() { $this->pro_notice( 'Carrier handover sheet' ); } private function pro_notice( $tool ) { wp_die( '

' . esc_html( $tool ) . ' is part of Sweepick Pro

See what Sweepick Pro adds

', 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 '
Sweepick Pro. ' . esc_html( $text[$part] ) . ' See Pro features
'; } } } final class PickFlow_MVP { use PickFlow_Pro_Features; const VERSION = '1.1.8'; const ACTION = 'pickflow_generate_sheet'; const BARCODE_ACTION = 'pickflow_start_barcode'; const REVIEW_ACTION = 'pickflow_review'; const CSV_ACTION = 'pickflow_export_csv'; const SHEETS_ACTION = 'pickflow_order_sheets'; const MANIFEST_ACTION = 'pickflow_manifest'; const CARRIER_SCAN_LIMIT = 2000; const DEFAULT_CARRIER_NAME = 'All shipping methods (one company)'; const MENU_SLUG = 'pickflow'; const OPTION = 'pickflow_settings'; const DB_VERSION_OPTION = 'pickflow_db_version'; const DASHBOARD_ORDER_LIMIT = 300; const URL_ORDER_LIMIT = 500; /** @var array Category lookup cache, keyed by product (or parent product) ID. */ private $cat_cache = array(); /** @var array|null Raw shipping names (cached per request). */ private $carrier_raw_cache = null; /** @var array Order lines that could not be included because their product no longer exists. */ private $skipped = array(); /** @var array Selected orders that contributed no product line, with the reason. */ private $empty_orders = array(); public function __construct() { add_action( 'admin_menu', array($this, 'admin_menu') ); add_action( 'admin_init', array($this, 'maybe_upgrade') ); add_action( 'admin_enqueue_scripts', array($this, 'admin_assets') ); add_action( 'admin_enqueue_scripts', array($this, 'orders_new_tab_script') ); add_action( 'admin_post_pickflow_save_settings', array($this, 'save_settings') ); add_action( 'admin_post_' . self::ACTION, array($this, 'render_sheet') ); add_action( 'admin_post_' . self::BARCODE_ACTION, array($this, 'render_barcode') ); add_action( 'admin_post_pickflow_dashboard_action', array($this, 'dashboard_action') ); add_action( 'admin_post_' . self::REVIEW_ACTION, array($this, 'render_review') ); add_action( 'admin_post_' . self::CSV_ACTION, array($this, 'export_csv') ); add_action( 'admin_post_' . self::SHEETS_ACTION, array($this, 'render_order_sheets') ); add_action( 'admin_post_' . self::MANIFEST_ACTION, array($this, 'render_manifest') ); // Bulk actions: classic Orders screen and HPOS Orders screen. add_filter( 'bulk_actions-edit-shop_order', array($this, 'add_bulk_action') ); add_filter( 'handle_bulk_actions-edit-shop_order', array($this, 'handle_bulk_action'), 10, 3 ); add_filter( 'bulk_actions-woocommerce_page_wc-orders', array($this, 'add_bulk_action') ); add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_action'), 10, 3 ); } /* ------------------------------------------------------------------ * Settings (store-wide) * ---------------------------------------------------------------- */ private function defaults() { return array_merge( array( 'sort_rules' => array(), 'columns' => array( 'image', 'sku', 'product', 'quantity' ), 'show_tick' => 1, 'show_shortages' => 1, 'exclude_virtual' => 1, 'title' => 'Sweepick Picking Sheet', 'sheet_logo' => '', 'sheet_company' => '', ), $this->pro_defaults() ); } /** Settings written by versions before 0.6.0 were stored per user. */ private function legacy_user_settings() { $uid = get_current_user_id(); $meta = ( $uid ? get_user_meta( $uid, '_pickflow_settings', true ) : '' ); if ( is_array( $meta ) && $meta ) { return $meta; } $users = get_users( array( 'meta_key' => '_pickflow_settings', 'number' => 1, 'fields' => 'ID', ) ); if ( $users ) { $meta = get_user_meta( (int) $users[0], '_pickflow_settings', true ); if ( is_array( $meta ) ) { return $meta; } } return array(); } private function normalize_legacy_keys( $s ) { $s = $this->pro_normalize_keys( $s ); unset($s['split_mode'], $s['mixed_category_orders'], $s['direction']); return $s; } private function get_settings() { $saved = get_option( self::OPTION, null ); if ( !is_array( $saved ) ) { $saved = $this->legacy_user_settings(); } $saved = $this->normalize_legacy_keys( $saved ); $s = wp_parse_args( $saved, $this->defaults() ); return $this->pro_filter_settings( $s ); } /** Tags and attributes Sweepick prints itself; everything it builds goes through wp_kses with this list. */ private function allowed_html() { $common = array( 'class' => true, 'id' => true, 'dir' => true, 'title' => true, 'role' => true, 'aria-label' => true, 'aria-hidden' => true, ); $tags = array( 'div', 'span', 'p', 'strong', 'b', 'em', 'h1', 'h2', 'h3', 'ul', 'li', 'table', 'thead', 'tbody', 'tr', 'bdi', 'fieldset', 'section', 'h4' ); $allowed = array( 'br' => array(), ); foreach ( $tags as $tag ) { $allowed[$tag] = $common; } $allowed['section'] = $common + array( 'data-pf-panel' => true, ); $allowed['th'] = $common + array( 'colspan' => true, ); $allowed['td'] = $common + array( 'colspan' => true, ); $allowed['fieldset'] = $common + array( 'disabled' => true, ); $allowed['a'] = $common + array( 'href' => true, 'target' => true, 'rel' => true, ); $allowed['form'] = $common + array( 'method' => true, 'action' => true, ); $allowed['input'] = $common + array( 'type' => true, 'name' => true, 'value' => true, 'placeholder' => true, 'disabled' => true, 'checked' => true, 'autocomplete' => true, 'spellcheck' => true, ); $allowed['button'] = $common + array( 'type' => true, 'disabled' => true, ); $allowed['svg'] = $common + array( 'xmlns' => true, 'viewbox' => true, 'width' => true, 'height' => true, 'preserveaspectratio' => true, 'fill' => true, ); $allowed['rect'] = array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, ); return $allowed; } /** Loads the script behind the Print and Close buttons of the printable pages. */ private function print_script() { wp_enqueue_script( 'pickflow-print', plugin_dir_url( __FILE__ ) . 'assets/print.js', array(), self::VERSION, false ); wp_print_scripts( 'pickflow-print' ); } /** True while a Pro licence (or Pro trial) is active. */ private function is_pro() { return function_exists( 'pplbpfw_fs' ) && pplbpfw_fs()->can_use_premium_code(); } private function upgrade_url() { $fallback = admin_url( 'admin.php?page=pickflow-settings#pro' ); try { if ( function_exists( 'pplbpfw_fs' ) && method_exists( pplbpfw_fs(), 'get_upgrade_url' ) ) { $url = pplbpfw_fs()->get_upgrade_url(); return ( $url ? $url : $fallback ); } } catch ( \Throwable $e ) { // Never let a licensing hiccup break the page. return $fallback; } return $fallback; } /** JavaScript for the licence-key box on the Pro tab (premium build only); empty when the SDK cannot activate keys. */ private function pro_key_js() { $fs = ( function_exists( 'pplbpfw_fs' ) ? pplbpfw_fs() : null ); if ( !$fs || $this->is_pro() || !method_exists( $fs, 'is_premium' ) || !$fs->is_premium() || !method_exists( $fs, 'get_ajax_action' ) || !method_exists( $fs, 'get_ajax_security' ) || !method_exists( $fs, 'get_id' ) ) { return ''; } return '(function(){var b=document.getElementById("pf-activate-key"),i=document.getElementById("pf-license-key"),m=document.getElementById("pf-key-msg");if(!b)return;i.addEventListener("keydown",function(e){if(e.key==="Enter"){e.preventDefault();b.click();}});' . 'b.addEventListener("click",function(){var k=i.value.trim();if(!k){m.textContent="Enter your license key.";return;}b.disabled=true;m.textContent="Activating…";' . 'var d=new URLSearchParams();d.append("action",' . wp_json_encode( $fs->get_ajax_action( 'activate_license' ) ) . ');d.append("security",' . wp_json_encode( $fs->get_ajax_security( 'activate_license' ) ) . ');d.append("module_id",' . wp_json_encode( (string) $fs->get_id() ) . ');d.append("license_key",k);' . 'fetch(' . wp_json_encode( admin_url( 'admin-ajax.php' ) ) . ',{method:"POST",credentials:"same-origin",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:d.toString()}).then(function(r){return r.text();}).then(function(t){var o;try{o=JSON.parse(t);}catch(e){o=null;}' . 'if(o&&o.success){m.textContent="Activated. Reloading…";window.location.href=' . wp_json_encode( admin_url( 'admin.php?page=pickflow-settings' ) ) . '+"#pro";window.location.reload();}' . 'else{b.disabled=false;var e=o&&o.error;m.textContent=(e&&e.message)?e.message:(typeof e==="string"&&e?e:"Could not activate this key.");}}).catch(function(){b.disabled=false;m.textContent="Network error. Try again.";});});})();'; } /** The "Pro" settings tab: buy, enter the licence key, what Pro adds and a Free vs Pro table. */ private function pro_panel() { $fs = ( function_exists( 'pplbpfw_fs' ) ? pplbpfw_fs() : null ); $pro = $this->is_pro(); $account = ( $fs && method_exists( $fs, 'get_account_url' ) ? $fs->get_account_url() : '' ); $h = '

Sweepick Pro

'; if ( $pro ) { $h .= '
Sweepick Pro is active. All Pro features are unlocked.' . (( $account ? ' Manage license' : '' )) . '
'; } else { $h .= '
Unlock everything with Pro.
Barcode picking, employees, carrier handover and a fully customizable invoice. Monthly plan, cancel any time.
Buy Pro
'; if ( $fs && method_exists( $fs, 'get_trial_url' ) && method_exists( $fs, 'is_trial_utilized' ) && !$fs->is_trial_utilized() ) { $h .= '

Or start a free trial, no card needed.

'; } $h .= '

Already have a license key?

'; if ( '' !== $this->pro_key_js() ) { $h .= '
'; } else { $h .= '

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 '
' . $html . '
'; } $html = 'Free edition. '; $html .= $btn( $this->upgrade_url(), 'Upgrade to Pro', true ); if ( $has( 'get_trial_url' ) && $has( 'is_trial_utilized' ) && !$fs->is_trial_utilized() ) { $html .= $btn( $fs->get_trial_url(), 'Start free trial' ); } $html .= $btn( admin_url( 'admin.php?page=pickflow-settings#pro' ), 'I have a license key' ); return '
' . $html . '
'; } /** 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.

1. Select order statuses

Select one or more statuses. The number beside each status is how many orders it currently has.

$label ) { $slug = ( 0 === strpos( $key, 'wc-' ) ? substr( $key, 3 ) : $key ); $count = ( function_exists( 'wc_orders_count' ) ? (int) wc_orders_count( $slug ) : null ); ?>

2. Date range (optional)

Filter by the date each order was created. Leave both fields empty to include every date.

pro_build() ) { ?>

3. Shipping company (optional)

Tick one or more to include only their orders. Leave all unticked to include every order.

carrier_options() as $carrier ) { ?>carrier_options() ) { ?>No shipping methods found yet.

pro_build() ? '4' : '3' ); ?>. Choose an action

Up to orders are processed at a time. If more match, Sweepick will tell you before continuing.

Settings

can_use() ) { wp_die( 'Permission denied.' ); } check_admin_referer( 'pickflow_dashboard_action' ); $statuses = ( isset( $_POST['statuses'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['statuses'] ) ) : array() ); list( $from, $from_ts ) = $this->parse_date_field( ( isset( $_POST['date_from'] ) ? sanitize_text_field( wp_unslash( $_POST['date_from'] ) ) : '' ), false ); list( $to, $to_ts ) = $this->parse_date_field( ( isset( $_POST['date_to'] ) ? sanitize_text_field( wp_unslash( $_POST['date_to'] ) ) : '' ), true ); if ( $from_ts && $to_ts && $from_ts > $to_ts ) { wp_die( 'The "From" date is after the "To" date. Go back and correct the date range.' ); } $task = ( isset( $_POST['task'] ) ? sanitize_key( wp_unslash( $_POST['task'] ) ) : 'print' ); $carriers = ( isset( $_POST['carriers'] ) ? array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['carriers'] ) ) : array() ); if ( $carriers && $this->pro_build() ) { $ids = $this->filter_by_carrier( $this->get_matching_order_ids( $statuses, $from_ts, $to_ts, self::CARRIER_SCAN_LIMIT ), $carriers ); } else { $ids = $this->get_matching_order_ids( $statuses, $from_ts, $to_ts, self::DASHBOARD_ORDER_LIMIT + 1 ); } if ( empty( $ids ) ) { wp_die( 'No orders matched the selected statuses and dates. Close this tab and adjust your selection.' ); } if ( count( $ids ) > self::DASHBOARD_ORDER_LIMIT ) { if ( empty( $_POST['confirm_limit'] ) ) { $this->render_limit_notice( $statuses, $from, $to, $task, $carriers ); } $ids = array_slice( $ids, 0, self::DASHBOARD_ORDER_LIMIT ); } $action = ( 'review' === $task ? self::REVIEW_ACTION : (( 'barcode' === $task ? self::BARCODE_ACTION : (( 'sheets' === $task ? self::SHEETS_ACTION : (( 'manifest' === $task ? self::MANIFEST_ACTION : self::ACTION )) )) )) ); wp_safe_redirect( $this->view_url( $action, $ids ) ); exit; } /** Shown when more orders match than one batch can hold, instead of silently dropping the newest orders. */ private function render_limit_notice( $statuses, $from, $to, $task, $carriers = array() ) { $limit = self::DASHBOARD_ORDER_LIMIT; $fields = '' . wp_nonce_field( 'pickflow_dashboard_action', '_wpnonce', true, false ); foreach ( $statuses as $status ) { $fields .= ''; } foreach ( $carriers as $carrier ) { $fields .= ''; } $fields .= ''; $html = '

More than ' . esc_html( $limit ) . ' orders match

' . '

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.

' . '
' . $fields . '

'; 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 '
' . esc_html( count( $this->empty_orders ) ) . (( 1 === count( $this->empty_orders ) ? ' selected order has' : ' selected orders have' )) . ' nothing to pick' . implode( '
', $bits ) . '
'; } /** Warning box listing order lines left out because their product was deleted. Empty string when nothing was skipped. */ private function skipped_html( $class ) { if ( empty( $this->skipped ) ) { return ''; } $bits = array(); foreach ( array_slice( $this->skipped, 0, 30 ) as $line ) { $bits[] = '#' . esc_html( $line['number'] ) . ' · ' . esc_html( $line['name'] ) . ' ×' . esc_html( wc_format_localized_decimal( $line['qty'] ) ) . ''; } $more = count( $this->skipped ) - count( $bits ); return '
' . esc_html( count( $this->skipped ) ) . (( 1 === count( $this->skipped ) ? ' order line is' : ' order lines are' )) . ' NOT in this list because the product no longer exists in the store. Check these orders by hand:
' . implode( '
', $bits ) . (( $more > 0 ? '
+ ' . esc_html( $more ) . ' more' : '' )) . '
'; } /** Aggregate order lines into one row per product (variation). */ private function get_order_rows( $ids ) { $rows = array(); $sequence = 0; $ex = $this->exclusion(); $this->skipped = array(); $this->empty_orders = array(); foreach ( $ids as $order_id ) { $order = wc_get_order( $order_id ); if ( !$order ) { $this->empty_orders[] = array( 'number' => (string) $order_id, 'status' => '', 'reason' => 'order not found', ); continue; } $sequence++; $used = 0; $excluded = 0; $refunded = 0; $lines = 0; foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) { if ( !is_a( $item, 'WC_Order_Item_Product' ) ) { continue; } $product = $item->get_product(); if ( !$product ) { $lost = $this->net_item_qty( $order, $item_id, $item ); if ( $lost > 0 ) { $this->skipped[] = array( 'number' => $order->get_order_number(), 'name' => $item->get_name(), 'qty' => $lost, ); } continue; } $lines++; if ( $this->is_excluded( $product, $ex ) ) { $excluded++; continue; } $qty = $this->net_item_qty( $order, $item_id, $item ); if ( $qty <= 0 ) { $refunded++; continue; } $used++; $key = $product->get_id(); if ( !isset( $rows[$key] ) ) { $cat = $this->category_info( $product ); $rows[$key] = array( 'product' => $product, 'name' => $this->line_name( $item, $product ), 'location' => $this->pro_location( $product, $ex ), 'sku' => (string) $product->get_sku(), 'quantity' => 0, 'orders' => array(), 'cat' => $cat, 'category' => $cat['display'], 'price' => (float) $product->get_price(), ); } $rows[$key]['quantity'] += $qty; if ( isset( $rows[$key]['orders'][$order_id] ) ) { $rows[$key]['orders'][$order_id]['qty'] += $qty; } else { $rows[$key]['orders'][$order_id] = array( 'seq' => $sequence, 'number' => $order->get_order_number(), 'qty' => $qty, ); } } if ( 0 === $used && empty( $this->skipped_for( $order->get_order_number() ) ) ) { $reason = ( 0 === $lines ? 'the order has no product lines' : (( $excluded > 0 ? 'all its products are left out (virtual / downloadable or an excluded category)' : 'all its quantities are refunded' )) ); $this->empty_orders[] = array( 'number' => $order->get_order_number(), 'status' => ( function_exists( 'wc_get_order_status_name' ) ? wc_get_order_status_name( $order->get_status() ) : $order->get_status() ), 'reason' => $reason, ); } } return $rows; } /* ------------------------------------------------------------------ * Sorting rules: the FIRST matching rule wins. * Key = [rule index, sub-key, product ID]; unmatched products use rule index = rule count. * ---------------------------------------------------------------- */ private function lower( $text ) { return ( function_exists( 'mb_strtolower' ) ? mb_strtolower( (string) $text, 'UTF-8' ) : strtolower( (string) $text ) ); } private function text_match( $condition, $candidates, $value ) { $needle = $this->lower( trim( $value ) ); if ( '' === $needle ) { return false; } foreach ( $candidates as $candidate ) { $hay = $this->lower( trim( (string) $candidate ) ); if ( '' === $hay ) { continue; } if ( 'equal' === $condition && $hay === $needle ) { return true; } if ( 'contains' === $condition && false !== strpos( $hay, $needle ) ) { return true; } } return false; } /** Returns false when the rule does not match, otherwise a sub-key string used to order rows inside the rule's group. */ private function rule_match( $row, $rule ) { $field = ( isset( $rule['field'] ) ? $rule['field'] : '' ); $condition = ( isset( $rule['condition'] ) ? $rule['condition'] : '' ); $value = trim( (string) (( isset( $rule['value'] ) ? $rule['value'] : '' )) ); if ( '' === $value ) { return false; } if ( 'name' === $field && in_array( $condition, array('equal', 'contains'), true ) ) { return ( $this->text_match( $condition, array((string) $row['name']), $value ) ? '' : false ); } return $this->pro_rule_match( $row, $rule ); } private function row_sort_key( $row, $rules ) { $id = (int) $row['product']->get_id(); foreach ( array_values( $rules ) as $index => $rule ) { $sub = $this->rule_match( $row, $rule ); if ( false !== $sub ) { return array($index, (string) $sub, $id); } } return array(count( $rules ), '', $id); } private function sort_rows( array $rows, array $rules ) { $keys = array(); foreach ( $rows as $id => $row ) { $keys[$id] = $this->row_sort_key( $row, $rules ); } uksort( $rows, function ( $a, $b ) use($keys) { if ( $keys[$a][0] !== $keys[$b][0] ) { return $keys[$a][0] <=> $keys[$b][0]; } $cmp = strcmp( $keys[$a][1], $keys[$b][1] ); if ( 0 !== $cmp ) { return $cmp; } return $keys[$a][2] <=> $keys[$b][2]; } ); return $rows; } /* ------------------------------------------------------------------ * Sections of the report (one section; Pro can split the work between employees) * Modes: balance (products), category (products), orders (whole orders). * Returns array( section_no => array( 'rows' => sorted rows, 'orders' => int|null, 'label' => string ) ). * ---------------------------------------------------------------- */ private function build_groups( array $ids, array $s ) { $rules = ( is_array( $s['sort_rules'] ) ? $s['sort_rules'] : array() ); $groups = $this->pro_build_groups( $ids, $s, $rules ); if ( null !== $groups ) { return $groups; } return array( 1 => array( 'rows' => $this->sort_rows( $this->get_order_rows( $ids ), $rules ), 'orders' => null, ), ); } /* ------------------------------------------------------------------ * Orders review screen * ---------------------------------------------------------------- */ /* ------------------------------------------------------------------ * Settings page * ---------------------------------------------------------------- */ public function settings_page() { if ( !current_user_can( 'manage_woocommerce' ) ) { return; } $s = $this->get_settings(); $columns = array( 'image' => 'Product image', 'sku' => 'SKU', 'product_id' => 'Product ID', 'product' => 'Product name / variation', 'category' => 'Category', 'price' => 'Price', 'quantity' => 'Total quantity', 'orders' => 'Number of orders', 'order_numbers' => 'Order numbers', ); $columns = $this->pro_columns( $columns ); $notice = get_transient( 'pickflow_notice_' . get_current_user_id() ); $updated = is_array( $notice ); $rejected = ( $updated ? (int) $notice['rejected'] : 0 ); if ( $updated ) { delete_transient( 'pickflow_notice_' . get_current_user_id() ); } ?>

Sweepick Settings

Customize your picking reports

v

Settings saved.

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.

Product sorting rules

Create rules and drag them to set priority. Each product follows the first rule it matches, and products are grouped in rule order. Products that match no rule, and products without a SKU, are placed last and sorted by Product ID (oldest first).

pro_ui( 'sorting_note', $s ); ?>
$rule ) { ?>
⠿ pro_rule_inputs( $i, $rule ); ?>

pro_ui( 'sorting_help', $s ); ?>

Which products are listed

These items cannot be picked from a shelf, so they are removed from the report, the CSV, barcode picking, order review and the stock-shortage box.

pro_ui( 'products', $s ); ?>

Employeespro_badge() ); ?>

Enable this only when picking work is shared between several employees. Each employee gets a separate section, starting on a new page when printed.

pro_ui( 'employees', $s ); ?>

Report layout

Choose how the printed picking report looks. The sheet on the right shows your choices with sample products.

pro_ui( 'layout', $s ); ?>

Visible columns

Image, SKU, product name and quantity are shown as the main cells of each row. Category, product ID, price and order count appear as a small line under the product name. Order numbers show as chips: the order's position in this batch, its number, and the quantity when it is more than one.

$label ) { ?>

Report title

Print invoice

A printable invoice for each order, one page per order, showing the order's own data. It is for display only: Sweepick does not number or store invoices. Choose what appears on it; the page on the right shows your choices with a sample order.

Upload the logo in Media, then paste its address here.

pro_ui( 'invoice', $s ); ?>

Carrier handover sheetpro_badge() ); ?>

One page per shipping company with the list of orders you hand over, the totals, and signature lines. Orders are grouped by their shipping method name, or by an order meta key if your carrier plugin stores the carrier there. The logo and company name come from the Print invoice tab.

pro_ui( 'handover', $s ); ?>

Barcode picking & reviewpro_badge() ); ?>

Scan products to confirm picking (Barcode Picking) or to check each order on its own (Review Orders).

pro_ui( 'barcode', $s ); ?>
pro_panel(), $this->allowed_html() ); ?>

Sweepick is an independent WooCommerce tool. Return to the Sweepick Dashboard to select orders and launch reports or review.

pro_columns( array( 'image' => '', 'sku' => '', 'product_id' => '', 'product' => '', 'category' => '', 'price' => '', 'quantity' => '', 'orders' => '', 'order_numbers' => '', ) ) ); $columns = ( isset( $_POST['columns'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['columns'] ) ) : array() ); $columns = array_values( array_intersect( $allowed, $columns ) ); if ( empty( $columns ) ) { $columns = array('sku', 'product', 'quantity'); } $sort_rules = array(); $rejected = 0; if ( isset( $_POST['sort_rules'] ) && is_array( $_POST['sort_rules'] ) ) { foreach ( array_slice( map_deep( wp_unslash( $_POST['sort_rules'] ), 'sanitize_text_field' ), 0, 100 ) as $rule ) { if ( !is_array( $rule ) ) { continue; } $value = ( isset( $rule['value'] ) ? sanitize_text_field( $rule['value'] ) : '' ); if ( '' === trim( $value ) ) { continue; } // Blank rows are ignored. $field = ( isset( $rule['field'] ) ? $rule['field'] : '' ); $condition = ( isset( $rule['condition'] ) ? $rule['condition'] : '' ); if ( 'name' === $field && in_array( $condition, array('equal', 'contains'), true ) ) { $sort_rules[] = array( 'field' => 'name', 'condition' => $condition, 'value' => $value, ); continue; } $pro_rule = $this->pro_sanitize_rule( $rule, $field, $condition, $value ); if ( false === $pro_rule ) { $rejected++; continue; } $sort_rules[] = $pro_rule; } } $free = array( 'sort_rules' => $sort_rules, 'columns' => $columns, 'show_tick' => ( isset( $_POST['show_tick'] ) ? 1 : 0 ), 'show_shortages' => ( isset( $_POST['show_shortages'] ) ? 1 : 0 ), 'exclude_virtual' => ( isset( $_POST['exclude_virtual'] ) ? 1 : 0 ), 'title' => ( isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : 'Sweepick Picking Sheet' ), 'sheet_logo' => ( isset( $_POST['sheet_logo'] ) ? esc_url_raw( wp_unslash( $_POST['sheet_logo'] ) ) : '' ), 'sheet_company' => substr( sanitize_text_field( wp_unslash( ( isset( $_POST['sheet_company'] ) ? $_POST['sheet_company'] : '' ) ) ), 0, 80 ), ); // Start from what is already saved so settings that have no control on this edition are kept. $saved = get_option( self::OPTION, array() ); $settings = array_merge( ( is_array( $saved ) ? $saved : array() ), $free, $this->pro_collect_settings() ); update_option( self::OPTION, $settings, false ); // The notice is handed over in a short-lived transient so the settings page reads no request data. set_transient( 'pickflow_notice_' . get_current_user_id(), array( 'rejected' => (int) $rejected, ), 60 ); wp_safe_redirect( add_query_arg( array( 'page' => 'pickflow-settings', ), admin_url( 'admin.php' ) ) ); exit; } /* ------------------------------------------------------------------ * Barcode picking screen * ---------------------------------------------------------------- */ /* ------------------------------------------------------------------ * Order sheets (display only, one page per order; no numbering, nothing is stored) * ---------------------------------------------------------------- */ public function render_order_sheets() { if ( !$this->can_use() ) { wp_die( 'You do not have permission to view these sheets.' ); } if ( !function_exists( 'wc_get_order' ) ) { wp_die( 'WooCommerce is required.' ); } if ( $this->pro_build() ) { $this->render_order_sheets_pro(); return; } $ids = $this->parse_order_ids(); if ( empty( $ids ) ) { wp_die( 'No orders were selected.' ); } $s = $this->get_settings(); $price = function ( $order, $v ) { return wc_price( $v, array( 'currency' => $order->get_currency(), ) ); }; $pages = 0; ?>>Order print_script(); ?>
Sweepick  Use Print / Save as PDF, then close this tab.
'; if ( '' !== $s['sheet_logo'] ) { echo ''; } echo '
'; if ( '' !== $s['sheet_company'] ) { echo '' . esc_html( $s['sheet_company'] ) . ''; } echo '

Order ' . esc_html( $order->get_order_number() ) . '

'; echo '

Bill to

' . wp_kses( $order->get_formatted_billing_address(), array( 'br' => array(), ) ); if ( $order->get_billing_email() ) { echo '
' . esc_html( $order->get_billing_email() ); } if ( $order->get_billing_phone() ) { echo '
' . esc_html( $order->get_billing_phone() ); } echo '

'; if ( $order->get_date_created() ) { echo '
Order date: ' . esc_html( wc_format_datetime( $order->get_date_created() ) ) . '
'; } if ( '' !== (string) $order->get_payment_method_title() ) { echo '
Payment method: ' . esc_html( $order->get_payment_method_title() ) . '
'; } echo '
'; echo ''; foreach ( $order->get_items( 'line_item' ) as $item ) { if ( !is_a( $item, 'WC_Order_Item_Product' ) ) { continue; } $qty = (float) $item->get_quantity(); $product = $item->get_product(); echo ''; } echo '
ProductQuantityUnit priceTotal price
' . (( $product ? wp_kses_post( $product->get_image( 'thumbnail' ) ) : '' )) . '' . esc_html( $item->get_name() ) . '' . esc_html( wc_format_localized_decimal( $qty ) ) . '' . wp_kses_post( $price( $order, ( $qty > 0 ? (float) $item->get_subtotal() / $qty : 0 ) ) ) . '' . wp_kses_post( $price( $order, (float) $item->get_subtotal() ) ) . '
'; $rows = array(array('Subtotal', $price( $order, (float) $order->get_subtotal() ), '')); if ( (float) $order->get_shipping_total() > 0 ) { $rows[] = array('Shipping', $price( $order, (float) $order->get_shipping_total() ), ''); } if ( (float) $order->get_discount_total() > 0 ) { $rows[] = array('Discount', '-' . $price( $order, (float) $order->get_discount_total() ), ''); } if ( (float) $order->get_total_tax() > 0 ) { $rows[] = array('Total tax', $price( $order, (float) $order->get_total_tax() ), ''); } $rows[] = array('Total', $price( $order, (float) $order->get_total() ), 'grand'); echo ''; foreach ( $rows as $r ) { echo ''; } echo '
' . esc_html( $r[0] ) . '' . wp_kses_post( $r[1] ) . '
'; } if ( 0 === $pages ) { echo '

No valid orders were found.

'; } ?>exclusion(); foreach ( $ids as $order_id ) { $order = wc_get_order( $order_id ); if ( !$order ) { continue; } foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) { if ( !is_a( $item, 'WC_Order_Item_Product' ) ) { continue; } $product = $item->get_product(); if ( !$product || $this->is_excluded( $product, $ex ) || !$product->managing_stock() && !$product->get_stock_managed_by_id() ) { continue; } if ( !$product->managing_stock() ) { $managed_id = (int) $product->get_stock_managed_by_id(); } else { $managed_id = (int) $product->get_id(); } if ( $managed_id <= 0 || $item->get_meta( '_reduced_stock', true ) ) { continue; } $qty = $this->net_item_qty( $order, $item_id, $item ); if ( $qty <= 0 ) { continue; } if ( !isset( $need[$managed_id] ) ) { $stock_product = ( $managed_id === (int) $product->get_id() ? $product : wc_get_product( $managed_id ) ); if ( !$stock_product ) { continue; } $need[$managed_id] = array( 'sku' => (string) $stock_product->get_sku(), 'name' => $this->line_name( $item, $product ), 'required' => 0, 'stock' => (float) $stock_product->get_stock_quantity(), 'backorder' => (bool) $stock_product->backorders_allowed(), ); } $need[$managed_id]['required'] += $qty; } } $short = array(); foreach ( $need as $id => $row ) { $row['short'] = $row['required'] - max( 0, $row['stock'] ); if ( $row['short'] > 0 ) { $short[$id] = $row; } } uasort( $short, function ( $a, $b ) { return $b['short'] <=> $a['short']; } ); return $short; } /** Columns of the report as enabled in the settings (same rules as the printed report). */ private function enabled_columns( $settings ) { $enabled = array_values( array_intersect( (array) $settings['columns'], array_keys( $this->pro_columns( array( 'image' => '', 'sku' => '', 'product_id' => '', 'product' => '', 'category' => '', 'price' => '', 'quantity' => '', 'orders' => '', 'order_numbers' => '', ) ) ) ) ); return ( $enabled ? $enabled : array('sku', 'product', 'quantity') ); } private function csv_cell( $value ) { $value = (string) $value; // Stop spreadsheets from running a cell that starts like a formula. if ( '' !== $value && false !== strpos( "=+-@\t\r", $value[0] ) && !is_numeric( $value ) ) { $value = "'" . $value; } return $value; } public function export_csv() { if ( !$this->can_use() ) { wp_die( 'You do not have permission to export this report.' ); } if ( !function_exists( 'wc_get_order' ) ) { wp_die( 'WooCommerce is required.' ); } $ids = $this->parse_order_ids(); if ( empty( $ids ) ) { wp_die( 'No orders were selected.' ); } $settings = $this->get_settings(); $built = $this->build_groups( $ids, $settings ); $enabled = array_diff( $this->enabled_columns( $settings ), array('image') ); $labels = $this->pro_columns( array( 'sku' => 'SKU', 'product_id' => 'Product ID', 'product' => 'Product', 'category' => 'Category', 'price' => 'Price', 'quantity' => 'Quantity', 'orders' => 'Orders', 'order_numbers' => 'Order numbers', ) ); $order = array_keys( $labels ); $cols = array_values( array_filter( $order, function ( $c ) use($enabled) { return in_array( $c, $enabled, true ); } ) ); $multi = count( $built ) > 1; nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="pickflow-picking-' . gmdate( 'Y-m-d-His' ) . '.csv"' ); $out = fopen( 'php://output', 'w' ); fprintf( $out, '%s', "" ); // UTF-8 BOM so Excel reads Arabic correctly. $head = array(); if ( $multi ) { $head[] = $this->pro_group_heading(); } foreach ( $cols as $c ) { $head[] = $labels[$c]; } fputcsv( $out, $head ); foreach ( $built as $group ) { foreach ( $group['rows'] as $row ) { $line = array(); if ( $multi ) { $line[] = ( isset( $group['label'] ) ? $group['label'] : '' ); } foreach ( $cols as $c ) { if ( 'sku' === $c ) { $line[] = $this->csv_cell( $row['sku'] ); } elseif ( 'product_id' === $c ) { $line[] = $row['product']->get_id(); } elseif ( isset( $labels[$c] ) && !in_array( $c, array( 'sku', 'product', 'category', 'price', 'quantity', 'orders', 'order_numbers' ), true ) ) { $line[] = $this->csv_cell( $this->pro_column_value( $c, $row ) ); } elseif ( 'product' === $c ) { $line[] = $this->csv_cell( $row['name'] ); } elseif ( 'category' === $c ) { $line[] = $this->csv_cell( $row['category'] ); } elseif ( 'price' === $c ) { $line[] = wc_format_decimal( $row['price'] ); } elseif ( 'quantity' === $c ) { $line[] = wc_format_decimal( $row['quantity'] ); } elseif ( 'orders' === $c ) { $line[] = count( $row['orders'] ); } elseif ( 'order_numbers' === $c ) { $bits = array(); foreach ( $row['orders'] as $info ) { $bits[] = $info['seq'] . ' (#' . $info['number'] . ') x' . wc_format_decimal( $info['qty'] ); } $line[] = implode( '; ', $bits ); } } fputcsv( $out, $line ); } } exit; // PHP closes php://output itself. } public function render_sheet() { if ( !$this->can_use() ) { wp_die( 'You do not have permission to view this sheet.' ); } if ( !function_exists( 'wc_get_order' ) ) { wp_die( 'WooCommerce is required.' ); } $ids = $this->parse_order_ids(); if ( empty( $ids ) ) { wp_die( 'No orders were selected.' ); } $settings = $this->get_settings(); $built = $this->build_groups( $ids, $settings ); $total_lines = 0; $total_units = 0; foreach ( $built as $group ) { $total_lines += count( $group['rows'] ); foreach ( $group['rows'] as $row ) { $total_units += (float) $row['quantity']; } } if ( 0 === $total_lines ) { wp_die( 'No products found in the selected orders.' ); } $sections = count( $built ); // Main cells of a row: tick, image, SKU, name, quantity. Everything else is a small line under the name. $enabled = $this->enabled_columns( $settings ); $shortages = ( !empty( $settings['show_shortages'] ) ? $this->shortages( $ids ) : array() ); $csv_url = $this->view_url( self::CSV_ACTION, $ids ); $show_image = in_array( 'image', $enabled, true ); $show_sku = in_array( 'sku', $enabled, true ); $show_name = in_array( 'product', $enabled, true ); $show_qty = in_array( 'quantity', $enabled, true ); $show_order_chips = in_array( 'order_numbers', $enabled, true ); $extras = array_values( array_intersect( array_merge( array_keys( $this->pro_extra_columns() ), array( 'category', 'product_id', 'price', 'orders' ) ), $enabled ) ); $has_name_cell = $show_name || $extras || $show_order_chips; $show_tick = !empty( $settings['show_tick'] ); $layout_two = $this->pro_two_columns( $settings ); $title = $settings['title']; // SKU column width follows the longest SKU in this report so every row lines up. $max_sku = 4; foreach ( $built as $group ) { foreach ( $group['rows'] as $row ) { $max_sku = max( $max_sku, strlen( (string) $row['sku'] ) ); } } $sku_width = min( 170, max( 44, (int) ceil( $max_sku * 8.800000000000001 ) + 6 ) ) . 'px'; $grid = array(); if ( $show_tick ) { $grid[] = '20px'; } if ( $show_image ) { $grid[] = '38px'; } if ( $show_sku ) { $grid[] = ( $has_name_cell ? $sku_width : 'minmax(0,1fr)' ); } if ( $has_name_cell ) { $grid[] = 'minmax(0,1fr)'; } if ( $show_qty ) { $grid[] = '44px'; } $grid_css = ( $grid ? implode( ' ', $grid ) : 'minmax(0,1fr)' ); ?>><?php echo esc_html( $title ); ?> print_script(); ?>
SweepickUse Print / Save as PDF, then close this tab.
CSV

orders product lines unitspro_sections_summary( $sections ), array( 'span' => array(), 'b' => array(), ) ); if ( $show_order_chips ) { ?>Order chips: position in batch · order number · ×quantity
skipped_html( 'lost' ) . $this->empty_orders_html( 'lost' ), $this->allowed_html() ); if ( $shortages ) { echo '

Short on stock: ' . esc_html( count( $shortages ) ) . (( 1 === count( $shortages ) ? ' product' : ' products' )) . ' cannot be fully picked

'; foreach ( $shortages as $short_row ) { echo ''; } echo '
SKUProductNeededIn stockShort by
' . esc_html( ( $short_row['sku'] ? $short_row['sku'] : '—' ) ) . '' . esc_html( $short_row['name'] ) . (( $short_row['backorder'] ? 'Backorder allowed' : '' )) . '' . esc_html( wc_format_localized_decimal( $short_row['required'] ) ) . '' . esc_html( wc_format_localized_decimal( max( 0, $short_row['stock'] ) ) ) . '' . esc_html( wc_format_localized_decimal( $short_row['short'] ) ) . '
'; } foreach ( $built as $group ) { $section_rows = array_values( $group['rows'] ); if ( empty( $section_rows ) ) { continue; } echo '
'; if ( $sections > 1 ) { $units = 0; foreach ( $section_rows as $r ) { $units += (float) $r['quantity']; } $meta = count( $section_rows ) . ' lines · ' . wc_format_localized_decimal( $units ) . ' units'; if ( null !== $group['orders'] ) { $meta = $group['orders'] . (( 1 === $group['orders'] ? ' order · ' : ' orders · ' )) . $meta; } echo ''; } echo ''; foreach ( $section_rows as $row ) { echo ''; } echo '
' . esc_html( ( isset( $group['label'] ) ? $group['label'] : '' ) ) . '' . esc_html( $meta ) . '
'; if ( $show_tick ) { echo ''; } if ( $show_image ) { echo '' . wp_kses_post( $row['product']->get_image( 'thumbnail' ) ) . ''; } if ( $show_sku ) { echo '' . esc_html( ( $row['sku'] ? $row['sku'] : '—' ) ) . ''; } if ( $has_name_cell ) { echo ''; if ( $show_name ) { echo '' . esc_html( $row['name'] ) . ''; } $meta_bits = array(); foreach ( $extras as $extra ) { if ( 'category' === $extra && '' !== $row['category'] ) { $meta_bits[] = '' . esc_html( $row['category'] ) . ''; } elseif ( 'product_id' === $extra ) { $meta_bits[] = 'ID ' . esc_html( $row['product']->get_id() ); } elseif ( 'price' === $extra ) { $meta_bits[] = wp_kses_post( wc_price( $row['price'] ) ); } elseif ( isset( $this->pro_extra_columns()[$extra] ) ) { $bit = $this->pro_column_bit( $extra, $row ); if ( '' !== $bit ) { $meta_bits[] = $bit; } } elseif ( 'orders' === $extra ) { $count = count( $row['orders'] ); $meta_bits[] = esc_html( $count ) . (( 1 === $count ? ' order' : ' orders' )); } } if ( $meta_bits ) { echo '' . wp_kses( implode( ' · ', $meta_bits ), $this->allowed_html() ) . ''; } if ( $show_order_chips ) { echo ''; foreach ( $row['orders'] as $order_info ) { echo '' . esc_html( $order_info['seq'] ) . '#' . esc_html( $order_info['number'] ) . (( $order_info['qty'] > 1 ? '×' . esc_html( wc_format_localized_decimal( $order_info['qty'] ) ) . '' : '' )) . ''; } echo ''; } echo ''; } if ( $show_qty ) { echo '' . esc_html( wc_format_localized_decimal( $row['quantity'] ) ) . ''; } echo '
'; } ?>