Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • None
  • Order-Delivery-Date-Pro-for-WooCommerce
    • Admin
      • Edit-Order
      • Non-Sensitive-Data-Capture
      • Settings
        • Custom-Delivery
        • General
        • Google-Calendar-Sync
    • Class-ORDDD-Email-Manager
    • Common-Functions
    • Delivery-Calendar
    • Emails
      • Class-ORDDD-Email-Update-Date
    • Filter
    • Frontend
      • Checkout-Page-Processes
      • ICS-Files-Data
    • Google-Calendar
    • Integration
    • Lang
    • License
    • Plugin-Updates
      • EDD-SL-Plugin-Updater

Classes

  • EDD_SL_Plugin_Updater
  • orddd_additional_settings
  • orddd_admin_delivery_class
  • orddd_appearance_settings
  • orddd_calendar_sync
  • orddd_calendar_sync_settings
  • orddd_class_view_deliveries
  • orddd_common
  • orddd_date_settings
  • orddd_delivery_days_settings
  • ORDDD_Email_Manager
  • ORDDD_Email_Update_Date
  • orddd_filter
  • orddd_holidays_settings
  • orddd_integration
  • orddd_license
  • orddd_process
  • orddd_settings
  • orddd_shipping_based_settings
  • orddd_shipping_days_settings
  • orddd_shipping_multiple_address
  • orddd_time_settings
  • orddd_time_slot_settings
  • ORDDD_View_Delivery_Dates_Table
  • ORDDD_View_Disable_Time_Slots_Table
  • ORDDD_View_Holidays_Table
  • ORDDD_View_Shipping_Settings_Table
  • ORDDD_View_Time_Slots_Table
  • OrdddGcal
  • order_delivery_date
  • TS_Tracker
  • TS_tracking

Functions

  • orddd_get_dateToCal
  • orddd_get_escapeString
  • orddd_t
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 
<?php 
/**
 * Order Delivery Date Pro for WooCommerce
 *
 * Handles the display and filtering of delivery details in WooCommerce->Orders
 *
 * @author      Tyche Softwares
 * @package     Order-Delivery-Date-Pro-for-WooCommerce/Filter
 * @since       2.7
 */

include_once( dirname( __FILE__ ) . '/orddd-common.php' );

/**
 * orddd_filter Class
 *
 * @class orddd_filter
 */
class orddd_filter {

    /**
     * This function is used to add the custom plugin column 
     * Delivery Date on WooCommerce->Orders page.
     * 
     * @param array $columns - The Existing columns for the WooCommerce->Orders table.
     * @return array $new_columns - Updated list of column names.
     * 
     * @hook manage_edit-shop_order_columns
     * @since 2.7
     */
    public static function orddd_woocommerce_order_delivery_date_column( $columns ) {
        // get all columns up to and excluding the 'order_actions' column
        $new_columns = array();
        foreach ( $columns as $name => $value ) {
            if ( $name == 'order_actions' ) {
                prev( $columns );
                break;
            }
            $new_columns[ $name ] = $value;
        }
        // inject our columns
        $new_columns[ 'order_delivery_date' ] = get_option( 'orddd_delivery_date_field_label' );
        // add the 'order_actions' column, and any others
        foreach ( $columns as $name => $value ) {
            $new_columns[ $name ] = $value;
        }
        return $new_columns;
    }

    /**
     * This function echoes the delivery details to the 
     * 'Delivery Date' column on WooCommerce->Orders for each order.
     * 
     * @param string $column - Column Name
     * 
     * @hook manage_shop_order_posts_custom_column
     * @since 2.7
     */
    public static function orddd_woocommerce_custom_column_value( $column ) {
        global $post, $orddd_date_formats;
        if ( $column == 'order_delivery_date' ) {
            $delivery_date_formatted = orddd_common::orddd_get_order_delivery_date( $post->ID  );
            echo $delivery_date_formatted;
            
            $time_slot = orddd_common::orddd_get_order_timeslot( $post->ID );
            echo '<p>' . $time_slot . '</p>';
        }   
        do_action( 'orddd_add_value_to_woocommerce_custom_column', $column, $post->ID );
    }

    /**
     * Adds the Delivery Date column in WooCommerce->Orders
     * as a sortable column. Mentions the meta key present in
     * post meta table that can be used for sorting.
     * 
     * @param array $columns - List of sortable columns
     * @return array - Sortable columns with the plugin column included.array
     * 
     * @hook manage_edit-shop_order_sortable_columns
     * @since 2.7
     */
    public static function orddd_woocommerce_custom_column_value_sort( $columns ) {
        $columns[ 'order_delivery_date' ] = '_orddd_timestamp';
        return $columns;
    }

    /**
     * Delivery date column orderby. 
     * 
     * Helps WooCommerce understand using the value based on which a column should be sorted.
     * The delivery date is stored as a timestamp in the _orddd_timestamp variable in wp_postmeta
     * 
     * @param array $vars - Query variables
     * @return array $vars - Updated Query variables.
     * 
     * @hook request
     * @since 2.7
     */
    public static function orddd_woocommerce_delivery_date_orderby( $vars ) {
        global $typenow;
        if( get_option( "orddd_show_column_on_orders_page_check" ) == 'on' ) {
            $delivery_field_label = '_orddd_timestamp';
            if ( isset( $vars[ 'orderby' ] ) ) {
                if ( $delivery_field_label == $vars[ 'orderby' ] ) {
                    $sorting_vars = array( 'orderby'  => array( 'meta_value_num' => $vars[ 'order' ], 'date' => 'ASC' ) );
                    if ( !isset( $_GET[ 'order_delivery_date_filter' ] ) || $_GET['order_delivery_date_filter'] == '') {
                        $sorting_vars[ 'meta_query' ] = array(  'relation' => 'OR', 
                            array (
                                'key'     => $delivery_field_label, 
                                'value'   => '', 
                                'compare' => 'NOT EXISTS'
                            ),
                            array (
                                    'key'     => $delivery_field_label,
                                    'compare' => 'EXISTS'
                                )
                            );
                    }
                    $vars = array_merge( $vars, $sorting_vars );
                }
            } elseif( get_option( "orddd_enable_default_sorting_of_column" ) == 'on' ) {
                if ( 'shop_order' != $typenow ) {
                    return $vars;
                }
                $sorting_vars = array(
                    'orderby'  => array( 'meta_value_num' => 'DESC', 'date' => 'ASC' ),
                    'order'    => 'DESC' );
                if ( !isset( $_GET[ 'order_delivery_date_filter' ] ) || $_GET[ 'order_delivery_date_filter'     ] == '' ) {
                    $sorting_vars[ 'meta_query' ] = array( 'relation' => 'OR', 
                            array (
                                    'key'     => $delivery_field_label, 
                                    'value'   => '', 
                                    'compare' => 'NOT EXISTS'
                                ),
                            array (
                                    'key'     => $delivery_field_label,
                                    'compare' => 'EXISTS'
                                )
                        );
                }
                $vars = array_merge( $vars, $sorting_vars );
            }
        }
        return $vars;
    }
    
