Created
September 21, 2021 07:38
-
-
Save girafffee/ca6a045222ace247889642fbdd3a5260 to your computer and use it in GitHub Desktop.
Add WooCommerce product with using Custom Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Jet_Form_Builder\Actions\Action_Handler; | |
use Jet_Form_Builder\Exceptions\Action_Exception; | |
/** | |
* add-product - the name of your custom hook | |
*/ | |
add_action( 'jet-form-builder/custom-action/add-product', function ( $request, Action_Handler $handler ) { | |
if ( ! function_exists( 'wc_get_product' ) ) { | |
return; | |
} | |
$product = wc_get_product( $request['inserted_post_id'] ?? 0 ); | |
if ( ! $product ) { | |
throw ( new Action_Exception( 'Undefined post for update product' ) )->dynamic_error(); | |
} | |
/** | |
* Here you can substitute your field name | |
* with a numeric value instead of 'price' | |
*/ | |
$price = $request['price'] ?? 1; | |
/** | |
* More methods here... | |
* https://woocommerce.github.io/code-reference/classes/WC-Product.html | |
*/ | |
$product->set_status( 'publish' ); | |
$product->set_price( $price ); | |
$product->set_regular_price( $price ); | |
$product->save(); | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a custom hook called check_email in JetFormBuilder. This hook checks whether an email exists in the database.
Expected Behavior:
If the email exists, the hook should return true (or "approved"), allowing the form to proceed with the next action.
If the email does not exist, the hook should return false (or "rejected"), preventing further actions from executing.
Issue:
I am unsure how to make JetFormBuilder process actions conditionally based on my hook’s return value. How can I return "approve" or "reject" and use this result to control form execution?
Any guidance would be appreciated!