WooCommerce: “Share Cart” Link Generator

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.

If logged in as admin and when on the (legacy) Cart page, you will see a “Generate Cart Link” button in the WP admin bar. On click, you will get the link to recreate this exact same cart, with products, quantities, and even applied coupons if any.

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:

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: “Share Cart” Link Generator

  1. 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.

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *