WooCommerce Print Invoice & Delivery Note
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • None
  • WooCommerce-Delivery-Notes
    • Admin
      • Edit-Order
    • Includes
    • Settings
    • Theme

Classes

  • WCDN_Welcome
  • WooCommerce_Delivery_Notes
  • WooCommerce_Delivery_Notes_Print
  • WooCommerce_Delivery_Notes_Settings
  • WooCommerce_Delivery_Notes_Theme
  • WooCommerce_Delivery_Notes_Writepanel

Functions

  • WCDN
  • wcdn_add_refunded_order_totals
  • wcdn_additional_product_fields
  • wcdn_company_info
  • wcdn_company_logo
  • wcdn_company_name
  • wcdn_content
  • wcdn_customer_notes
  • wcdn_document_title
  • wcdn_get_company_logo_id
  • wcdn_get_customer_notes
  • wcdn_get_formatted_item_price
  • wcdn_get_imprint
  • wcdn_get_order
  • wcdn_get_order_info
  • wcdn_get_order_invoice_date
  • wcdn_get_order_invoice_number
  • wcdn_get_orders
  • wcdn_get_personal_notes
  • wcdn_get_policies_conditions
  • wcdn_get_print_link
  • wcdn_get_template_content
  • wcdn_get_template_title
  • wcdn_get_template_type
  • wcdn_has_customer_notes
  • wcdn_has_refund
  • wcdn_has_shipping_address
  • wcdn_imprint
  • wcdn_navigation
  • wcdn_navigation_style
  • wcdn_personal_notes
  • wcdn_policies_conditions
  • wcdn_remove_payment_method_from_totals
  • wcdn_remove_semicolon_from_totals
  • wcdn_template_stylesheet
  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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 
<?php



/**
 * Exit if accessed directly
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Print class
 */
