Skip to content

Instantly share code, notes, and snippets.

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/4ea4bac44dc495de9555eee6db94e54b to your computer and use it in GitHub Desktop.
Save webdados/4ea4bac44dc495de9555eee6db94e54b to your computer and use it in GitHub Desktop.
Update order objects tracking information on DPD Portugal for WooCommerce
<?php
/**
* Run action each time a object tracking information is updated (not recommended unless you need to diferentiate each object)
*
* @param int $order_id The order ID
* @param string $tracking_number The object tracking number
* @param array $tracking_information An array will all objects traking information (the key is the tracking number)
* @param bool $programmatically If the udpate was triggered programmatically
**/
add_action( 'woo_dpd_portugal_updated_object_tracking_information', function( $order_id, $tracking_number, $tracking_information, $programmatically ) {
if ( $programmatically ) {
echo '<h1>Updated object ' . $tracking_number . ' (order ' . $order_id . ') tracking information</h1>';
echo '<pre>';
var_dump( $tracking_information );
echo '</pre>';
if ( $tracking_information['final'] ) {
// Do something for final statuses (like stopping the scheduled task)
echo '<p>Object is in final status</p>';
}
if ( $tracking_information['delivered'] ) {
// Do something for delivred status (like changinfg the order status - if not already changed, but its better to do it on woo_dpd_portugal_updated_order_tracking_information if all are final)
echo '<p>Object is delivered</p>';
}
if ( $tracking_information['updated'] ) {
// Do something for an object that was updated on DPD
echo '<p>Object was updated</p>';
}
echo '<hr/>';
}
}, 10, 4 );
/**
* Run action each time an order tracking information is updated
*
* @param int $order_id The order ID
* @param array $tracking_information An array will all objects traking information (the key is the tracking number)
* @param bool $all_final If all objects are in a final status
* @param bool $all_delivered If all objects are delivered
* @param bool $any_updated If any object was updated on DPD
* @param bool $programmatically If the udpate was triggered programmatically
**/
add_action( 'woo_dpd_portugal_updated_order_tracking_information', function( $order_id, $tracking_information, $all_final, $all_delivered, $any_updated, $programmatically ) {
if ( $programmatically ) {
if ( $any_updated ) {
// Do something if any object was updated on DPD (like notifying the shop owner or customer)
echo '<h1>Updated order ' . $order_id . ' tracking information</h1>';
echo '<pre>';
var_dump( $tracking_information );
echo '</pre>';
if ( $all_final ) {
// Do something for final statuses (like stopping the scheduled task)
echo '<p>All order objects are in final status</p>';
}
if ( $all_delivered ) {
// Do something for delivred status (like changinfg the order status - if not already changed)
echo '<p>All order objects are delivered</p>';
}
} else {
// No object has been updated, don't do anything, or maybe check how long are you running the schedule without any updates and cancel it after some days to avoid running it forever
}
}
}, 10, 6 );
/**
* Update object tracking information
* This can be scheduled for example on the `woo_dpd_portugal_label_issued` action
* If scheduled, it's important to set it to a long interval and make sure it is unscheduled when all objects are final or after some days even if not all objects are final
*/
do_action( 'woo_dpd_portugal_update_order_tracking_information', 18052 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment