-
Notifications
You must be signed in to change notification settings - Fork 1
PHP utility functions
This page serves as a comprehensive guide to the utility functions contained within includes -> class-rcn-utility.php
. Here, you'll find a detailed listing of all utility functions, complete with explanations of their usage, underlying logic, and implementation details.
As our PHP utility functions continue to expand, this file will evolve accordingly, ensuring you have access to the latest tools and functionalities.
Function Description: Check Product in WooCommerce Cart
The function accepts a product/variation ID and loops through the WooCommerce cart to determine if the product/variation exists. It returns a true status if the product/variation is found; otherwise, it returns a false status.
Logic
Although WooCommerce provides a built-in function find_product_in_cart($product_cart_id)
to check for product existence in the cart, it's limited to simple products only. To address this limitation, we've developed a custom function. For more details, refer to WooCommerce: Check if Product ID is in the Cart.
Usage
This method accepts a product/variation ID and returns an array containing status
and message
elements.
- If the function successfully retrieves the product cart status, the
status
is true. - If the function fails to retrieve the product cart status, the
status
in the array is false. Themessage
element contains the corresponding error message.
Refer to the source code for further insights.
Function Description: Add Product/Variation to Cart with Specified Quantity
The function accepts a product/variation ID and a specified quantity, adding them to the cart. It updates the quantity if the product is already in the cart. However, if the same quantity is already in the cart, the function does nothing.
Logic
This function utilizes the WooCommerce built-in add_to_cart
function. Its construction aims to handle success and error messages more efficiently, especially for scenarios where the product is already in the cart, out of stock, or unavailable for addition.
Usage
This method accepts a product/variation ID and a quantity (default 1), returning an array with status
and message
elements.
- If the function successfully adds the product to the cart, the
status
is true. - If the function fails to add the product to the cart, the
status
in the array is false. Themessage
element contains the corresponding error message.
For further insights, refer to the source code.
Function Description: Retrieve Product Quantity Information
The function retrieves quantity information based on the provided parameters, returning either the WooCommerce cart quantity or the stock quantity depending on the specified user flag.
Logic
This function fetches the product quantity in the cart or the stock quantity based on the flag provided. It was developed alongside the WooCommerce built-in get_stock_quantity
function to enhance error handling capabilities and provide cart quantity retrieval.
Usage
This method accepts a product ID, a flag (stock
or cart
), and a variation ID (for variable products), returning an array with status
, stock_quantity
or cart_quantity
, and a message
element.
Please Note: For variable products, the function requires the variation ID and returns the stock quantity of the variation, not the parent product. If you need to retrieve the quantity of the parent product, we recommend using the WooCommerce built-in get_stock_quantity
function.
For further insights, refer to the source code.
Function Description: Remove Product from Cart
This function removes a product from the WooCommerce cart based on the provided product ID and optional variation ID.
Note: While the variation ID is not mandatory for product removal, it's required for variable products. This ensures the product's presence in the cart, verified by the is_product_in_cart
function.
The is_product_in_cart
function, in turn, requires the variation ID, adding an extra layer of confirmation before proceeding with product removal.
Logic
Our custom function utilizes the WooCommerce built-in remove_cart_item
function to remove the product from the cart. It enhances error handling and passes the cart item key to remove_cart_item
for execution.
Usage
This method accepts a product ID and a variation ID (for variable products) and removes the product from the cart.
The function returns an array with status
and message
elements.
- If the removal is successful, it returns a
true
status and a corresponding success message. - If the function fails to remove the product from the cart, it returns a
false
status along with an error message.
For further insights, refer to the source code.
Function Description: Update Cart Quantity
This function modifies the quantity of a specified product in the WooCommerce cart.
Logic
The function requires both the product ID and the variation ID for variable products. We utilize the WooCommerce built-in set_quantity
function to perform the quantity update.
While the set_quantity
function doesn't require the product ID to update the variation quantity in the cart, our function does. This is because we've incorporated additional utility functions that necessitate both IDs.
This function also enhances error handling for various scenarios.
Usage
This method accepts a product ID, a new quantity number, and a variation ID (for variable products), updating the cart quantity accordingly.
The function returns an array containing a status
and message
element.
- If the function fails to update the product quantity in the cart, it returns a
false
status along with an error message. - If the product quantity is updated successfully, it returns a
true
status, and the message element contains the corresponding success message.
For further insights, refer to the source code.