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
    },
  ])
ParameterTypeRequiredDescription
variantIdStringYesThe unique ID for each variant of the product
quantityNumberYesThe number of items that needs to be added to cart
sellingPlanIdStringNounique Id of variant incase of subscription available for the product which needs to be passed as an parameter.
attributesArray of JSONNoKey value pairs that can be passed as parameter

Structure of Response

StatusResponse
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

  1. Use for single-item purchases: Perfect for fast checkout flows or limited-time offers.
  2. Confirm stock availability: Ensure the product is in stock before triggering buyNow.
  3. Skip cart logic: This action automatically bypasses cart addition.
  4. Handle async flows: Await the Promise or use .then() for proper flow control.
  5. Track conversions: Pair with analytics or tracking to measure one-click purchases.

Caveats

  1. Variant required: You must provide a valid variantId; the action won’t work with just a product ID.
  2. Bypasses cart: This skips the cart and takes the user straight to checkout.
  3. App-only: Works only inside the Superfans mobile app environment.\
  4. Limited customization: The checkout process follows Shopify’s standard checkout flow.
  5. Asynchronous behavior: Handle responses properly to avoid race conditions.