Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active November 24, 2023 21:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/625366a1fe75bf46fa22a04b5d7b47e0 to your computer and use it in GitHub Desktop.
Save damiencarbery/625366a1fe75bf46fa22a04b5d7b47e0 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Copy WooCommerce order shipping address to clipboard
Plugin URI: https://www.damiencarbery.com/2022/11/copy-woocommerce-order-shipping-address-to-clipboard/
Description: Copy the shipping (or billing) address, email, phone number, full name or order number to the clipboard for pasting in another application. A WooCommerce Community member asked how to add a button to the order admin page to copy the shipping address to the clipboard. Asked on <a href="https://www.facebook.com/groups/advanced.woocommerce/permalink/6233848903296143/">WooCommerce Community Facebook group</a>.
Author: Damien Carbery
Version: 0.5
*/
class CopyOrderAddressToClipboard {
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
// Write the order address and JavaScript code to the page.
add_action( 'woocommerce_admin_order_data_after_shipping_address', array( $this, 'add_copy_data_and_icon' ) );
}
public function add_copy_data_and_icon( $order ) {
// Retrieve the shipping address.
$formattedAddress = $order->get_formatted_shipping_address();
// If shipping empty (e.g. only virtual products in the order) get billing address.
if ( empty( $formattedAddress ) ) {
$formattedAddress = $order->get_formatted_billing_address();
}
$emailAddress = $order->get_billing_email();
$phoneNumber = $order->get_billing_phone();
$orderNumber = $order->get_order_number();
$fullName = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
?>
<style>
.dcwd-copyToClipboard.copied:after { content: 'Copied'; color: green; padding-left: 0.5em; font-size: 70%; font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; }
</style>
<script>
jQuery(document).ready(function( $ ) {
// Add the link markup before the Edit Address icon in the Shipping section.
$( '<a href="#" class="dcwd-copyToClipboard dcwd-copyAddress dashicons dashicons-external" title="Copy address to clipboard"></a>' ).insertBefore( '.order_data_column:nth-child(3) a.edit_address' );
$( '<a href="#" class="dcwd-copyToClipboard dcwd-copyEmail dashicons dashicons-external" title="Copy email address to clipboard"></a>' ).insertAfter( '.order_data_column:nth-child(2) .address p:nth-child(2) a' );
$( '<a href="#" class="dcwd-copyToClipboard dcwd-copyPhone dashicons dashicons-external" title="Copy phone number to clipboard"></a>' ).insertAfter( '.order_data_column:nth-child(2) .address p:nth-child(3) a' );
$('<a href="#" class="dcwd-copyToClipboard dcwd-copyFullName dashicons dashicons-external" title="Copy full name to clipboard"></a>' ).insertBefore( '.order_data_column:nth-child(2) .address br:first-child' );
// Use .append (which has opposite syntax to .insertAfter) to put the icon beside the .woocommerce-order-data__heading content.
$( '.woocommerce-order-data__heading' ).append( '<a href="#" class="dcwd-copyToClipboard dcwd-copyOrderNum dashicons dashicons-external" title="Copy order number to clipboard"></a>' );
// When the link is clicked copy the shipping name and address to the clipboard.
$( '.dcwd-copyAddress' ).click(function() {
formattedAddress = '<?php echo $formattedAddress; ?>';
formattedAddress = formattedAddress.replace(/<br\/>/g, "\n");
navigator.clipboard.writeText(formattedAddress).then(function() {
// Add the 'copied' class and remove it 4 seconds later.
$( '.dcwd-copyAddress' ).addClass( 'copied' );
setTimeout(() => { $( '.dcwd-copyAddress' ).removeClass( 'copied' );; }, "4000")
}, function(err) {
// TODO: Consider some indication of the error.
console.error('Async: Could not copy text: ', err);
});
});
// When the link is clicked copy the email address to the clipboard.
$( '.dcwd-copyEmail' ).click(function() {
emailAddress = '<?php echo $emailAddress; ?>';
navigator.clipboard.writeText(emailAddress).then(function() {
// Add the 'copied' class and remove it 4 seconds later.
$( '.dcwd-copyEmail' ).addClass( 'copied' );
setTimeout(() => { $( '.dcwd-copyEmail' ).removeClass( 'copied' );; }, "4000")
}, function(err) {
// TODO: Consider some indication of the error.
console.error('Async: Could not copy text: ', err);
});
});
// When the link is clicked copy the phone number to the clipboard.
$( '.dcwd-copyPhone' ).click(function() {
phoneNumber = '<?php echo $phoneNumber; ?>';
navigator.clipboard.writeText(phoneNumber).then(function() {
// Add the 'copied' class and remove it 4 seconds later.
$( '.dcwd-copyPhone' ).addClass( 'copied' );
setTimeout(() => { $( '.dcwd-copyPhone' ).removeClass( 'copied' );; }, "4000")
}, function(err) {
// TODO: Consider some indication of the error.
console.error('Async: Could not copy text: ', err);
});
});
// When the link is clicked copy the order number to the clipboard.
$( '.dcwd-copyOrderNum' ).click(function() {
orderNumber = '<?php echo $orderNumber; ?>';
navigator.clipboard.writeText(orderNumber).then(function() {
// Add the 'copied' class and remove it 4 seconds later.
$( '.dcwd-copyOrderNum' ).addClass( 'copied' );
setTimeout(() => { $( '.dcwd-copyOrderNum' ).removeClass( 'copied' );; }, "4000")
}, function(err) {
// TODO: Consider some indication of the error.
console.error('Async: Could not copy text: ', err);
});
});
// When the link is clicked copy the full name to the clipboard.
$( '.dcwd-copyFullName' ).click(function() {
fullName = '<?php echo $fullName; ?>';
navigator.clipboard.writeText(fullName).then(function() {
// Add the 'copied' class and remove it 4 seconds later.
$( '.dcwd-copyFullName' ).addClass( 'copied' );
setTimeout(() => { $( '.dcwd-copyFullName' ).removeClass( 'copied' );; }, "4000")
}, function(err) {
// TODO: Consider some indication of the error.
console.error('Async: Could not copy text: ', err);
});
});
});
</script>
<?php
}
}
$CopyOrderAddressToClipboard = new CopyOrderAddressToClipboard();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment