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

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

/**
 * Writepanel class
 */
if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {

    /**
     * WooCommerce Print Delivery Notes
     * 
     * @author Tyche Softwares
     * @package WooCommerce-Delivery-Notes/Admin/Edit-Order
     */
    class WooCommerce_Delivery_Notes_Writepanel {

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

        /**
         * Load the admin hooks
         */
        public function load_admin_hooks() {
            // Hooks
            add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_listing_actions' ) );
            add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
            add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) );

            add_action( 'add_meta_boxes_shop_order', array( $this, 'add_box' ) );

            add_action( 'admin_footer-edit.php', array( $this, 'add_bulk_actions' ) );
            add_action( 'load-edit.php', array( $this, 'load_bulk_actions' ) );
            add_action( 'admin_notices', array( $this, 'confirm_bulk_actions' ) );
        }

        /**
         * Add the styles
         */
        public function add_styles() {
            if( $this->is_order_edit_page() || $this->is_order_post_page() ) {
                wp_enqueue_style('thickbox');
                wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css' );
            }
        }

        /**
         * Add the scripts
         */
        public function add_scripts() {
            if( $this->is_order_edit_page() || $this->is_order_post_page() ) {
                wp_enqueue_script( 'thickbox' );
                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-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'woocommerce-delivery-notes-print-link' ) );
            }
        }

        /**
         * Is order edit page
         */
        public function is_order_edit_page() {
            global $typenow, $pagenow;
            if( $typenow == 'shop_order' && $pagenow == 'edit.php' ) {
                return true;
            } else {
                return false;
            }
        }

        /**
         * Is order edit page
         */
        public function is_order_post_page() {
            global $typenow, $pagenow;
            if( $typenow == 'shop_order' && ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) ) {
                return true;
            } else {
                return false;
            }
        }

        /**
         * Add print actions to the orders listing
         */
        public function add_listing_actions( $order ) {

            $wdn_order_id =  ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">="  ) ) ? $order->get_id() : $order->id;
            ?>
            <?php foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) : ?>
                <?php if( get_option( 'wcdn_template_type_' . $template_registration['type'] ) == 'yes' && $template_registration['type'] !== 'order' ) : ?>

                    <a href="<?php echo wcdn_get_print_link( $wdn_order_id, $template_registration['type'] ); ?>" class="button tips print-preview-button <?php echo $template_registration['type']; ?>" target="_blank" alt="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>" data-tip="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>">
                        <?php _e( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ); ?>
                    </a>

                <?php endif; ?>
            <?php endforeach; ?>

            <span class="print-preview-loading spinner"></span>
            <?php
        }

        /**
         * Add bulk actions with javascript to the dropdown.
         * This is not so pretty but WordPress does not yet
         * offer any better solution. The JS code is inline
         * because we can't determine the page without
         * checking the post_type.
         * https://core.trac.wordpress.org/ticket/16031
         */
        public function add_bulk_actions() {
            if( $this->is_order_edit_page() ) : ?>
                <script type="text/javascript">
                    jQuery(document).ready(function($) {
                        <?php foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) : ?>
                            <?php if( get_option( 'wcdn_template_type_' . $template_registration['type'] ) == 'yes' && $template_registration['type'] !== 'order' ) : ?>

                                $('<option>').val('wcdn_print_<?php echo $template_registration['type']; ?>').attr('title', '<?php echo $template_registration['type']; ?>').text('<?php echo esc_js( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action"]');
                                $('<option>').val('wcdn_print_<?php echo $template_registration['type']; ?>').attr('title', '<?php echo $template_registration['type']; ?>').text('<?php echo esc_js( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action2"]');

                            <?php endif; ?>
                        <?php endforeach; ?>
                    });
                </script>
            <?php endif;
        }

        /**
         * Add bulk print actions to the orders listing
         */
        public function load_bulk_actions() {
            if( $this->is_order_edit_page() ) {
                // get the action staht should be started
                $wp_list_table = _get_list_table('WP_Posts_List_Table');
                $action = $wp_list_table->current_action();

                // stop if there are no post ids
                if( !isset( $_REQUEST['post'] ) ) {
                    return;
                }

                // only for specified actions
                foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) {
                    if( $action == 'wcdn_print_' . $template_registration['type'] ) {
                        $template_type = $template_registration['type'];
                        $report_action = 'printed_' . $template_registration['type'];
                        break;
                    }
                }
                if( !isset( $report_action ) ) {
                    return;
                }

                // security check
                check_admin_referer('bulk-posts');

                // get referrer
                if( !wp_get_referer() ) {
                    return;
                }

                // filter the referer args
                $referer_args = array();
                parse_str( parse_url( wp_get_referer(), PHP_URL_QUERY ), $referer_args );

                // set the basic args for the sendback
                $args = array(
                    'post_type' => $referer_args['post_type']
                );
                if( isset( $referer_args['post_status'] ) ) {
                    $args = wp_parse_args( array( 'post_status' => $referer_args['post_status'] ), $args );
                }
                if( isset( $referer_args['paged'] ) ) {
                    $args = wp_parse_args( array( 'paged' => $referer_args['paged'] ), $args );
                }
                if( isset( $referer_args['orderby'] ) ) {
                    $args = wp_parse_args( array( 'orderby' => $referer_args['orderby'] ), $args );
                }
                if( isset( $referer_args['order'] ) ) {
                    $args = wp_parse_args( array( 'orderby' => $referer_args['order'] ), $args );
                }

                // do the action
                $post_ids = array_map( 'absint', (array) $_REQUEST['post'] );
                $total = count( $post_ids );
                $url = wcdn_get_print_link( $post_ids , $template_type );

                // generate more args and the sendback string
                $args = wp_parse_args( array( $report_action => true, 'total' => $total, 'print_url' => urlencode( $url ) ), $args );
                $sendback = add_query_arg( $args, '' );
                wp_redirect( $sendback );
                exit;
            }
        }

        /**
         * Show confirmation message that orders are printed
         */
        public function confirm_bulk_actions() {
            if( $this->is_order_edit_page() ) {
                foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) {
                    if( isset( $_REQUEST['printed_' . $template_registration['type']] ) ) {
                        // use singular or plural form
                        $total = isset( $_REQUEST['total'] ) ? absint( $_REQUEST['total'] ) : 0;
                        if( $total <= 1 ) {
                            $message = $template_registration['labels']['message'];
                        } else {
                            $message = $template_registration['labels']['message_plural'];
                        }
                        // changed
                        ?>

                        <div id="woocommerce-delivery-notes-bulk-print-message" class="updated">
                            <p><?php _e( $message, 'woocommerce-delivery-notes' ); ?> <a href="<?php echo urldecode( esc_url_raw ( $_REQUEST['print_url'] ) ); ?>" target="_blank" class="print-preview-button" id="woocommerce-delivery-notes-bulk-print-button"><?php _e( 'Print now', 'woocommerce-delivery-notes' ) ?></a> <span class="print-preview-loading spinner"></span></p>
                        </div>

                        <?php
                        break;
                    }
                }
            }
        }

        /**
         * Add the meta box on the single order page
         */
        public function add_box() {
            add_meta_box( 'woocommerce-delivery-notes-box', __( 'Order Printing', 'woocommerce-delivery-notes' ), array( $this, 'create_box_content' ), 'shop_order', 'side', 'low' );
        }

        /**
         * Create the meta box content on the single order page
         */
        public function create_box_content() {
            global $post_id, $wcdn;
            ?>
            <div class="print-actions">
                <?php foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) : ?>
                    <?php if( get_option( 'wcdn_template_type_' . $template_registration['type'] ) == 'yes' && $template_registration['type'] !== 'order' ) : ?>

                        <a href="<?php echo wcdn_get_print_link( $post_id, $template_registration['type'] ); ?>" class="button print-preview-button <?php echo $template_registration['type']; ?>" target="_blank" alt="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>"><?php _e( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ); ?></a>

                    <?php endif; ?>
                <?php endforeach; ?>
                <span class="print-preview-loading spinner"></span>
            </div>
            <?php
            $create_invoice_number = get_option( 'wcdn_create_invoice_number' );
            $has_invoice_number = get_post_meta( $post_id, '_wcdn_invoice_number', true );
            if( !empty( $create_invoice_number ) && $create_invoice_number == 'yes' && $has_invoice_number ) :
                $invoice_number = wcdn_get_order_invoice_number( $post_id );
                $invoice_date = wcdn_get_order_invoice_date( $post_id ); ?>

                <ul class="print-info">
                    <li><strong><?php _e( 'Invoice number: ', 'woocommerce-delivery-notes' ); ?></strong> <?php echo $invoice_number; ?></li>
                    <li><strong><?php _e( 'Invoice date: ', 'woocommerce-delivery-notes' ); ?></strong> <?php echo $invoice_date; ?></li>
                </ul>

            <?php endif; ?>
            <?php
        }

    }

}

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