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' );
}
/**
* Add parcel machine information to order view (Customer)
*
* @since 3.0.0
*/
public function parcel_machine_details( $order ) {
$machine_id = get_post_meta( $order->get_id(), '_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;
}
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 ) {
echo '
';
$order_id = $order->get_id();
$machine_id = get_post_meta( $order_id, '_parcel_machine', true );
if ( !empty( $machine_id ) ) {
list( $carrier, $machine ) = explode( '||', $machine_id );
if ( !empty( $machine ) ) {
$machine = self::mk_get_machine( $carrier, $machine );
if ( $machine ) {
echo '
'.__( 'Selected parcel machine', 'wc_makecommerce_domain' ).''.__( 'Edit' ).'
'.__( 'Parcel machine', 'wc_makecommerce_domain' ).'
';
$res = '';
$machines = self::mk_get_machines( $carrier, method_exists( $order, 'get_shipping_country' ) ? $order->get_shipping_country() : $order->shipping_country );
$res .= ''.__( '-- select parcel machine --', 'wc_makecommerce_domain' ).' ';
$pcity = false;
foreach ( $machines as $smachine ) {
$city = strtolower( $smachine['city'] );
if ( $city !== $pcity ) {
if ( $pcity ) {
$res .= '';
}
$res .= '';
}
$mname = $smachine['name'];
$res .= ''.$mname.' ';
$pcity = $city;
}
if ( $pcity ) {
$res .= ' ';
}
echo $res;
echo '
'.ucfirst($carrier).' '.$machine['name'] . '' . $machine['address'] . '
';
}
}
}
if ( empty( $carrier ) ) {
$carrier = '';
}
$shipment_id = get_post_meta( $order_id, '_parcel_machine_shipment_id', true );
$shipment_id_error = get_post_meta( $order_id, '_parcel_machine_error', true );
$shipment_manifest = get_post_meta( $order_id, '_parcel_machine_manifest', 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 = get_post_meta( $order_id, '_delivery_time', true );
if ( !$shipment_id_error ) {
if ( $delivery_time === '1' ) {
echo '
'.__( 'Delivery time', 'wc_makecommerce_domain' ).' - '.__( 'Any time', 'wc_makecommerce_domain' ).'
';
}
if ( $delivery_time === '2' ) {
echo '
'.__( 'Delivery time', 'wc_makecommerce_domain' ).' - 09:00..17:00
';
}
if ( $delivery_time === '3' ) {
echo '
'.__( 'Delivery time', 'wc_makecommerce_domain' ).' - 17:00..21:00
';
}
}
}
}
if ( $shipment_id ) {
$trackinglink = '
'.__( 'Shipment tracking code', 'wc_makecommerce_domain' ).' ';
//check if its omniva courier
if ( $carrier == "" ) {
$carrier = $this->get_order_shipment_carrier( $order );
}
$theLink = $this->get_tracking_link( $carrier, $order_id, true );
if ( $carrier != "" && $theLink != "" ) {
$trackinglink .= ' ('. ucfirst( $carrier ) .'): ' . $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' ) {
$machine_id = get_post_meta( $the_order_id, '_parcel_machine', true );
$shipment_id = get_post_meta( $the_order_id, '_parcel_machine_shipment_id', true );
$shipment_id_error = get_post_meta( $the_order_id, '_parcel_machine_error', true );
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 '';
echo ''.__( '-- filter by shipping method', 'wc_makecommerce_domain' ) . ' ';
foreach ( $methods as $method ) {
echo 'id ? ' selected="selected"' : '').'>'.$method->title.' ';
}
echo ' ';
}
}
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 ) {
//check if makecommerce shipping has been used
//add tracking information (if possible)
$shipment_id = get_post_meta( $order->get_id(), "_parcel_machine_shipment_id", true );
$machine_id = get_post_meta( $order->get_id(), '_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
$fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).')', 'value' => $machine['name'].' - '.$machine['address']);
}
$shipment_id_error = get_post_meta( $order->get_id(), '_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 = get_post_meta( $order->get_id(), '_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 ) {
$trackinglink = ''.__(' Shipment tracking code', 'wc_makecommerce_domain' ).' ';
if ( $carrier == "" ) {
$carrier = $this->get_order_shipment_carrier( $order );
}
$href = $this->get_tracking_link( $carrier, $order->get_id() );
if ( $href != "" ) {
$href .= $shipment_id;
$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;
}
}