loader = $loader; $this->define_hooks(); } /** * Define all wordpress hooks * * @since 3.0.0 */ public function define_hooks() { $this->loader->add_filter( 'woocommerce_order_details_after_customer_details', $this, 'parcel_machine_details' ); $this->loader->add_filter( 'woocommerce_admin_order_data_after_shipping_address', $this, 'parcel_machine_changing', 10, 1 ); $this->loader->add_filter( 'manage_shop_order_posts_custom_column', $this, 'shipping_method_order_view', 3 ); $this->loader->add_filter( 'woocommerce_email_customer_details_fields', $this, 'shipping_email_details', 10, 3 ); $this->loader->add_filter( 'restrict_manage_posts', $this, 'filter_orders' ); // HPOS Add shipping method filtering selectbox to Woo orders page as well $this->loader->add_filter( 'woocommerce_order_list_table_restrict_manage_orders', $this, 'hpos_filter_orders' ); // HPOS Add shipping method filtering functionality $this->loader->add_filter( 'woocommerce_order_query_args', $this, 'order_filter_by_shipping_method' ); // HPOS Add bulk actions $this->loader->add_filter( 'bulk_actions-woocommerce_page_wc-orders', $this, 'mc_bulk_actions' ); } /** * Add parcel machine information to order view (Customer) * * @since 3.0.0 */ public function parcel_machine_details( $order ) { $machine_id = $order->get_meta( '_parcel_machine', true ); if ( empty( $machine_id ) ) { return; } list( $carrier, $machine ) = explode( '||', $machine_id ); if ( empty( $machine ) ) { return; } $machine = self::mk_get_machine( $carrier, $machine ); if ( !$machine ) { return; } if ( $carrier == 'lp_express_lt' ) { $carrier = "LP Express"; } echo ' '.__( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).') '.$machine['name'].'
'.$machine['address'].' '; } /** * Allow changing parcel machine in admin and show data in order view * * @since 3.0.0 */ public function parcel_machine_changing( $order ) { $machine_id = $order->get_meta( '_parcel_machine', true ); $carrier = ''; if ( !empty( $machine_id ) ) { list( $carrier, $machine ) = explode( '||', $machine_id ); } if ( !empty( $machine ) ) { $machine = self::mk_get_machine( $carrier, $machine ); if ( $machine ) { echo '

'; // Add the parcel size templates to LP Express orders if ( $carrier === 'lp_express_lt' ) { echo '

'; // Show the chosen template in the order view echo '

LP Express
'.$machine['name'] . '
' . $machine['address'] . '

Chosen template for LP Express: '.$chosenTemplate.'

'; } else { echo '

'.ucfirst( $carrier ).'
'.$machine['name'] . '
' . $machine['address'] . '

'; } } } else { //in case of a courier, wrap the time and trackinglink in address div as well //to hide it during order editing. //can be an empty div in case of other shipping methods echo '
'; } $shipment_id = $order->get_meta( '_parcel_machine_shipment_id', true ); $shipment_id_error = $order->get_meta( '_parcel_machine_error', true ); $shipment_manifest = $order->get_meta( '_parcel_machine_manifest', true ); $shippingMethod["method_title"] = ''; //currently the only delivery time option exists for smartpost courier shipping method. //only one shipping method, get it easily from array with foreach foreach ( $order->get_shipping_methods() as $shippingMethod) { if ( $shippingMethod["method_id"] == "courier_smartpost" && !$shipment_id_error ) { $delivery_time = $order->get_meta( '_delivery_time', true ); switch ( $delivery_time ) { case '1': echo '

'.__( 'Delivery time', 'wc_makecommerce_domain' ).' - '.__( 'Any time', 'wc_makecommerce_domain' ).'

'; break; case '2': echo '

'.__( 'Delivery time', 'wc_makecommerce_domain' ).' - 09:00..17:00

'; break; case '3': echo '

'.__( 'Delivery time', 'wc_makecommerce_domain' ).' - 17:00..21:00

'; break; } } } if ( $shipment_id ) { $trackinglink = '

'.__( 'Shipment tracking code', 'wc_makecommerce_domain' ).''; if ( $carrier == "" ) { $carrier = $this->get_order_shipment_carrier( $order ); } if ( $carrier === 'lp_express_lt' ) { $shipment_id = $order->get_meta( '_parcel_machine_shipment_barcode', true ); $carrier = "lpexpress"; } $theLink = $this->get_tracking_link( $carrier, $order, $shipment_id, true ); if ( $carrier != "" && $theLink != "" ) { $trackinglink .= ' ('. ucfirst( $shippingMethod["method_title"] ) .'):
' . $shipment_id . ''; } else { $trackinglink .= ':
'.$shipment_id; } echo $trackinglink.'

'; } if ( $shipment_manifest ) { echo '

'.__( 'Shipments pickup manifest' ).':
link

'; } if ( $shipment_id_error ) { echo '

'.__( 'Shipment registration error' ).':
' . $shipment_id_error . '

'; } echo '
'; } /** * Add shipping information on order list view * * @since 3.0.0 */ public function shipping_method_order_view( $column ) { global $post, $woocommerce, $the_order; if ( !$the_order ) { return; } $the_order_id = $the_order->get_id(); if ( empty( $the_order ) || $the_order_id != $post->ID ) { $the_order = wc_get_order( $post->ID ); } if ( $column === 'shipping_address' ) { $shipment_id = $the_order->get_meta( '_parcel_machine_shipment_id', true ); $shipment_id_error = $the_order->get_meta( '_parcel_machine_error', true ); $identifier = $the_order->get_meta( '_lp_express_cart_identifier', true ); $barcode = $the_order->get_meta( '_parcel_machine_shipment_barcode', true ); // LP Express if ( !empty( $identifier ) && !empty( $barcode ) ) { $shipment_id = $barcode; } if ( $shipment_id ) { echo __( 'Shipment tracking code', 'wc_makecommerce_domain' ) . ': ' . $shipment_id . '
'; echo ''; } else if ( $shipment_id_error ) { echo ''.__( 'Package shipment generation error:', 'wc_makecommerce_domain' ) . '
' .$shipment_id_error; } } } /** * Fires before the Filter button on the Posts and Pages list tables. * Allows filtering of orders by shipping method * * @since 3.0.0 */ public function filter_orders() { global $typenow; if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) { $selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false; $methods = WC()->shipping->load_shipping_methods(); echo ''; } } /** * Fires before the Filter button on the orders page * Allows filtering of orders by shipping method * * @since 3.4.0 */ public function hpos_filter_orders() { if ( $_GET['page'] == 'wc-orders' ) { $selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false; $methods = WC()->shipping->load_shipping_methods(); echo ''; } } /** * Returns order shipment carrier * * @since 3.0.0 */ private function get_order_shipment_carrier( $order ) { foreach ( $order->get_shipping_methods() as $shippingMethod ) { $carrier = explode( "_", $shippingMethod["method_id"] ); $carrier = $carrier[1]; } return $carrier; } /** * Add shipment data to email * * @since 3.0.0 */ public function shipping_email_details( $fields, $sent_to_admin, $order ) { // No mc metadata without calling the wc_get_order again $order = wc_get_order( $order->get_id() ); //check if makecommerce shipping has been used //add tracking information (if possible) $shipment_id = $order->get_meta( "_parcel_machine_shipment_id", true ); $machine_id = $order->get_meta( '_parcel_machine', true ); $machine = ""; $carrier = ""; $machineIdArr = explode( '||', $machine_id ); if ( count( $machineIdArr ) == 2 ) { $carrier = $machineIdArr[0]; $machine = $machineIdArr[1]; } else if ( count( $machineIdArr ) == 1 ) { $carrier = $machineIdArr[0]; } $machine = self::mk_get_machine( $carrier, $machine ); //do something about the language only if we even have something to add to the email if ( $shipment_id || ( $machine != "" && $carrier != "" ) ) { //this only needs to be done for polylang. Don't do it for wpml, that won't work. if ( \MakeCommerce\i18n::is_polylang_active() ) { $orderLang = get_locale(); if ( $orderLang != "" ) { if ( substr( $orderLang, 0 ,2 ) == "en" ) { switch_to_locale( "et" ); do_action( 'wpml_switch_language', "et" ); } else { switch_to_locale( "en_GB" ); do_action( 'wpml_switch_language', "en_GB" ); } switch_to_locale( $orderLang ); do_action( 'wpml_switch_language', $orderLang ); } load_plugin_textdomain( 'wc_makecommerce_domain', false, '/makecommerce/languages/' ); } } if ( $machine != "" && $carrier != "" ) { //add parcel machine name/location if ( strtolower( $carrier ) == 'lp_express_lt') { $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' (LP Express)', 'value' => $machine['name'].' - '.$machine['address']); } else { $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).')', 'value' => $machine['name'].' - '.$machine['address']); } } $shipment_id_error = $order->get_meta( '_parcel_machine_error', true ); //currently the only delivery time option exists for smartpost courier shipping method. foreach ( $order->get_shipping_methods() as $shippingMethod ) { if ( $shippingMethod["method_id"] == "courier_smartpost" ) { $delivery_time = $order->get_meta( '_delivery_time', true ); if ( !$shipment_id_error ) { if ( $delivery_time === '1' ) { $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => __( 'Any time', 'wc_makecommerce_domain' ) ); } if ( $delivery_time === '2' ) { $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "09:00..17:00" ); } if ( $delivery_time === '3' ) { $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "17:00..21:00" ); } } } } //add tracking information (if possible) if ( $shipment_id && strtolower( $carrier ) !== "lp_express_lt" ) { if ( $carrier == "" ) { $carrier = $this->get_order_shipment_carrier( $order ); } $href = $this->get_tracking_link( $carrier, $order, $shipment_id ); if ( $href != "" ) { $fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ).' ('.$carrier.')', 'value' => "".$href."" ); } else { $fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ), 'value' => $shipment_id ); } } return $fields; } /** * Adds bulk actions to orders page without JS * Works with HPOS enabled * * @since 3.4.0 */ public function mc_bulk_actions( $actions ) { $actions['parcel_machine_register_orders'] = __( 'Register parcel machine shipments', 'wc_makecommerce_domain' ); $actions['parcel_machine_print_labels'] = __( 'Print parcel machine labels', 'wc_makecommerce_domain' ); return $actions; } /** * Filters orders by _mc_shipping_method meta key * Relies on hpos_filter_orders() * Works with HPOS enabled * * @since 3.4.0 */ public function order_filter_by_shipping_method( $args ) { if ( is_admin() && !empty( $_REQUEST['_shipping_method'] ) && $_GET['page'] == 'wc-orders' ) { $shipping_method = sanitize_text_field( $_REQUEST['_shipping_method'] ); $args['meta_query'] = [ [ 'key' => '_mc_shipping_method', 'value' => $shipping_method ] ]; } return $args; } }