Display an order barcode in a custom admin screen

Display an order barcode in a custom administration screen using the order passed to a WooCommerce hook. This example adds a barcode below the order details on the edit screen.

Note:

This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.

Before you start

↑ Back to top

Install and activate WooCommerce and Order Barcodes, enable barcodes, and use a test store and test order. Add PHP through a site-specific plugin or a child theme. The extension already displays a barcode in an order panel; this example shows how to add it in another location.

Add the example

↑ Back to top

Add the following code to your site-specific plugin or child theme. If the PHP file already starts with an opening PHP tag, omit the opening tag from this example. WooCommerce passes the current order to the hook. The capability check limits the example to users who can manage WooCommerce, and the function check avoids an error if Order Barcodes is inactive.

<?php
/**
 * Show a barcode in the order administration screen.
 *
 * @package Example
 */

add_action(
	'woocommerce_admin_order_data_after_order_details',
	'example_show_order_barcode'
);

/**
 * Show the barcode for the current order to store managers.
 *
 * @param WC_Order $order The current order.
 * @return void
 */
function example_show_order_barcode( $order ) {
	if ( ! $order instanceof WC_Order ) {
		return;
	}
	// manage_woocommerce is a WooCommerce capability.
	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		return;
	}
	if ( ! function_exists( 'WC_Order_Barcodes' ) ) {
		return;
	}

	$barcode = WC_Order_Barcodes()->display_barcode(
		$order->get_id(),
		true
	);
	if ( '' !== $barcode ) {
		echo '<div class="example-order-barcode">';
		echo wp_kses_post( $barcode );
		echo '</div>';
	}
}

The second argument, true, requests the image version. The method returns markup or a message when the order has no barcode. The example filters the returned markup before output. The extension’s default output uses an image URL that contains the barcode value: treat that URL and the barcode as sensitive, avoid shared caches, and don’t expose them on a public page.

Check the result

↑ Back to top

Open a test order in the dashboard as an administrator or shop manager. A barcode should appear below the order details in addition to the extension’s normal barcode panel. Confirm the displayed barcode belongs to the test order and that a visitor without store-management access can’t use the admin screen.

Troubleshooting

↑ Back to top

If you see “Barcode does not exist!”, open the order in the dashboard and check its Order Barcode panel. The extension can generate a barcode for an older order when that panel loads. If nothing appears, confirm the extensions are active and your account can manage WooCommerce. Test your site-specific customization on a staging store before using it on a live store.

This example is for the order administration screen. If you adapt it to another page, determine the order in that context and verify that the current viewer is allowed to see that specific order before displaying it. The example does not implement customer or guest order authorization.

Questions and support

↑ Back to top

Something missing from this documentation? Still have questions and need assistance?

  • If you have a question about a specific extension or theme you’d like to purchase, contact us to get answers.
  • If you already purchased this product and need some assistance, get in touch with a Happiness Engineer via our support page and select this product’s name from the Product dropdown.

Related Products

WooCommerce Subscriptions is a WooCommerce extension that lets customers subscribe to your products or...

Power your membership association, online magazine, elearning sites, and more with access control to...

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.