Link to add products to a cart

At the MODX Meetup today I was challenged to make it possible to link a cart in 30 minutes… :white_check_mark:

Add this snippet and visit the page with a products query parameter containing the product IDs to add.

Might wrap this up in an installable module at a later time.

$path = $modx->getOption('commerce.core_path', null, MODX_CORE_PATH . 'components/commerce/') . 'model/commerce/';
$params = ['mode' => $modx->getOption('commerce.mode')];
/** @var Commerce|null $commerce */
$commerce = $modx->getService('commerce', 'Commerce', $path, $params);
if (!($commerce instanceof Commerce)) {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Could not load Commerce service in commerce.get_cart snippet.');
    return 'Could not load Commerce. Please try again later.';

if ($commerce->isDisabled()) {
    return $commerce->adapter->lexicon('commerce.mode.disabled.message');

$order = comOrder::loadUserOrder($commerce);

$productIDs = array_key_exists('products', $_GET) ? (string)$_GET['products'] : '';
$productIDs = array_map('intval', explode(',', $productIDs));

foreach ($productIDs as $pid) {
    $product = $modx->getObject('comProduct', [
        'id' => $pid,
        'removed' => false,
    if ($product instanceof comProduct) {
        /** @var comOrderItem $item */
        $item = $modx->newObject('comOrderItem');



// Redirect to the cart
$cart = $modx->makeUrl($modx->getOption('commerce.cart_resource'));