Skip to main content
SureCart provides multiple ways to add items to the cart programmatically:
  1. URL Parameters - Redirect users to checkout with pre-filled line items
  2. Shortcodes - Use built-in shortcodes for add-to-cart buttons
Looking to customize cart or checkout behavior? See the Cart Hooks and Checkout Hooks reference for available actions and filters.

URL Parameters

The most straightforward way to add items to cart is by redirecting users to the checkout page with line_items query parameters.

Basic Example

<?php
$checkout_url = add_query_arg(
    [
        'line_items' => [
            [
                'price_id' => 'price_xxxxxxxxxxxxx', // Your SureCart price ID
                'quantity' => 1,
            ],
        ],
    ],
    \SureCart::pages()->url( 'checkout' )
);
?>

<a href="<?php echo esc_url( $checkout_url ); ?>">
    Add to Cart
</a>

Multiple Items

<?php
$checkout_url = add_query_arg(
    [
        'line_items' => [
            [
                'price_id' => 'price_product_one',
                'quantity' => 1,
            ],
            [
                'price_id' => 'price_product_two',
                'quantity' => 2,
            ],
        ],
    ],
    \SureCart::pages()->url( 'checkout' )
);
?>

With Coupon Code

<?php
$checkout_url = add_query_arg(
    [
        'line_items' => [
            [
                'price_id' => 'price_xxxxxxxxxxxxx',
                'quantity' => 1,
            ],
        ],
        'coupon' => 'SAVE10', // Promotion code
    ],
    \SureCart::pages()->url( 'checkout' )
);
?>

With Product Variant

<?php
$checkout_url = add_query_arg(
    [
        'line_items' => [
            [
                'price_id'   => 'price_xxxxxxxxxxxxx',
                'quantity'   => 1,
                'variant_id' => 'variant_xxxxxxxxxxxxx', // Optional variant
            ],
        ],
    ],
    \SureCart::pages()->url( 'checkout' )
);
?>

Shortcodes

SureCart provides built-in shortcodes for adding products to cart.

Add to Cart Button

[sc_product_cart_button id="prod_xxxxxxxxxxxxx" text="Add To Cart"]
Parameters
Shortcode Parameters

Buy Button with Line Items

[sc_buy_button]
    [sc_line_item price_id="price_xxxxxxxxxxxxx" quantity="1"]
[/sc_buy_button]

Multiple Line Items

[sc_buy_button]
    [sc_line_item price_id="price_product_one" quantity="1"]
    [sc_line_item price_id="price_product_two" quantity="2"]
[/sc_buy_button]

Using Shortcodes in PHP

You can render shortcodes programmatically in PHP using WordPress’s do_shortcode function.
<?php
echo do_shortcode( '[sc_product_cart_button id="prod_xxxxxxxxxxxxx" text="Buy Now"]' );
?>
For buy buttons with multiple line items:
<?php
echo do_shortcode( '
    [sc_buy_button]
        [sc_line_item price_id="price_product_one" quantity="1"]
        [sc_line_item price_id="price_product_two" quantity="2"]
    [/sc_buy_button]
' );
?>