Skip to content

Instantly share code, notes, and snippets.

@webdados
Created November 19, 2020 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdados/8ad281235c5e45cb8c25a4befb642f43 to your computer and use it in GitHub Desktop.
Save webdados/8ad281235c5e45cb8c25a4befb642f43 to your computer and use it in GitHub Desktop.
Create a DPD Portugal return label programmatically, right after the shipping label, with the "DPD Portugal for WooCommerce" WordPress plugin
<?php
//You need this plugin: https://www.webdados.pt/wordpress/plugins/dpd-portugal-para-woocommerce-wordpress/
//Monitor the "issue label" result (success) and do whatver you want with it - Needs to be set before running "woo_dpd_portugal_issue_label"
add_action( 'woo_dpd_portugal_label_issued', function( $order_id ) {
$order = wc_get_order( $order_id );
echo 'Label issued for order '.$order_id.' on '.$order->get_meta( '_woo_dpd_portugal_label_issued' ).'<br/>';
echo 'Tracking number(s): '.implode( ' / ', $order->get_meta( '_woo_dpd_portugal_tracking_number' ) ).'<br/>';
echo 'Label PDF URL: '.$order->get_meta( '_woo_dpd_portugal_pdf_url' ).'<br/>';
}, 10, 1 );
//Monitor the "issue label" result (error) and do whatver you want with it - Needs to be set before running "woo_dpd_portugal_issue_label"
add_action( 'woo_dpd_portugal_label_issued_error', function( $order_id, $error_message ) {
echo 'Label NOT issued for order '.$order_id.' - Error: '.$error_message;
//wp_mail( 'email@domain', 'Label NOT issued for order '.$order_id, 'Error: '.$error_message );
}, 10, 2 );
//Set the shipping account
add_filter( 'woo_dpd_portugal_shipment_data', function( $shipment_data, $order_id ) {
$shipment_data['service'] = 'dpd18';
return $shipment_data;
}, 10, 2 );
//Monitor the "issue return" result (success) and do whatver you want with it - Needs to be set before running "woo_dpd_portugal_issue_return"
add_action( 'woo_dpd_portugal_return_issued', function( $order_id ) {
$order = wc_get_order( $order_id );
echo 'Return issued for order '.$order_id.' on '.$order->get_meta( '_woo_dpd_portugal_return_issued' ).'<br/>';
echo 'Tracking number(s): '.implode( ' / ', $order->get_meta( '_woo_dpd_portugal_return_tracking_number' ) ).'<br/>';
echo 'Label PDF URL: '.$order->get_meta( '_woo_dpd_portugal_return_pdf_url' ).'<br/>';
}, 10, 1 );
//Monitor the "issue return" result (error) and do whatver you want with it - Needs to be set before running "woo_dpd_portugal_issue_return"
add_action( 'woo_dpd_portugal_return_issued_error', function( $order_id, $error_message ) {
echo 'Return NOT issued for order '.$order_id.' - Error: '.$error_message;
//wp_mail( 'email@domain', 'Return NOT issued for order '.$order_id, 'Error: '.$error_message );
}, 10, 2 );
//Set the return account
add_filter( 'woo_dpd_portugal_return_data', function( $return_data, $order_id ) {
$return_data['service'] = 'dpd18';
//Maybe you should also postpone the return date 2 or 3 days because the customer can't possibly return the same day it's shipped
$date = new DateTime( $return_data['date'] );
$date->add( new DateInterval( 'P3D' ) );
$return_data['date'] = $date->format('Y-m-d');
return $return_data;
}, 10, 2 );
//DO IT - Issue return after issuing label - Needs to be set before running "woo_dpd_portugal_issue_label"
add_action( 'woo_dpd_portugal_label_issued', function( $order_id ) {
do_action( 'woo_dpd_portugal_issue_return', $order_id );
}, 11, 1);
//DO IT - Issue label - Replace 16276 with your order id
do_action( 'woo_dpd_portugal_issue_label', 16496 );
//More information: https://www.webdados.pt/wordpress/plugins/dpd-portugal-para-woocommerce-wordpress/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment