Sharing a WooCommerce cart with specific products, quantities, and optional coupon codes can be a powerful tool for support, marketing, or testing.
This snippet adds a convenient “Generate Cart Link” option right in the WordPress admin bar for admins and shop managers, letting you instantly create a URL representing the current cart contents.
A single click builds a custom checkout URL including product IDs, quantities, and applied coupons. The link is then copied directly to your clipboard for easy sharing.
This approach is for the classic/legacy WooCommerce Cart page, making it simple to replicate or share carts without manually crafting URLs. It’s a neat little feature that boosts productivity and streamlines workflows — all with just a few lines of code.

PHP Snippet: Generate Shareable WooCommerce Cart Link from Admin Bar
This PHP snippet integrates with the WooCommerce Store API to generate shareable checkout URLs based on the current cart contents.
It adds a new “Generate Cart Link” item to the WordPress admin bar, visible only to logged-in users with the proper permissions.
When clicked, a JavaScript handler sends an AJAX request to a custom handler (generate_cart_link) registered via wp_ajax. The handler accesses the current cart using WooCommerce’s cart object, iterates through cart items to build a product ID and quantity list, and collects applied coupons.
It then constructs a checkout URL with these parameters using the new sharable checkout URLs feature. The generated URL is returned as a JSON response. Finally, the JavaScript copies the link to the clipboard and alerts the user. This clean, minimal code leverages WooCommerce’s Store API for easy and secure cart sharing.
/**
* @snippet Generate Cart Share Link @ WooCommerce Cart Page
* @tutorial https://businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 10
* @community https://businessbloomer.com/club/
*/
add_action( 'admin_bar_menu', 'bbloomer_add_generate_cart_link_to_admin_bar', 999 );
function bbloomer_add_generate_cart_link_to_admin_bar( $wp_admin_bar ) {
if ( ! is_user_logged_in() || ! current_user_can( 'manage_woocommerce' ) || ! is_cart() ) {
return;
}
$wp_admin_bar->add_node([
'id' => 'generate_cart_link',
'title' => 'Generate Cart Link',
'href' => '#',
'meta' => [ 'title' => 'Generate Cart Link' ]
]);
wc_enqueue_js( "
jQuery('#wp-admin-bar-generate_cart_link a').on('click', function(e) {
e.preventDefault();
jQuery.post('" . admin_url( 'admin-ajax.php' ) . "', {
action: 'generate_cart_link'
}, function(response) {
if (response.success) {
navigator.clipboard.writeText(response.data).then(function() {
alert('Cart link copied to clipboard:\\n' + response.data);
}, function(err) {
alert('Link: ' + response.data + '\\n(Copy failed: ' + err + ')');
});
} else {
alert('Error: ' + response.data);
}
});
});
");
}
add_action( 'wp_ajax_generate_cart_link', 'bbloomer_ajax_generate_cart_link' );
function bbloomer_ajax_generate_cart_link() {
if ( ! is_user_logged_in() || ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'Unauthorized' );
}
$cart = WC()->cart;
if ( ! $cart || $cart->is_empty() ) {
wp_send_json_error( 'Cart is empty' );
}
$products = [];
foreach ( $cart->get_cart() as $item ) {
$product_id = $item['product_id'];
$qty = $item['quantity'];
$products[] = "{$product_id}:{$qty}";
}
$coupon_codes = $cart->get_applied_coupons();
$products_param = implode( ',', $products );
$coupon_param = $coupon_codes ? '&coupon=' . urlencode( implode( ',', $coupon_codes ) ) : '';
$url = home_url( "/checkout-link/?products={$products_param}{$coupon_param}" );
wp_send_json_success( $url );
}
If you click on the button while logged in as an admin, you will see this notification, and the “magic link” will be copied in your clipboard:










Thank you for this. We were creating a ecommerce website for a friend and this kind of stumped us and ChatGPT. After running around for ‘some time’ with AI suggested fixes (which did not work!), a good old web search gave us your page. Can’t thank you enough.
Awesome!