autora lab

Cart Share developer hooks

Cart Share has five filters for the link, the message, the share buttons and the settings, one action for when it opens one of its own links, and a way to add your own share app.

Add these snippets to a small custom plugin or to your child theme's functions.php.

The link format

Cart Share makes two kinds of link. On WooCommerce 10 and newer, most carts get WooCommerce's own checkout link. A cart with two or more coupons, or with an option chosen from “Any”, gets Cart Share's own link to the cart page, because WooCommerce's link can't carry those. On older WooCommerce versions, and when Link destination is Cart, every cart gets Cart Share's link.

WooCommerce's checkout link:
{site}/checkout-link/?products=ID:QTY,ID:QTY&coupon=CODE&utm_source=cart-share&utm_medium=APP

Cart Share's link:
{cart page}?products=ID:QTY:OPTIONS,ID:QTY&coupon=CODE,CODE&utm_source=cart-share&utm_medium=APP
  • For a variable product, the ID is the variation's ID.
  • OPTIONS is there only for a variation with an “Any” option. It holds the chosen values as URL-safe base64 of a JSON object, such as {"attribute_pa_size":"m"}.
  • APP is the app's key (whatsapp, email and so on), copy-link, share-menu or link-generator. A Link Generator link with a campaign name also has utm_campaign.
  • On stores with plain permalinks, WooCommerce's checkout link starts with ?checkout-link=true instead of /checkout-link/.

Filters

Filter and argumentsChanges
alcartshare_base_url
string $base_url, string $format
The address a link starts with. $format is checkout-link for WooCommerce's checkout link, or cart for Cart Share's link.
alcartshare_url
string $share_url, array $context
The finished link. $context holds products (the products value), coupons, format, medium and campaign.
alcartshare_text
string $share_text
The message sent with the link. By default, the Share message setting: “Here's my cart from” followed by your store name.
alcartshare_social_links
array $links, string $share_url, string $share_text
The share buttons in the popup, keyed by app, each with a url, label and icon. Each app gets its own link, tagged with the app. $share_url is the link for Copy link.
alcartshare_settings
array $settings
The fields on WooCommerce > Settings > Cart Share.

This example changes the share message, whatever the Share message setting says:

add_filter( 'alcartshare_text', function () {
	return 'Here is my cart from Good Ideas Studio:';
} );

Action: alcartshare_processed

Runs after Cart Share opens one of its own links, before it sends the person on to checkout or the cart. It gets the link's lines as array $items, each with product_id, quantity and options. Links that use WooCommerce's own checkout link are opened by WooCommerce, so this action doesn't run for them.

Add your own share app

Write a class that implements AlCartShare\SocialMedia\ShareProvider, then register it with ShareProviderFactory::register(). Apps you add are always on and have no checkbox in the settings. The key you register (line here) becomes utm_medium on the app's links, and the label shows in the order note.

<?php
// class-my-line-share.php

use AlCartShare\SocialMedia\ShareProvider;

class My_Line_Share implements ShareProvider {
	public function generate_share_url( string $url, string $text ): string {
		return 'https://social-plugins.line.me/lineit/share?url=' . rawurlencode( $url );
	}

	public function get_label(): string {
		return 'LINE';
	}

	public function get_icon(): string {
		return '<svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">...</svg>';
	}

	public function get_name(): string {
		return 'line';
	}
}
add_action( 'init', function () {
	if ( ! interface_exists( \AlCartShare\SocialMedia\ShareProvider::class ) ) {
		return; // Cart Share isn't active.
	}

	require_once __DIR__ . '/class-my-line-share.php';
	\AlCartShare\SocialMedia\ShareProviderFactory::register( 'line', My_Line_Share::class );
} );

get_icon() returns inline SVG markup. Use currentColor so the icon matches the other buttons.