Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Last active November 4, 2024 15:18
Show Gist options
  • Save Lonsdale201/b422e16428cf336e0b2f9280cfe3717e to your computer and use it in GitHub Desktop.
Save Lonsdale201/b422e16428cf336e0b2f9280cfe3717e to your computer and use it in GitHub Desktop.
JetFromBuilder - Call hook - Woo product Stock modification (like preorder)
// place the code in the child theme functions.php or a custom code snippets plugin, like FluentSnippets
// What do this code
// What this Call hook does is to extract the product identifier from the field that is filled,
// and the order quantity from the quantity field. It checks when the form is submitted
// if there is enough of the product in stock, if so, it subtracts the quantity when the form is submitted.
// If there is not enough, an error is generated and the stock of the product is not changed.
// This code does not require the JFB Add to cart add-on. The hook does not go through the checkout process,
// it only adjusts the stock of the product based on the quantity of the order the user has submitted.
// Example image: https://prnt.sc/WpJXbOlExK__
// If the quantity entered is greater than the quantity in stock,
// an error message will be displayed, you can change the text in the code
// HOW TO USE
// Create your jetformbuilder form. Create a select field where you will list your products,
// (The point is that your field can return the product id, from which the code works)
// and a quantity field (suggested number field type)
// This 2 field required to work properly this Call hook action
// In the code change this two line and replace with your own form field name:
// $product_id = intval($request['products']); // change the 'products' with your Form field name
// $quantity = intval($request['quantity']); // change the 'quantity' with your Form field name
// in the jetformbuilder add a Call Hook post submit action top of the list, and paste this name: check-product-stock
// example image: https://prnt.sc/eygOolauR5GF
use Jet_Form_Builder\Exceptions\Action_Exception;
add_filter('jet-form-builder/custom-filter/check-product-stock', function($result, $request, $action_handler) {
// Check if WooCommerce is active
if (!class_exists('WC_Product')) {
return $result;
}
if (isset($request['products']) && isset($request['quantity'])) {
$product_id = intval($request['products']); // change the 'products' with your Form field name
$quantity = intval($request['quantity']); // change the 'quantity' with your Form field name
// Get the product object
$product = wc_get_product($product_id);
if (!$product) {
throw new Action_Exception('Invalid product selected.', 'failed');
}
// Get the current stock quantity
$stock_quantity = $product->get_stock_quantity();
// Check if stock management is enabled and stock quantity is set
if ($stock_quantity === null) {
// If stock quantity is null, stock management might be disabled
// You can choose to allow or deny the submission here
return $result;
}
// Check if the product is in stock
if (!$product->is_in_stock() || $stock_quantity <= 0) {
throw new Action_Exception('Not available in such a large quantity (maximum available: 0)', 'failed'); // change your text here
}
// Check if there is enough stock (equality is acceptable)
if ($stock_quantity < $quantity) {
throw new Action_Exception('Not available in such a large quantity (maximum available: ' . $stock_quantity . ')', 'failed'); // change your text here
}
// Reduce the stock by the requested quantity
$new_stock = $stock_quantity - $quantity;
$product->set_stock_quantity($new_stock);
$product->save();
// Allow the form submission to proceed
return $result;
} else {
throw new Action_Exception('Required fields are missing.', 'failed'); // change your text here
}
}, 10, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment