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
  3. Checkout Form Customization - Add custom fields, checkboxes, or content
For a complete reference of available hooks, see the Cart Hooks and Checkout Hooks documentation.

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]
' );
?>

Checkout Form Customization

Add custom fields, checkboxes, or content to checkout forms using the render_block filter. This example walks you through adding a terms checkbox with server-side validation.
1

Add custom content before the submit button

Use the render_block filter to inject HTML before any checkout block. Target the block by checking $block['blockName'].
add_filter( 'render_block', function( $block_content, $block ) {
    if ( 'surecart/submit' !== $block['blockName'] ) {
        return $block_content;
    }

    $checkbox = '<div class="my-terms-checkbox" style="margin-bottom: 1em;">
        <label style="display: flex; align-items: start; gap: 0.5em; cursor: pointer;">
            <input type="checkbox" name="accept_terms" value="yes" required />
            <span>I agree to the <a href="/terms" target="_blank">terms and conditions</a></span>
        </label>
    </div>';

    return $checkbox . $block_content;
}, 10, 2 );
2

Add server-side validation

Validate custom fields using the surecart/checkout/validate filter. Return errors to prevent checkout submission.
add_filter( 'surecart/checkout/validate', function( $errors, $args, $request ) {
    // phpcs:ignore WordPress.Security.NonceVerification.Missing
    $accept_terms = isset( $_POST['accept_terms'] ) ? sanitize_text_field( wp_unslash( $_POST['accept_terms'] ) ) : '';

    if ( 'yes' !== $accept_terms ) {
        $errors->add( 'terms_required', 'You must accept the terms and conditions.' );
    }

    return $errors;
}, 10, 3 );
See Checkout Hooks for more validation examples.

Checkout Block Names

Block NameDescription
surecart/submitSubmit/Pay button
surecart/emailEmail field
surecart/nameFull name field
surecart/first-nameFirst name field
surecart/last-nameLast name field
surecart/phonePhone number field
surecart/addressAddress fields
surecart/paymentPayment method selection
surecart/couponCoupon code field
surecart/line-itemsOrder line items
surecart/totalsOrder totals summary