File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -21,9 +21,35 @@ class Receipt {
21
21
22
22
public function __construct ( int $ order_id ) {
23
23
$ 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
24
47
}
25
48
26
49
50
+ /**
51
+ * @return void
52
+ */
27
53
public function get_template (): void {
28
54
try {
29
55
$ order = wc_get_order ( $ this ->order_id );
Original file line number Diff line number Diff line change 136
136
margin-bottom: 3px;
137
137
}
138
138
</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
+ ?>
139
146
</head>
140
147
<body <?php body_class (); ?> >
141
148
<div class="sales-receipt">
You can’t perform that action at this time.
0 commit comments