复制WooCommerce订单发货地址到剪贴板上

<?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 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. 
Author: Damien Carbery
Version: 0.4
*/


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();
?>
<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' );
	
	// When the link is clicked then 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 then 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 then 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);
		});
	});
});

</script>
<?php
	}
}

$CopyOrderAddressToClipboard = new CopyOrderAddressToClipboard();
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享