How to add shipment history after client create shipment

wpcpq-pending-quotation

Copy and paste this code in your functions.php file in your active theme

add_action('plugins_loaded', 'remove_wpcfe_save_shipment_history');
function remove_wpcfe_save_shipment_history(){
	remove_action( 'after_wpcfe_save_shipment', 'wpcfe_save_shipment_history', 10, 2 );
}
add_action( 'after_wpcfe_save_shipment', 'after_wpcfe_save_shipment_callback', 10, 2 );
function after_wpcfe_save_shipment_callback( $post_id, $data ) {
	global $wpdb, $wpcargo;
	$current_user 			= wp_get_current_user();
	$gen_settings 			= $wpcargo->settings;
	$edit_history_role 		= ( array_key_exists( 'wpcargo_edit_history_role', $gen_settings ) ) ? $gen_settings['wpcargo_edit_history_role'] : array();
    $role_intersected 		= array_intersect( $current_user->roles, $edit_history_role );
    $old_status             = get_post_meta($post_id, 'wpcargo_status', true);    
	
	$user_id        = get_current_user_id();
    $full_name      = $wpcargo->user_fullname( $user_id );
	
	if( $role_intersected ){
        $history = array();
        $shipments_update = array();
        if( isset( $data['wpcargo_shipments_update'] ) ){
            $shipments_update = $data['wpcargo_shipments_update'];
        }   
        if( !empty( $shipments_update ) ){
            foreach ($shipments_update as $h_record ) {
                if( array_key_exists( 'updated-name', $h_record ) && empty($h_record['updated-name']) ){
                    $h_record['updated-name'] = $full_name;
                }
                $history[] = $h_record;
            }
        }
        
		if( isset( $data['status'] ) &&  $data['status'] != '' ){
            wpcfe_save_report( $post_id, $old_status, sanitize_text_field( $data['status'] ) );
            update_post_meta( $post_id, 'wpcargo_status', sanitize_text_field( $data['status'] ) );
			update_post_meta( $post_id, 'location', sanitize_text_field( $data['location'] ) );
			$new_history = array();
			foreach( wpcargo_history_fields() as $history_name => $history_fields ){
				if( $history_name != 'updated-name' ){
                    $value = array_key_exists( $history_name, $data ) ? sanitize_text_field( $data[$history_name] ) : '' ;
					$new_history[$history_name] = $value;
				}
			}
			$new_history['updated-name'] = $full_name;
			$history[] = $new_history;
			if( $data['status'] != $old_status ){
                wpcargo_send_email_notificatio( $post_id, $data['status'] );

			}
		}
		$history = maybe_serialize( $history );
		update_post_meta( $post_id, 'wpcargo_shipments_update', $history );
	}else{
		if( empty($old_status) ){
			$data['status'] = wpcfe_default_status();
			$history        = get_post_meta( $post_id, 'wpcargo_shipments_update', true ) ? maybe_unserialize( get_post_meta( $post_id, 'wpcargo_shipments_update', true ) ) : array() ;
			$new_history = array();
			foreach( wpcargo_history_fields() as $history_name => $history_fields ){
				if( $history_name != 'updated-name' ){
                    $value = array_key_exists( $history_name, $data ) ? sanitize_text_field( $data[$history_name] ) : '' ;
					$new_history[$history_name] = $value;
				}
			}
			/*
			 * Change the following value if you want
			 */
			$new_history['updated-name'] = $full_name;
			$new_history['date'] = date("Y-m-d");
			$new_history['time'] = date("h:i:sa");
			$new_history['location'] = '';
			$new_history['status'] = wpcfe_default_status();
			$new_history['remarks'] = '';

			$history[] = $new_history;
			$history = maybe_serialize( $history );
			update_post_meta( $post_id, 'wpcargo_shipments_update', $history );
			if( $data['status'] != $old_status ){
				wpcargo_send_email_notificatio( $post_id, $data['status'] );
			}
		}
	}
}
0
    0
    Your Cart
    Your cart is emptyReturn to Shop