Skip to content

Commit e47537f

Browse files
committed
fix receipt print regression
1 parent f79704b commit e47537f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

includes/Templates/Receipt.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,35 @@ class Receipt {
2121

2222
public function __construct( int $order_id ) {
2323
$this->order_id = $order_id;
24+
25+
add_action( 'woocommerce_pos_receipt_head' , array( $this, 'receipt_head' ) );
26+
}
27+
28+
/**
29+
* Adds a script to the head of the WordPress template when the
30+
* 'woocommerce_pos_receipt_head' action is triggered. The script listens for
31+
* a 'message' event with a specific action ('wcpos-print-receipt') and, upon
32+
* receiving such an event, triggers the browser's print functionality.
33+
*
34+
* Usage: Call `do_action( 'woocommerce_pos_receipt_head' );` at the desired
35+
* location in your template file to include the script.
36+
*/
37+
public function receipt_head(): void {
38+
?>
39+
<script>
40+
window.addEventListener("message", ({data}) => {
41+
if (data.action && data.action === "wcpos-print-receipt") {
42+
window.print();
43+
}
44+
}, false);
45+
</script>
46+
<?php
2447
}
2548

2649

50+
/**
51+
* @return void
52+
*/
2753
public function get_template(): void {
2854
try {
2955
$order = wc_get_order( $this->order_id );

templates/receipt.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@
136136
margin-bottom: 3px;
137137
}
138138
</style>
139+
<?php
140+
/**
141+
* IMPORTANT!
142+
* This hook adds the javascript to print the receipt.
143+
*/
144+
do_action( 'woocommerce_pos_receipt_head' );
145+
?>
139146
</head>
140147
<body <?php body_class(); ?>>
141148
<div class="sales-receipt">

0 commit comments

Comments
 (0)