Cart
Introduction
The cart uses the Session
storage to keep track of the items in the cart. It also uses functions from all the layouts to get the current data of the cart.
Not all functions exist in each layout. They are added as needed depending on the layout.
Functions Used
isJson()
isJson()
This PHP code takes a single argument, a string of JSON data. The function first decodes the JSON data using the "json_decode" function and checks if it is a valid JSON object or array. It returns true if the input string is a valid JSON object or array and false otherwise.
onBuySubmit()
onBuySubmit()
This PHP code handles a form submission (from the Buy Button) and initiates a Stripe Checkout session. The function starts by grabbing the shop settings, including the currency and Stripe API key. It then gets the input data from an October CMS AJAX request and sets various non-dynamic variables such as product name, ID, price, and description.
Next, it declares arrays for dynamic variables such as pricing, pre-product data, metadata, and option pricing. The function populates these arrays based on the product details received.
After preparing the metadata, the function prepares the pricing for Stripe checkout and sets the shipping methods for checkout. Finally, it initiates a checkout session and redirects the user to the URL of that session.
onAddToCart()
onAddToCart()
The onAddToCart()
function first retrieves the shop settings and generates success and cancel URLs for Stripe. It then sets the Stripe API key from the shop settings and retrieves input data from an October CMS AJAX request.
Next, it sets several non-dynamic variables for the product being added to the cart, including the product name, ID, price, description, and image. It also declares several arrays for dynamic variables, including pricing, pre-product data, metadata, and option pricing. The function then adds dynamic content to each of these arrays using a series of foreach
loops.
onRemoveFromCart()
onRemoveFromCart()
The onRemoveFromCart()
function simply retrieves the shopping cart data from the user's session, removes the item at the specified index, and updates the session data. It then retrieves and returns the updated cart items.
onLoad()
onLoad()
This PHP code does the following:
Retrieves input data from an October CMS AJAX request.
Sets non-dynamic variables such as product name, ID, price, description, and image.
Declares arrays for dynamic variables, including pricing, pre-product data, metadata, and option pricing.
Adds dynamic content to their respective arrays based on the product details retrieved in step 1.
Calculates the option pricing and product total based on the values in the arrays.
Sets the values of the arrays and other variables as properties of the current object.
Retrieves the cart items from the current session and sets them as a property of the current object.
onStart()
onStart()
This PHP code assigns the value of a session variable called 'cart_items' to a property of the current object referred to by $this
. The session variable value is retrieved using the static method Session::get()
.
onCheckout()
onCheckout()
This PHP code initiates a checkout session by performing various actions. It retrieves the shop settings, currency, success and cancel URLs. It sets the Stripe key from the settings and gets cart data from the session. It then sets the line items, including product data and metadata, and checks if items are out of stock. The function then sets the shipping options and initiates the checkout session with the line items, shipping options, and success/cancel URLs. Finally, it returns a redirect to the checkout session URL.
Stripe Webhooks
In order to receive orders in the backend, you'll need to setup your webhooks. You can learn how to do that below:
Last updated