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
if ( !defined( 'ABSPATH' ) ) {
exit;
}
if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {
class WooCommerce_Delivery_Notes_Theme {
public function __construct() {
add_action( 'wp_loaded', array( $this, 'load_hooks' ) );
}
public function load_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 );
}
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' ) );
}
}
public function create_print_button_account_page( $actions, $order ) {
if( get_option( 'wcdn_print_button_on_my_account_page' ) == 'yes' ) {
$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;
}
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;
if( get_option( 'wcdn_print_button_on_view_order_page' ) == 'yes' ) {
$print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ) );
if( $this->is_woocommerce_tracking_page() ) {
$wdn_order_email = sanitize_email( $_REQUEST['order_email'] ) ;
$print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_email );
}
if( is_order_received_page() && !is_user_logged_in() ) {
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
}
}
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
<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;
}
}
}
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;
}
public function is_woocommerce_tracking_page() {
return ( is_page( wc_get_page_id( 'order_tracking' ) ) && isset( $_REQUEST['order_email'] ) ) ? true : false;
}
}
}
?>