Last active
September 19, 2019 06:58
-
-
Save xadapter/90f6b1f63b67fd4ef596b35adc940314 to your computer and use it in GitHub Desktop.
Snippet to add Company Logo in top of UPS label in ZPL format. WooCommerce UPS Shipping Plugin with Print Label by PluginHive - https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Snippet to add Company Logo in top of UPS label in zpl format. | |
* Created at : 01 Aug 2018 | |
* Updated at : 01 Aug 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
* Gist Link : https://gist.github.com/xadapter/90f6b1f63b67fd4ef596b35adc940314 | |
*/ | |
if( ! function_exists('ph_generate_document_file') ) { | |
function ph_generate_document_file($content, $file_name){ | |
$app_logo_file = "wp-content/uploads/aps-logo.zpl"; // Path of zpl company logo, relative to wordpress directory | |
$uploads_dir_info = wp_upload_dir(); | |
$file_name_with_path = $uploads_dir_info['basedir'].$file_name; | |
$app_logo_data = file_get_contents( ABSPATH.$app_logo_file); | |
if( ! empty($app_logo_data) ) { | |
file_put_contents( $file_name_with_path, $app_logo_data ); | |
$content = str_replace( "^XA^LRN^MNY^MFN,N^LH10,12^MCY^PW812^CI27", "LRN^MNY^MFN,N^LH10,420^MCY^PW812^CI27", $content ); | |
} | |
file_put_contents( $file_name_with_path, $content, FILE_APPEND ); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename='.basename($file_name)); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: ' . filesize($file_name_with_path)); | |
readfile($file_name_with_path); | |
unlink($file_name_with_path); | |
return; | |
} | |
} | |
add_action( 'woocommerce_after_register_post_type', function() { | |
if( is_admin() && ! empty($_GET['wf_ups_print_label']) ) { | |
$query_string = explode('|', base64_decode($_GET['wf_ups_print_label'])); | |
$shipmentId = $query_string[0]; | |
$post_id = $query_string[1]; | |
$label_extn_code = $query_string[2]; | |
$index = $query_string[3]; | |
$tracking_number = $query_string[4]; | |
$label_meta_name='ups_label_details_array'; | |
if(isset($query_string[4])){ | |
$return = $query_string[4]; | |
if($return=='return'){ | |
$label_meta_name='ups_return_label_details_array'; | |
} | |
} | |
$ups_label_details_array = get_post_meta( $post_id, $label_meta_name, true ); | |
$graphic_image = $ups_label_details_array[$shipmentId][$index]["GraphicImage"]; | |
if("ZPL" == $label_extn_code ) { | |
$binary_label = base64_decode(chunk_split($graphic_image)); | |
// By default zpl code returned by UPS has ^POI command, which will invert the label because | |
// of some reason. Removing it so that label will not be inverted. | |
$zpl_label_inverted = str_replace( "^POI", "", $binary_label); | |
$file_name = 'UPS-Shipping-Label-With-Company'.$post_id.'-'.$tracking_number.'.zpl'; | |
ph_generate_document_file($zpl_label_inverted, $file_name); | |
exit; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment