I am trying to print labels directly from the zen cart admin when the print button is clicked. I am using qz print tray 2.0.7 and can get everything working correctly on their demo and feel that I have the js written almost correctly to get this to work. I need help with the last bit.
This is on a page to print shipping labels (a commercial mod), and it has most of the logic written above the html section. The trouble I am having is getting a variable from the logic passed to the javascript to complete the URL string. When the label is created a png file is also created, stored in the admin images folder and referenced in a DB table. I cannot seem to pass the variable for the file, either directly referenced or through the DB label.
My coding chops are so rusty it's not even funny. I could really use some help or pointers to get this working. When I print the image file name to an alert box it's empty, and the qz log console keeps saying it can't connect to the endpoint. So, it appears to me that the variable is not being recognized. I have spent many hours over the past few days trying many different things and come up empty.
Here's the code that creates the image file of the label, it's in a switch statement:
PHP Code:
if (in_array(MODULE_SHIPPING_FEDEX_WEBSERVICES_LABELS_IMAGETYPE, array(
'PNG',
'PDF'
))) {
$tracking_number_image = $tracking_number . '.' . strtolower(MODULE_SHIPPING_FEDEX_WEBSERVICES_LABELS_IMAGETYPE);
$fp = fopen(DIR_FS_ADMIN . 'images/fedex/' . $tracking_number_image, 'wb');
fwrite($fp, $response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image); //Create PNG or PDF file
fclose($fp);
}
$sql_data_array = array(
'orders_id' => $order_id,
'labels' => $tracking_number_image
);
zen_db_perform(DB_PREFIX . 'fedex_labels', $sql_data_array);
my javascript, which is loaded after the <body> tag:
Code:
<script type="text/javascript">
function connectAndPrint() {
// our promise chain
connect().then(function() {
return print();
}).then(function() {
success(); // exceptions get thrown all the way up the stack
}).catch(fail); // so one catch is often enough for all promises
// NOTE: If a function returns a promise, you don't need to wrap it in a fuction call.
// The following is perfectly valid:
//
// connect().then(print).then(success).catch(fail);
//
// Important, in this case success is NOT a promise, so it should stay wrapped in a function() to avoid confusion
}
// connection wrapper
// - allows active and inactive connections to resolve regardless
// - try to connect once before firing the mimetype launcher
// - if connection fails, catch the reject, fire the mimetype launcher
// - after mimetype launcher is fired, try to connect 3 more times
function connect() {
return new RSVP.Promise(function(resolve, reject) {
if (qz.websocket.isActive()) { // if already active, resolve immediately
resolve();
} else {
// try to connect once before firing the mimetype launcher
qz.websocket.connect().then(resolve, function reject() {
// if a connect was not succesful, launch the mimetime, try 3 more times
window.location.assign("qz:launch");
qz.websocket.connect({ retries: 2, delay: 1 }).then(resolve, reject);
});
}
});
}
// print logic
function print() {
var printer = "Zebra ZP 450-200dpi";
var options = { size: { width: 4, height: 6}, units: "in", density: "600", language: "ZPL" };
var config = qz.configs.create(printer, options);
var data = [{ type: 'png', format: 'image', flavor: 'file', data: 'images/fedex/<?php echo $fp; ?>' }];
// return the promise so we can chain more .then().then().catch(), etc.
return qz.print(config, data);
}
// notify successful print
function success() {
alert("Success");
}
// exception catch-all
function fail(e) {
alert("Error: " + e);
}
</script>
So, as you can see in the js, I've got this line:
Code:
data: 'images/fedex/<?php echo $fp; ?>'
Which references the $fp variable from the above PHP section. I have tried do this, too, but still nothing:
Code:
<script type="text/javascript">
function connectAndPrint() {
<?php $oID = zen_db_prepare_input($_GET['oID']);
$fedex_labels = $db->Execute('SELECT * FROM ' . DB_PREFIX . 'fedex_labels WHERE labels IS NOT NULL AND orders_id = ' . $oID . ' ORDER BY labels ASC;');
if ($fedex_labels->RecordCount() > 0) {
while(!$fedex_labels->EOF) {
$printLabel = print_var($fedex_labels);
$fedex_labels->MoveNext();
}}
?>
// our promise chain
connect().then(function() {
.......
I've tried so many other things that I forget them now, kind of a blur hahaha. I am hoping it is something pretty straightforward, because I really need to get this to work. In a very busy warehouse, having to click through 3 times to print a label (as this mod is currently set up by default), it's a big time waster. That's why I am trying to get qz tray to work, so when the print button is clicked it just prints the label to the thermal printer.
I have no problem paying to get this done, but thought I would throw it out here first to see if someone had a suggestion first (mostly because I really want to know what I've done wrong and learn/grow from it, not because of the cost).
Bookmarks