Buy Now
Purchase a product directly from PDP Page.
Description
The buyNow action immediately starts the checkout flow for a single product variant, bypassing the cart.
It enables a fast, one-click purchase experience by directly taking the user to the checkout page with the selected item preloaded.
Function Signature
VajroSDK.actions.buyNow([
{
variantId: "12345",
quantity: 1,
sellingPlanId: "1234234342" // Optional
attributes: [{ "key:"custom_key","value":"custom_value"}] // Optional
},
])| Parameter | Type | Required | Description |
|---|---|---|---|
| variantId | String | Yes | The unique ID for each variant of the product |
| quantity | Number | Yes | The number of items that needs to be added to cart |
| sellingPlanId | String | No | unique Id of variant incase of subscription available for the product which needs to be passed as an parameter. |
| attributes | Array of JSON | No | Key value pairs that can be passed as parameter |
Structure of Response
| Status | Response |
|---|---|
| Success | { "status": "success", "message": "Checkout opened with the the provided line items" } |
| Invalid Params | { "status": "error", "errorId": 400, "errorHandle": "invalid-params", "message": "Invalid params" } |
| Unexpected Error | { "status": "error", "errorId": 500, "errorHandle": "unknown-error", "message": "Something unexpected happened" } |
Example Usage
const variant_to_be_added=VajroSDK.variables.product.selectedVariant.id;
function addItemToCart(variantId, quantity) {
VajroSDK.actions.buyNow([
{
variantId: variant_to_be_added,
quantity: 3
}
])
}
document.getElementById("openCartButton").addEventListener('click',()=>{
// Example usage
addItemToCart(variant_to_be_added, 1); // Replace "12345" with the actual variant ID
})<div class="cart-container" id="ct">
<button class="open-cart-btn" id="openCartButton">Buy Now</button>
</div>.cart-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.open-cart-btn {
background-color: #28a745;
color: #fff;
border: none;
padding: 15px 30px;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
.open-cart-btn:hover {
background-color: #218838;
}Best Practices
- Use for single-item purchases: Perfect for fast checkout flows or limited-time offers.
- Confirm stock availability: Ensure the product is in stock before triggering buyNow.
- Skip cart logic: This action automatically bypasses cart addition.
- Handle async flows: Await the Promise or use .then() for proper flow control.
- Track conversions: Pair with analytics or tracking to measure one-click purchases.
Caveats
- Variant required: You must provide a valid variantId; the action won’t work with just a product ID.
- Bypasses cart: This skips the cart and takes the user straight to checkout.
- App-only: Works only inside the Superfans mobile app environment.\
- Limited customization: The checkout process follows Shopify’s standard checkout flow.
- Asynchronous behavior: Handle responses properly to avoid race conditions.
Updated about 1 month ago