ClassesBigCommerceCartBuy Now

Buy_Now

Handles requests from the Buy Now button for products.

Extends the Add_To_Cart class to reuse functionality for adding products to the cart, with special handling for redirecting the user to the checkout page.

Constants

ConstantVisibilityTypeValue
ACTIONpublic’buy’

Methods

handle_response

Handles the response after a product has been successfully added to the cart and the purchase process begins.

protected handle_response(\BigCommerce\Api\v3\Model\Cart $response, \BigCommerce\Cart\Cart $cart, int $post_id, int $product_id, int $variant_id): void

Redirects the user to the appropriate checkout page, either embedded or external.

Parameters:

ParameterTypeDescription
$response\BigCommerce\Api\v3\Model\CartThe response from the API after adding the product to the cart.
$cart\BigCommerce\Cart\CartThe Cart object containing cart data and methods.
$post_idintThe ID of the product being purchased.
$product_idintThe ID of the product being purchased.
$variant_idintThe ID of the selected variant.

handle_exception

Handles exceptions that occur during the purchase process.

protected handle_exception(\BigCommerce\Api\v3\ApiException $e, \BigCommerce\Cart\Cart $cart): void

Displays a user-friendly error message based on the API exception.

Parameters:

ParameterTypeDescription
$e\BigCommerce\Api\v3\ApiExceptionThe API exception that occurred during the purchase process.
$cart\BigCommerce\Cart\CartThe Cart object containing cart data.

Inherited methods

handle_request

Handles the request to add a product to the cart.

public handle_request(int $post_id, \BigCommerce\Api\v3\Api\CartApi $cart_api): void

Parameters:

ParameterTypeDescription
$post_idintThe ID of the product post.
$cart_api\BigCommerce\Api\v3\Api\CartApiThe CartApi instance to interact with the cart API.

handle_response

Handles the response from the API after attempting to add a product to the cart.

protected handle_response(\BigCommerce\Api\v3\Model\Cart|null $response, \BigCommerce\Cart\Cart $cart, int $post_id, int $product_id, int $variant_id): void

Parameters:

ParameterTypeDescription
$response\BigCommerce\Api\v3\Model\Cart|nullThe response from the API.
$cart\BigCommerce\Cart\CartThe cart instance.
$post_idintThe ID of the product post.
$product_idintThe ID of the product in BigCommerce.
$variant_idintThe variant ID for the product.

handle_exception

Handles exceptions thrown during the cart API process.

protected handle_exception(\BigCommerce\Api\v3\ApiException $e, \BigCommerce\Cart\Cart $cart): void

Parameters:

ParameterTypeDescription
$e\BigCommerce\Api\v3\ApiExceptionThe exception thrown during the API request.
$cart\BigCommerce\Cart\CartThe cart instance.

validate_request

Validates the request to ensure the product exists and is published.

protected validate_request(int $post_id, array $submission): bool

Parameters:

ParameterTypeDescription
$post_idintThe ID of the product post.
$submissionarrayThe form submission data.

Return Value:

True if the request is valid, false otherwise.


get_variant_id

Gets the product variant ID from the form submission.

protected get_variant_id(\BigCommerce\Post_Types\Product\Product $product, array $submission): int

Parameters:

ParameterTypeDescription
$product\BigCommerce\Post_Types\Product\ProductThe product object.
$submissionarrayThe form submission data.

Return Value:

The variant ID.



Automatically generated on 2025-01-21