if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {

    /**
     * WooCommerce Print Delivery Notes
     * 
     * @author Tyche Softwares
     * @package WooCommerce-Delivery-Notes/Includes
     */
    class WooCommerce_Delivery_Notes_Print {

        public static $template_registrations;
        public static $template_styles;

        public $template_locations;
        public $template;

        public $api_endpoints;
        public $query_vars;

        public $order_ids;
        public $order_email;
        public $orders;

        /**
         * Constructor
         */
        public function __construct() {
            // Define the templates
            self::$template_registrations = apply_filters( 'wcdn_template_registration', array(
                apply_filters( 'wcdn_template_registration_invoice', array(
                    'type' => 'invoice',
                    'labels' => array(
                        'name' => __( 'Invoice', 'woocommerce-delivery-notes' ),
                        'name_plural' => __( 'Invoices', 'woocommerce-delivery-notes' ),
                        'print' => __( 'Print Invoice', 'woocommerce-delivery-notes' ),
                        'print_plural' => __( 'Print Invoices', 'woocommerce-delivery-notes' ),
                        'message' => __( 'Invoice created.', 'woocommerce-delivery-notes' ),
                        'message_plural' => __( 'Invoices created.', 'woocommerce-delivery-notes' ),
                        'setting' => __( 'Show "Print Invoice" button', 'woocommerce-delivery-notes' )
                    )
                ) ),
                apply_filters( 'wcdn_template_registration_delivery_note', array(
                    'type' => 'delivery-note',
                    'labels' => array(
                        'name' => __( 'Delivery Note', 'woocommerce-delivery-notes' ),
                        'name_plural' => __( 'Delivery Notes', 'woocommerce-delivery-notes' ),
                        'print' => __( 'Print Delivery Note', 'woocommerce-delivery-notes' ),
                        'print_plural' => __( 'Print Delivery Notes', 'woocommerce-delivery-notes' ),
                        'message' => __( 'Delivery Note created.', 'woocommerce-delivery-notes' ),
                        'message_plural' => __( 'Delivery Notes created.', 'woocommerce-delivery-notes' ),
                        'setting' => __( 'Show "Print Delivery Note" button', 'woocommerce-delivery-notes' )
                    )
                ) ),
                apply_filters( 'wcdn_template_registration_receipt', array(
                    'type' => 'receipt',
                    'labels' => array(
                        'name' => __( 'Receipt', 'woocommerce-delivery-notes' ),
                        'name_plural' => __( 'Receipts', 'woocommerce-delivery-notes' ),
                        'print' => __( 'Print Receipt', 'woocommerce-delivery-notes' ),
                        'print_plural' => __( 'Print Receipts', 'woocommerce-delivery-notes' ),
                        'message' => __( 'Receipt created.', 'woocommerce-delivery-notes' ),
                        'message_plural' => __( 'Receipts created.', 'woocommerce-delivery-notes' ),
                        'setting' => __( 'Show "Print Receipt" button', 'woocommerce-delivery-notes' )
                    )
                ) )
            ) );

            // Add the default template as first item after filter hooks passed
            array_unshift( self::$template_registrations, array(
                'type' => 'order',
                'labels' => array(
                    'name' => __( 'Order', 'woocommerce-delivery-notes' ),
                    'name_plural' => __( 'Orders', 'woocommerce-delivery-notes' ),
                    'print' => __( 'Print Order', 'woocommerce-delivery-notes' ),
                    'print_plural' => __( 'Print Orders', 'woocommerce-delivery-notes' ),
                    'message' => null,
                    'message_plural' => null,
                    'setting' => null
                )
            ) );

            // Template styles
            self::$template_styles = apply_filters( 'wcdn_template_styles', array() );

            // Add the default style as first item after filter hooks passed
            array_unshift( self::$template_styles, array(
                'name' => __( 'Default', 'woocommerce-delivery-notes' ),
                'type' => 'default',
                'path' => WooCommerce_Delivery_Notes::$plugin_path . 'templates/print-order/',
                'url' => WooCommerce_Delivery_Notes::$plugin_url . 'templates/print-order/'
            ) );

            // Default template
            $this->template = self::$template_registrations[0];

            // Build all template locations
            $this->template_locations = $this->build_template_locations();

            // Add the endpoint for the frontend
            $this->api_endpoints = array(
                'print-order' => get_option( 'wcdn_print_order_page_endpoint', 'print-order' )
            );

            // Insert the query vars
            $this->query_vars = array(
                'print-order-type',
                'print-order-email'
            );

            // Load the hooks
            add_action( 'init', array( $this, 'load_hooks' ) );
            add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
            add_action( 'parse_request', array( $this, 'parse_request' ) );
            add_action( 'template_redirect', array( $this, 'template_redirect_theme' ) );
            add_action( 'wp_ajax_print_order', array( $this, 'template_redirect_admin' ) );
            add_action( 'wcdn_after_items', array( $this, 'wdn_add_extra_data_after_items' ), 10 , 1 );
        }

        /**
         * Add extra data after the order items in the invoice
         * 
         * @param WC_Order $order
         */
        public function wdn_add_extra_data_after_items ( $order ) {
            
            /**
             * Local pickup plus plugin is active
             */
            if ( class_exists( "WC_Local_Pickup_Plus")  ) {
                
                $cdn_local_pickup_plugin_plugins_version = wc_local_pickup_plus()->get_version();

                if( version_compare( $cdn_local_pickup_plugin_plugins_version, '2.0.0', ">=" ) ) { 
                    $cdn_local_pickup_object    = new WC_Local_Pickup_Plus_Orders();
                    $local_pickup   = wc_local_pickup_plus();
                    $cdn_local_pickup_locations        = $cdn_local_pickup_object->get_order_pickup_data( $order );
                    $cdn_local_pickup__shipping_object = $local_pickup->get_shipping_method_instance();
                    WooCommerce_Delivery_Notes_Print::cdn_print_local_pickup_address( $cdn_local_pickup_locations, $cdn_local_pickup__shipping_object );
                }
            }
        }

        /**
         * Print the Local Pickup Address if the Pickup Plus plugin is active
         * 
         * @param array $cdn_local_pickup_locations
         * @param mixed $shipping_method
         */
        public function cdn_print_local_pickup_address( $cdn_local_pickup_locations, $shipping_method ) {

            $package_number = 1;
            $packages_count = count( $cdn_local_pickup_locations );
            foreach ( $cdn_local_pickup_locations as $pickup_meta ) : 
            ?>
                <div>
                    <?php if ( $packages_count > 1 ) : ?>
                        <h5><?php echo sprintf( is_rtl() ? '#%2$s %1$s': '%1$s #%2$s', esc_html( $shipping_method->get_method_title() ), $package_number ); ?></h5>
                    <?php endif; ?>
                    <ul>
                        <?php foreach ( $pickup_meta as $label => $value ) : ?>
                            <li>
                                <?php if ( is_rtl() ) : ?>
                                    <?php echo wp_kses_post( $value ); ?> <strong>:<?php echo esc_html( $label ); ?></strong>
                                <?php else : ?>
                                    <strong><?php echo esc_html( $label ); ?>:</strong> <?php echo wp_kses_post( $value ); ?>
                                <?php endif; ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                    <?php $package_number++; ?>
                </div>
            <?php
            endforeach; 
        }
        /**
         * Load the init hooks
         */
        public function load_hooks() {
            // Add the endpoints
            $this->add_endpoints();
        }

        /**
         * Add endpoints for query vars.
         * the endpoint is used in the front-end to
         * generate the print template and link.
         */
        public function add_endpoints() {
            foreach( $this->api_endpoints as $var ) {
                add_rewrite_endpoint( $var, EP_PAGES );
            }

            // Flush the rules when the transient is set.
            // This is important to make the endpoint work.
            if( get_transient( 'wcdn_flush_rewrite_rules' ) == true ) {
                delete_transient( 'wcdn_flush_rewrite_rules' );
                flush_rewrite_rules();
            }
        }

        /**
         * Add the query vars to the url
         */
        public function add_query_vars( $vars ) {
            foreach( $this->query_vars as $var ) {
                $vars[] = $var;
            }
            return $vars;
        }

        /**
         * Parse the query variables
         */
        public function parse_request( $wp ) {
            // Map endpoint keys to their query var keys, when another endpoint name was set.
            foreach( $this->api_endpoints as $key => $var ) {
                if( isset( $_GET[$var] ) ) {
                    // changed
                    $wdn_get_end_point_var = sanitize_text_field($_GET[$var]);
                    $wp->query_vars[$key] = $wdn_get_end_point_var;
                } elseif ( isset( $wp->query_vars[$var] ) ) {
                    $wp->query_vars[$key] = $wp->query_vars[$var];
                }
            }
        }

        /**
         * Build the template locations
         */
        public function build_template_locations() {
            $wc_template_directory = WC_TEMPLATE_PATH . 'print-order/';

            // Get the paths for custom styles
            $settings_type = get_option( 'wcdn_template_style' );
            $settings_path = null;
            $settings_url = null;
            if( isset( $settings_type ) && $settings_type !== 'default' ) {
                foreach( self::$template_styles as $template_style ) {
                    if( $settings_type === $template_style['type'] ) {
                        $settings_path = $template_style['path'];
                        $settings_url = $template_style['url'];
                        break;
                    }
                }
            }

            // Build the locations
            $locations = array(
                'child_theme' => array(
                    'path' => trailingslashit( get_stylesheet_directory() ) . $wc_template_directory,
                    'url' => trailingslashit( get_stylesheet_directory_uri() ) . $wc_template_directory
                ),

                'theme' => array(
                    'path' => trailingslashit( get_template_directory() ) . $wc_template_directory,
                    'url' => trailingslashit( get_template_directory_uri() ) . $wc_template_directory
                ),

                'settings' => array(
                    'path' => $settings_path,
                    'url' => $settings_url
                ),

                'plugin' => array(
                    'path' => self::$template_styles[0]['path'],
                    'url' => self::$template_styles[0]['url']
                )
            );

            return $locations;
        }

        /**
         * Template handling in the front-end
         */
        public function template_redirect_theme() {
            global $wp;
            // Check the page url and display the template when on my-account page
            if( !empty( $wp->query_vars['print-order'] ) && is_account_page() ) {
                $type = !empty( $wp->query_vars['print-order-type'] ) ? $wp->query_vars['print-order-type'] : null;
                $email = !empty( $wp->query_vars['print-order-email'] ) ? $wp->query_vars['print-order-email'] : null;
                $this->generate_template( $wp->query_vars['print-order'], $type, $email );
                exit;
            }
        }

        /**
         * Template handling in the back-end
         */
        public function template_redirect_admin() {
            // Let the backend only access the page
            // changed
            if( is_admin() && current_user_can( 'edit_shop_orders' ) && !empty( $_REQUEST['print-order'] ) && !empty( $_REQUEST['action'] ) ) {
                $type = !empty( $_REQUEST['print-order-type'] ) ? sanitize_text_field ( $_REQUEST['print-order-type'] ) : null;
                $email = !empty( $_REQUEST['print-order-email'] ) ? sanitize_email ( $_REQUEST['print-order-email'] ) : null;
                // changed
                $wdn_get_print_order = sanitize_text_field ( $_GET['print-order'] );
                $this->generate_template( $wdn_get_print_order, $type, $email );
                exit;
            }
            exit;
        }

        /**
         * Generate the template
         */
        public function generate_template( $order_ids, $template_type = 'order', $order_email = null ) {
            global $post, $wp;

            // Explode the ids when needed
            if( !is_array( $order_ids ) ) {
                $this->order_ids = array_filter( explode('-', $order_ids ) );
            }

            // Set the current template
            foreach( self::$template_registrations as $template_registration ) {
                if( $template_type == $template_registration['type'] ) {
                    $this->template = $template_registration;
                    break;
                }
            }

            // Set the email
            if( empty( $order_email ) ) {
                $this->order_email = null;
            } else {
                $this->order_email = strtolower( $order_email );
            }

            // Create the orders and check permissions
            $populated = $this->populate_orders();

            // Only continue if the orders are populated
            if( !$populated ) {
                die();
            }

            // Load the print template html
            $location = $this->get_template_file_location( 'print-order.php' );
            wc_get_template( 'print-order.php', null, $location, $location );
            exit;
        }

        /**
         * Find the location of a template file
         */
        public function get_template_file_location( $name, $url_mode = false ) {
            $found = '';
            foreach( $this->template_locations as $template_location ) {
                if( isset( $template_location['path'] ) && file_exists( trailingslashit( $template_location['path'] ) . $name ) ) {
                    if( $url_mode ) {
                        $found = $template_location['url'];
                    } else {
                        $found = $template_location['path'];
                    }
                    break;
                }
            }
            return $found;
        }

        /**
         * Get print page url
         */
        public function get_print_page_url( $order_ids, $template_type = 'order', $order_email = null, $permalink = false ) {
            // Explode the ids when needed
            if( !is_array( $order_ids ) ) {
                $order_ids = array_filter( explode( '-', $order_ids ) );
            }

            // Build the args
            $args = array();

            // Set the template type arg
            foreach( self::$template_registrations as $template_registration ) {
                if( $template_type == $template_registration['type'] && $template_type != 'order' ) {
                    $args = wp_parse_args( array( 'print-order-type' => $template_type ), $args );
                    break;
                }
            }

            // Set the email arg
            if( !empty( $order_email ) ) {
                $args = wp_parse_args( array( 'print-order-email' => $order_email ), $args );
            }

            // Generate the url
            $order_ids_slug = implode( '-', $order_ids );

            // Create another url depending on where the user prints. This
            // prevents some issues with ssl when the my-account page is
            // secured with ssl but the admin isn't.
            if( is_admin() && current_user_can( 'edit_shop_orders' ) && $permalink == false ) {
                // For the admin we use the ajax.php for better security
                $args = wp_parse_args( array( 'action' => 'print_order' ), $args );
                $base_url = admin_url( 'admin-ajax.php' );
                $endpoint = 'print-order';

                // Add the order ids and create the url
                $url = add_query_arg( $endpoint, $order_ids_slug, $base_url );
            } else {
                // For the theme
                $base_url = wc_get_page_permalink( 'myaccount' );
                $endpoint = $this->api_endpoints['print-order'];

                // Add the order ids and create the url
                if( get_option( 'permalink_structure' ) ) {
                    $url = trailingslashit( trailingslashit( $base_url ) . $endpoint . '/' . $order_ids_slug );
                } else {
                    $url = add_query_arg( $endpoint, $order_ids_slug, $base_url );
                }
            }

            // Add all other args
            $url = add_query_arg( $args, $url );

            return esc_url( $url );
        }

        /**
         * Create the orders list and check the permissions
         */
        private function populate_orders() {
            $this->orders = array();

            // Get the orders
            $args = array(
                'posts_per_page' => -1,
                'post_type' => 'shop_order',
                'post_status' => 'any',
                'post__in' => $this->order_ids,
                'orderby' => 'post__in'
            );
            $posts = get_posts( $args );

            // All orders should exist
            if( count( $posts ) !== count( $this->order_ids ) ) {
                $this->orders = null;
                return false;
            }

            // Check permissons of the user to determine
            // if the orders should be populated.
            foreach( $posts as $post ) {
                $order = new WC_Order( $post->ID );

                $wdn_order_id =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_id() : $order->id;
                // Logged in users
                if( is_user_logged_in() && ( !current_user_can( 'edit_shop_orders' ) && !current_user_can( 'view_order', $wdn_order_id ) ) ) {
                    $this->orders = null;
                    return false;
                }

                $wdn_order_billing_id  =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_billing_email() : $order->billing_email;


                // An email is required for not logged in users
                if( !is_user_logged_in() && ( empty( $this->order_email ) || strtolower( $wdn_order_billing_id ) != $this->order_email ) ) {
                    $this->orders = null;
                    return false;
                }

                // Save the order to get it without an additional database call
                $this->orders[$post->ID] = $order;
            }
            return true;
        }

        /**
         * Get the order
         */
        public function get_order( $order_id ) {
            if( isset( $this->orders[$order_id] ) ) {
                return $this->orders[$order_id];
            }
            return;
        }

        /**
         * Get the order invoice number
         */
        public function get_order_invoice_number( $order_id ) {
            $invoice_count = intval( get_option( 'wcdn_invoice_number_count', 1 ) );
            $invoice_prefix = get_option( 'wcdn_invoice_number_prefix' );
            $invoice_suffix = get_option( 'wcdn_invoice_number_suffix' );

            // Add the invoice number to the order when it doesn't yet exist
            $meta_key = '_wcdn_invoice_number';
            $meta_added = add_post_meta( $order_id, $meta_key, $invoice_prefix . $invoice_count . $invoice_suffix, true );

            // Update the total count
            if( $meta_added ) {
                update_option( 'wcdn_invoice_number_count', $invoice_count + 1  );
            }

            // Get the invoice number
            return apply_filters( 'wcdn_order_invoice_number', get_post_meta( $order_id, $meta_key, true ) );
        }

        /**
         * Get the order invoice date
         */
        public function get_order_invoice_date( $order_id ) {
            // Add the invoice date to the order when it doesn't yet exist
            $meta_key = '_wcdn_invoice_date';
            $meta_added = add_post_meta( $order_id, $meta_key, time(), true );

            // Get the invoice date
            $meta_date = get_post_meta( $order_id, $meta_key, true );
            $formatted_date = date_i18n( get_option('date_format'), $meta_date );
            return apply_filters( 'wcdn_order_invoice_date', $formatted_date, $meta_date );
        }

    }

}
?>
WooCommerce Print Invoice & Delivery Note API documentation generated by ApiGen