    /**
     * Prints a dropdown to filter the orders based on Delivery Dates
     * in WooCommerce->Orders.
     * 
     * @hook restrict_manage_posts
     * @since 2.7
     */
    public static function orddd_restrict_orders() {
        global $typenow, $wpdb, $wp_locale;

        if ( 'shop_order' != $typenow ) {
            return;
        }

        $javascript = '';
        $filter_field_name = 'order_delivery_date_filter';
        $db_field_name = '_orddd_timestamp';
    
        $months = $wpdb->get_results( $wpdb->prepare( "
        SELECT YEAR( FROM_UNIXTIME( meta_value ) ) as year, MONTH( FROM_UNIXTIME( meta_value ) ) as month, CAST( meta_value AS UNSIGNED ) AS meta_value_num
        FROM " . $wpdb->postmeta . "
        WHERE meta_key = %s
        GROUP BY year, month
        ORDER BY meta_value_num DESC", $db_field_name ) );
        $month_count = count( $months );

        if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) {
            return;
        }

        if ( isset( $_GET[ $filter_field_name ] ) && $_GET[ $filter_field_name ] == 'today' ) {
            $m = $_GET[ $filter_field_name ];
        } else if ( isset( $_GET[ $filter_field_name ] ) && $_GET[ $filter_field_name ] == 'tomorrow' ) {
            $m = $_GET[ $filter_field_name ];
        } else {
            $m = isset( $_GET[ $filter_field_name ] ) ? (int) $_GET[ $filter_field_name ] : 0;
        }
        
        $current_time = current_time( 'timestamp' );
        $today_option = array( 'year' => date( 'Y', $current_time ), 'month' => 'today', 'meta_value_num' => $current_time );

        $tomorrow_date = date( 'Y-m-d', strtotime( '+1 day', $current_time ) );
        $tomorrow_time = strtotime( $tomorrow_date );
        $tomorrow_option = array( 'year' => date( 'Y', $tomorrow_time ), 'month' => 'tomorrow', 'meta_value_num' => $tomorrow_time );
        array_unshift( $months, (object)$today_option, (object)$tomorrow_option );
        ?>
        <select name="order_delivery_date_filter" id="order_delivery_date_filter" class="orddd_filter">
            <option value=""><?php _e( "Show all Delivery Dates", "order-delivery-date" ); ?></option>
            <?php
            foreach ( $months as $arc_row ) {
                if ( $arc_row->month != 'today' && $arc_row->month != 'tomorrow' ) {
                    if ( 0 == $arc_row->year || '1969' == $arc_row->year ) {
                        continue;
                    }
                    $month = zeroise( $arc_row->month, 2 );
                    $year = $arc_row->year;
                    printf( '<option %s value="%s">%s</option>',
                        selected( $m, $year . $month, false ),
                        esc_attr( $arc_row->year . $month ),
                        /* translators: 1: month name, 2: 4-digit year */
                        sprintf( __( '%1$s %2$d', 'order-delivery-date' ), $wp_locale->get_month( $month ), $year )
                    );
                } else {
                    $arc_row->year = $year = '';
                    $month = $arc_row->month;
                    printf( '<option %s value="%s">%s</option>',
                        selected( $m, $arc_row->month, false ),
                        $arc_row->month,
                        ucfirst( $arc_row->month )
                    );
                }
            }
        ?></select><?php

        $javascript .= "jQuery( 'select#order_delivery_date_filter' ).select2();";
        wc_enqueue_js( $javascript );
    }
    
    /**
     * Filter the orders displayed in WooCommerce->Orders
     * based on the Delivery Dates filter dropdown.
     *
     * @param array $vars - Query Variables
     * @return array $vars - Updated Query Variables
     * 
     * @hook request
     * @since 2.7
     */
    public static function orddd_add_filterable_field( $vars ) {
        global $typenow;
        if ( 'shop_order' != $typenow ) {
            return $vars;
        }
        $meta_queries = array( 'relation' => 'AND' );

        // if the field is filterable and selected by the user
        if ( isset( $_GET[ 'order_delivery_date_filter' ] ) && $_GET[ 'order_delivery_date_filter' ] ) {
            $date = $_GET[ 'order_delivery_date_filter' ];
            if ( $date == 'today' ) {
                // from the start to the end of the month
                $current_time = current_time( 'timestamp' );
                $current_date = date( 'Y-m-d', $current_time );
                 
                $from_date = date( 'Y-m-d H:i:s', strtotime( $current_date . '00:00:00' ) );
                $to_date = date( 'Y-m-d H:i:s', strtotime( $current_date . '23:59:59' ) );
                
                $meta_queries[] = array(
                    'key'     => '_orddd_timestamp',
                    'value'   => array( strtotime( $from_date ), strtotime( $to_date ) ),
                    'type'    => 'NUMERIC',
                    'compare' => 'BETWEEN'
                );
            } else if ( $date == 'tomorrow' ) {             
                $current_time = current_time( 'timestamp' );
                $current_date = date( 'Y-m-d', strtotime('+1 day', $current_time ) );
                 
                $from_date = date( 'Y-m-d H:i:s', strtotime( $current_date . '00:00:00' ) );
                $to_date = date( 'Y-m-d H:i:s', strtotime( $current_date . '23:59:59' ) );
                
                $meta_queries[] = array(
                    'key'     => '_orddd_timestamp',
                    'value'   => array( strtotime( $from_date ), strtotime( $to_date ) ),
                    'type'    => 'NUMERIC',
                    'compare' => 'BETWEEN'
                );
            } else {
                // from the start to the end of the month
                $from_date = substr( $date, 0, 4 ) . '-' . substr( $date, 4, 2 ) . '-01';
                $to_date   = substr( $date, 0, 4 ) . '-' . substr( $date, 4, 2 ) . '-' . date( 't', strtotime( $from_date ) );
                $meta_queries[] = array(
                    'key'     => '_orddd_timestamp',
                    'value'   => array( strtotime( $from_date.' 00:00:00' ), strtotime( $to_date .' 23:59:59' ) ),
                    'type'    => 'NUMERIC',
                    'compare' => 'BETWEEN'
                );
            }
        }
        // update the query vars with our meta filter queries, if needed
        if ( count( $meta_queries ) > 1 ) {
            $vars = array_merge(
                $vars,
                array( 'meta_query' => $meta_queries )
            );
        }
        return $vars;
    }

    /** 
     * Adds the Delivery Date field to the set of searchable fields so that
     * the orders can be searched based on Delivery details.
     *
     * @param array $search_fields - Array of post meta fields to search by 
     * @return array $search_fields - Updated array of post meta fields to search by 
     *  
     * @hook woocommerce_shop_order_search_fields
     * @since 2.7 
     */
    public static function orddd_add_search_fields( $search_fields ) {
        array_push( $search_fields, get_option( 'orddd_delivery_date_field_label' ) );
        return $search_fields;
    }

    /**
     * Echoes the Delivery date on WooCommerce->Orders->Edit Order page.
     * 
     * @param WC_Order $order - Order object
     * 
     * @hook woocommerce_admin_order_data_after_billing_address
     *       woocommerce_admin_order_data_after_shipping_address
     * @since 2.7      
     */
    public static function orddd_display_delivery_date_admin_order_meta( $order ) {
        global $orddd_date_formats;
        
        if( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) {            
            $order_id = $order->get_id();
        } else {
            $order_id = $order->id;
        }
        
        $delivery_date_formatted = orddd_common::orddd_get_order_delivery_date( $order_id );
        
        $post_array = array();
        $post_array[ 'billing_country' ] = get_post_meta( $order_id, '_billing_country', true );
        $post_array[ 'billing_state' ] = get_post_meta( $order_id, '_billing_state', true );
        $post_array[ 'billing_postcode' ] = get_post_meta( $order_id, '_billing_postcode', true );
        $post_array[ 'ID' ] = $order_id;
        $zone_details = explode( "-", orddd_common::orddd_get_zone_id( $post_array, false ) );

        $zone_id = $zone_details[ 0 ];

        $shipping_method = orddd_common::orddd_get_order_shipping_method( $order_id );
        if( '' != $zone_id ) {
             if( false !== strpos( $shipping_method, 'usps' ) ) {
                $shipping_method = $zone_id . ":" . $shipping_method;
            }
            if( false !== strpos( $shipping_method, 'fedex' ) ) {
                $shipping_method = $zone_id . ":" . $shipping_method;
            }
        }

        $product_category = orddd_common::orddd_get_order_product_category( $order_id );
        $shipping_class = orddd_common::orddd_get_product_shipping_class( $order_id );

        $field_date_label = orddd_common::orddd_get_delivery_date_field_label( $shipping_method, $product_category, $shipping_class ); 
        
        if( $delivery_date_formatted != '' ) {
            echo '<p><strong>' . __( $field_date_label, 'order-delivery-date' ) . ': </strong>' . $delivery_date_formatted;
        }
    }
    
    /**
     * Echoes the Delivery time on WooCommerce->Orders->Edit Order page.
     * 
     * @param WC_Order $order - Order object
     * 
     * @hook woocommerce_admin_order_data_after_billing_address
     *       woocommerce_admin_order_data_after_shipping_address
     * @since 2.7      
     */
    public static function orddd_display_time_slot_admin_order_meta( $order ) {
        if( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) {            
            $order_id = $order->get_id();
        } else {
            $order_id = $order->id;
        }
        $time_slot = orddd_common::orddd_get_order_timeslot( $order_id );

        $post_array = array();
        $post_array[ 'billing_country' ] = get_post_meta( $order_id, '_billing_country', true );
        $post_array[ 'billing_state' ] = get_post_meta( $order_id, '_billing_state', true );
        $post_array[ 'billing_postcode' ] = get_post_meta( $order_id, '_billing_postcode', true );
        $post_array[ 'ID' ] = $order_id;
        $zone_details = explode( "-", orddd_common::orddd_get_zone_id( $post_array, false ) );

        $zone_id = $zone_details[ 0 ];

        $shipping_method = orddd_common::orddd_get_order_shipping_method( $order_id );
        if( '' != $zone_id ) {
             if( false !== strpos( $shipping_method, 'usps' ) ) {
                $shipping_method = $zone_id . ":" . $shipping_method;
            }
            if( false !== strpos( $shipping_method, 'fedex' ) ) {
                $shipping_method = $zone_id . ":" . $shipping_method;
            }
        }

        $product_category = orddd_common::orddd_get_order_product_category( $order_id );
        $shipping_class = orddd_common::orddd_get_product_shipping_class( $order_id );

        $time_field_label = orddd_common::orddd_get_delivery_time_field_label( $shipping_method, $product_category, $shipping_class ); 

        if ( $time_slot != '' && $time_slot != '' ) {
            echo '<p>' . $time_field_label . ': ' . $time_slot . '</p>';
        }
    }
}
$orddd_filter = new orddd_filter();
?>
API documentation generated by ApiGen