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 
<?php

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

/**
 * Frontend Theme class
 */
if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {

    /**
     * WooCommerce Print Delivery Notes
     * 
     * @author Tyche Softwares
     * @package WooCommerce-Delivery-Notes/Theme
     */
    class WooCommerce_Delivery_Notes_Theme {

        /**
         * Constructor
         */
        public function __construct() {
            // Load the hooks
            add_action( 'wp_loaded', array( $this, 'load_hooks' ) );
        }

        /**
         * Load the hooks at the end when
         * the theme and plugins are ready.
         */
        public function load_hooks() {
            // hooks
            add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'create_print_button_account_page' ), 10, 2 );
            add_action( 'woocommerce_view_order', array( $this, 'create_print_button_order_page' ) );
            add_action( 'woocommerce_thankyou', array( $this, 'create_print_button_order_page' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );
            add_action( 'woocommerce_email_after_order_table', array( $this, 'add_email_print_url' ), 100, 3 );
        }

        /**
         * Add the scripts
         */
        public function add_scripts() {
            if ( is_account_page() || is_order_received_page() || $this->is_woocommerce_tracking_page() ) {
                wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ) );
                wp_enqueue_script( 'woocommerce-delivery-notes-theme', WooCommerce_Delivery_Notes::$plugin_url . 'js/theme.js', array( 'jquery', 'woocommerce-delivery-notes-print-link' ) );
            }
        }

        /**
         * Create a print button for the 'My Account' page
         */
        public function create_print_button_account_page( $actions, $order ) {
            if( get_option( 'wcdn_print_button_on_my_account_page' ) == 'yes' ) {
                // Add the print button
                $wdn_order_id =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_id() : $order->id;
                $actions['print'] = array(
                    'url'  => wcdn_get_print_link( $wdn_order_id, $this->get_template_type( $order ) ),
                    'name' => __( 'Print', 'woocommerce-delivery-notes' )
                );
            }
            return $actions;
        }

        /**
         * Create a print button for the 'View Order' page
         */
        public function create_print_button_order_page( $order_id ) {
            $order = new WC_Order( $order_id );
            $wdn_order_billing_id  =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_billing_email() : $order->billing_email;
            // Output the button only when the option is enabled
            if( get_option( 'wcdn_print_button_on_view_order_page' ) == 'yes' ) {
                // Default button for all pages and logged in users
                $print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ) );

                // Pass the email to the url for the tracking
                // and thank you page. This allows to view the
                // print page without logging in.
                if( $this->is_woocommerce_tracking_page() ) {
                    //changed
                    $wdn_order_email = sanitize_email( $_REQUEST['order_email'] ) ;
                    $print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_email );
                }

                // Thank you page
                if( is_order_received_page() && !is_user_logged_in() ) {
                    // Don't output the butten when there is no email
                    if( !$wdn_order_billing_id ) {
                        return;
                    }
                    $print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_billing_id );
                }

                ?>
                <p class="order-print">
                    <a href="<?php echo $print_url; ?>" class="button print"><?php _e( 'Print', 'woocommerce-delivery-notes' ); ?></a>
                </p>
                <?php
            }
        }

        /**
         * Add a print url to the emails that are sent to the customer
         */
        public function add_email_print_url( $order, $sent_to_admin = true, $plain_text = false ) {
            if( get_option( 'wcdn_email_print_link' ) == 'yes' ) {
                $wdn_order_billing_id  =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_billing_email() : $order->billing_email;
                if( $wdn_order_billing_id && !$sent_to_admin ) {
                    $wdn_order_id =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_id() : $order->id;

                    $url = wcdn_get_print_link( $wdn_order_id, $this->get_template_type( $order ), $wdn_order_billing_id, true );

                    if( $plain_text ) :
echo __( 'Print your order', 'woocommerce-delivery-notes' ) . "\n\n";

echo $url . "\n";

echo "\n****************************************************\n\n";
                    else : ?>
                    <?php // changed ?>
                        <p><strong><?php _e( 'Print:', 'woocommerce-delivery-notes' ); ?></strong> <a href="<?php echo esc_url_raw( $url ); ?>"><?php _e( 'Open print view in browser', 'woocommerce-delivery-notes' ); ?></a></p>
                    <?php endif;
                }
            }
        }

        /**
         * Get the print button template type depnding on order status
         */
        public function get_template_type( $order ) {

            $wdn_order_status =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_status() : $order->status;

            if( $wdn_order_status == 'completed' ) {
                $type = apply_filters( 'wcdn_theme_print_button_template_type_complete_status', 'invoice' );
            } else {
                $type = apply_filters( 'wcdn_theme_print_button_template_type', 'order' );
            }
            return $type;
        }

        /**
         * Is WooCommerce 'Order Tracking' page
         */
        public function is_woocommerce_tracking_page() {
            return ( is_page( wc_get_page_id( 'order_tracking' ) ) && isset( $_REQUEST['order_email'] ) ) ? true : false;
        }

    }

}

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