I have a problem that I am not sure how to fix.
I have a formstack form on my wordpress site. It allows people to buy a product. For this
particular page, there is only one item available for sale, so once the
form is processed (data sent to Formstack->Stripe and payment confirmation received),
the page needs to "turn off" so others can't purchase the item.
Before I used formstack, I used gravity forms plugin.
When the form submitted, I had a add_action filter in my functions.php in
wordpress. It fired when gravity forms completed the submission process and
ran a function that turned off the page.
Here is that code that worked beautifully.
add_action("gform_after_submission", "set_post_content", 10, 2);
function set_post_content(){
global $cfs;
$field_data = array('sold' => '1');
$post_data = array('ID' => get_the_ID()); // the ID is required
$cfs->save($field_data, $post_data);
}
I see formstack has a webhook function.
I cannot seem to find code on how to parse the data the formstack webhook sends. I used http://requestb.in/ to see the data that was being sent.
I believe I need to use php://input, but once I read the data, I am not sure how to say
If formstack webhook fires, then run this code
function set_post_content(){
global $cfs;
$field_data = array('sold' => '1');
$post_data = array('ID' => get_the_ID()); // the ID is required
$cfs->save($field_data, $post_data);
}
I also believe the code needs to be in the functions.php in my theme file because otherwise it won't understand the $cfs variable that is from another wordpress plugin.
I saw this new formstack function the other day - it sounds like it could solve your problem?http://support.formstack.com/customer/portal/articles/1444519-event-fields
Event handling options
When your Event has filled to capacity, you can mark the Event as "Sold Out" so new submitters will see the item is no longer available. You may also choose to hide this field on the Form once the items have sold out. Additionally, you can deactivate the whole Form when the Event has sold out; this is done through the Form Settings tab > General > Deactivate Form settings.
Related
I have a set of custom fields as part of the user that need to be validated by an API before being saved. For this I am using the user_profile_update_errors hook. The errors are correctly returned to the admin, but the fields are saved regardless of the errors. According to the description of this hook the fields should only be saved if the error array is empty, but this does not seem to be the case.
I have created a very simple script to test this. It contains the following code:
if (!function_exists("testEditUser")) {
function testEditUser(WP_error $errors, bool $update, stdClass $user) {
$errors->add("test_error", "A test error occured");
}
}
add_action('user_profile_update_errors', 'testEditUser', 20, 3);
This script seems to work fine as when I update a user an error is shown after the page loads again
But when I look at the fields that I "tried to change" they actually changed. Not only on the frontend, but also in the database. When I reload the page I am shown this:
I am unsure if this is related.
I am using Woocommerce and Jet Engine for some of the custom fields. Could these plugins be the source of the problem? Should I use a different hook? Any form of help would be appreciated!
I have just started working with wordpress, woocommerce and woocommerce rest API.
This is something I am unable to figure out the reason of.
When I use wordpress woocommerce rest api to post data, it posts it multiple time and keeps on posting again and again, like an infinite loop.
(Get, Delete, Put are working fine)
Code:
$product_to_be_duplicated = $woocommerce->get('products/' . $id_to_be_dulpicated);
$duplicated_product = json_decode(json_encode($product_to_be_duplicated), true);
$dup_description = "duplicate of " . $duplicated_product['description'];
$dup_tags = array_merge($duplicated_product['tags'], [['id' => $tag_id, 'name' => $tag_name, 'slug' => $tag_slug]]);
array_shift($duplicated_product);
$duplicated_product['description']= $dup_description;
$duplicated_product['tags'] = $dup_tags;
$duplicated_product['catalog_visibility'] = 'hidden';
try
{
$woocommerce->post('products', $duplicated_product);//the problem statement
}
catch (HttpClientException $e)
{
print_r($e->getMessage());
exit();
}
The only problem is with the post as it is being called and filling database again and again.
How is this piece of code hooked in? Which action is it attached to? Often times, things like this will happen when the code you've hooked to is fired by the code you run, i.e. you've hooked to:
add_action('save_post', 'my_example_function');
and your code triggers:
do_action('save_post');
In your example, since you're duplicating a product, saving that will trigger the save_post action, which calls your code again, which starts your infinite loop.
Read up on this section from the WP docs: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops
I am sharing 3 screens step by step will be through woocommerce.
I have a shop page. On selecting a product, I will go to a front end form for creating a post. After creating a post I will go to checkout page.
My question is that, can I get the middle one (front end form for creating a post) in woocommerce platform ?
Please look my shared screen for understanding it better.
Thank you
First Screen
Second Screen
Third Screen
My question is that, can I get the middle one (front end form for
creating a post) in woocommerce platform ?
Sure, but you have to save the data from step 2 in the current session (cart).
Only when an order is created, collect the session data and create the (custom_post_type) job post. You don't want to create the job post before the order is completed / paid for.
Your question is very general, so you can expect a similar answer. We do not build complete plugins here. You will need to use multiple hooks to complete this.
EDIT: some more info
# step 2, collect, sanitize and validate the form values.
I would save this data in the cart session. something like this:
// I don't know how and where you collect the step 2 data,
// so i can't provide a hook for this function.
function step_2_data_to_session() {
global $woocommerce;
$step_two_data = array();
// validate and sanitize data first
// save data to woocommerce session
$woocommerce->session->set('step_two_data', $step_two_data );
}
Then when the user pays and creates an order, use a hook to collect the session data and save it to your costom post type.
add_action('woocommerce_checkout_update_order_meta', 'new_order', 10, 2)
function new_order($order_id, $data) {
global $woocommerce;
// At this point the order is created and the session data is still alive.
if(! is_admin() ) {
$step_two_data = $woocommerce->session->get('step_two_data');
if($step_two_data) {
$args = array(
'post_author' => get_current_user_id(),
'post_title' => 'The Job title here',
'post_type' => 'job',
'post_status' => 'publish'
);
$new_job_post_id = wp_insert_post($args);
if($new_job_post_id) {
// Job post is saved now..
// Now you'll probably want to add step_two_data as meta data to the post.
}
}
}
}
NOTE: I haven't tested this code, error handling is not included, etc. This is just a pointer how to save the session data when the order is created.
You'll have to do much more, for example, i see that the job listing has a set expiration date. I would use a cron-job to daily check which jobs have to be removed, with the same cron i would also inform customers their job post 'will be removed in 2 days' etc etc ;-)
Regards, Bjorn
In WordPress, I have custom registration site for new users. Upon registration, there is an optional checkbox to subscribe to our newsletter. As far as I understand it, it adds the value of the checkbox to the user_meta table (the whole thing has been coded by a company in India, which I woould very much prefer to not involve again, since they delayed their work time and time again and didn't do good work after all).
The corresponding code snippet in my child theme's functions.php looks like this:
<?php echo '<div class="form-row form-row-wide">
<input type="checkbox" name="inter_offers" value="yes"> '; _e('I would like to subscribe to the Kurth Electronic newsletter.','kurth-child'); echo '<br></div>';
return $fields; ?>
add_action('woocommerce_created_customer','adding_extra_reg_fields');
function adding_extra_reg_fields($user_id) {
extract($_POST);
update_user_meta($user_id, 'billing_inter_offers',$inter_offers);
} ?>
(I have left out lines irrelevant to this issue.)
Now, this value is saved internally, but not displayed to me. I would like to show the value in an E-Mail or notification generated by WordPress when the user completes registration, so that we can manually add them to our newsletter list whenever someone chooses to subscribe to the newsletter. The problem is that I only have a limited knowledge of PHP and I don't know where to start.
I should also note that this is not done via the standard WorPress registration form, but by a WooCommerce registration form (I have disabled the standard WordPress registration for security reasons).
I tried using the "Better Notifications" plugin (https://wordpress.org/plugins/bnfw/) for custom notifications whenever a new user completes their registration, but it ignores any php code that I add to the custom notifications body to display the user_meta data.
Any help would be appreciated.
Because the registration is done via woocomerce you may have to look for a notification PlugIN that works with woocomerce, the one you have may just work properly with wordpress core version!
You also could generate a mail via php in the function, so that you get a message with the user mail adress, but i think without php knowledge it is not that easy to use the built in php mailer... (You may need an api there!)
But wouldn't it be better to automatically sign them into your newsletter software? For example for Mailchimp or other systems like that there are quite good wordpress plugins!
You may also be able to include the forms of these PlugIns in your registration form, but without a closer look at this woocomerce registration i can't tell for sure!
I think this will do the trick, it will notify you every time a new user is created and also tell you if they subscribed or not.
function new_customer_registered_send_email_admin($user_login, $user_email) {
ob_start();
do_action('woocommerce_email_header', 'New customer registered');
$email_header = ob_get_clean();
ob_start();
do_action('woocommerce_email_footer');
$email_footer = ob_get_clean();
$user = get_user_by( 'email', $user_email );
$subscribed = get_user_meta( $user->ID, 'billing_inter_offers', true );
woocommerce_mail(
get_bloginfo('admin_email'),
get_bloginfo('name').' - New customer registered',
$email_header.'<p>The user '.esc_html( $user_login ).' created an account ' . ( $subscribed ? ' and subscribed to the newsletter.' : '.' ) . '<br>Email:'.esc_html( $user_email ).'</p>'.$email_footer
);
}
add_action('new_customer_registered', 'new_customer_registered_send_email_admin', 10, 2);
I ended up using the plugin amr users to generate a userlist of all users who had a certain metadata tag set to a certain value (in my case, if they want to recieve a newsletter - the previous developers never bothered to make the data actually readable without extra effort). It is a little clunky to use and not what I originally intended, but it got the job done.
Maybe I overlooked it, but as much as I searched, I could not find which action to hook when a subscription changes price or frequency in Woocommerce Subscriptions.
The documentation says that to support price changes in your payment gateway you have to list subscription_amount_changes, but nowhere it says which function will be called when the amount actually changes..
Also in the Action reference I was unable to find an action hook which is called when the subscription amount or frequency changes. If anyone has a clue which hook to use or how to implement this particular feature, please tell me.
Edit
Ok thanks for the comments and answer by #Reigel, so if I understand correctly the change of a subscription in the admin menu (which I indeed refer to), has to be handled by the save_post action. Could you provide a small example how to hook this action and check if it is a subscription and get the $order_id (I guess this is the same as post_id?) to use in the subscription management calls?
Thank you very much already!
This should be considered an addon to the answer by #Reigel. If you upvote this, upvote his answer too.
Here's an example of hooking the pre_post_update action. It occurs a little before the save_post action. Both actions are triggered in the wp_insert_post() function in post.php.
function post_save_subscription_check( $post_ID, $data )
{
if( $data['post_type'] == 'product' ) {
if (!empty($_POST['_subscription_price']) && get_post_meta($post_ID, '_subscription_price', true) != $_POST['_subscription_price']) {
/* do stuff here */
}
if (!empty($_POST['_subscription_period']) && get_post_meta($post_ID, '_subscription_period', true) != $_POST['_subscription_period']) {
/* do stuff here */
}
}
}
add_action('pre_post_update', 'post_save_subscription_check', 10, 2 );
In the logic we are checking for the old value, obtained with get_post_meta() and the new value, held in the $_POST variable and comparing them.
This code only executes when a post updates, not for a new post
The code gets placed in your theme functions.php or custom plugin code.
In live code I would recommend cleaning any $_POST data before using it. I haven't bothered here.
I will try to explain about supports.
subscription_amount_changes is just a support and will not fire anything. You can use it for conditional statements, like:
if ( !$chosen_gateway->supports( 'subscription_amount_changes' )) {
echo 'Please be considerate and do not change the price for the chosen gateway does not support it.';
}
now, other plugins can then check if the chosen gateway supports subscription_amount_changes and do their appropriate actions.
action hook which is called when the subscription amount or frequency
changes
subscription is just a product type. Which means this is just a post with a post_type of product. The amount and frequency are just post meta. all are handled on save_post action. add_action( 'save_post', __CLASS__ . '::save_subscription_meta', 11 );. This is on the post_type=product. You have to check also for save_post on post_type=shop_order as it's more appropriate for checking support. Because there's already a gateway chosen.