I would like to use Cred, Types and Woocommerce to build a donation page that allows people to make a donation of any amount to a specific project.
I created two post types: Donations, and Projects. Donations are a child to Projects. I created a Woocommerce product called Donation. I created a Cred form for the Donations post type that shows a dropdown field of available parent Projects and is linked to the Donate product.
This all works fine. But it only works if a price is defined for the Donate product. I want people to be able to enter any price. So I installed the Name Your Price extension for Woocommerce: hidden link
How can I add an editable amount field to the Cred form that will change the Donate product price?
Adding the hidden _regular_price Woocommerce field to Cred doesn't work, because it can only be added to a form that would be adding a woocommerce product, not a form adding a Donation post.
I tried the function Beda gives that uses to the cred_save_data hook, but it did not work either.
Theoretically, it will be possible but as I do not have "name your price" plugin so I would suggest you should try above steps and if you still need further help, please get in touch with me with problem URL, CRED form name etc..etc.
It would be great for Cred to natively support this extension. However, I have made this work using a numeric custom field on the Donations post type and modifying The code I have looks like this:
function es_donation_amount($post_id, $form_data) {
// if a specific form
if ($form_data['id']==FORM_ID) {
$new_price = get_post_meta($post_id, 'CUSTOM_FIELD', true);
$old_price = get_post_meta(PRODUCT_ID, '_regular_price', true);
update_post_meta(PRODUCT_ID, '_price', $new_price, $old_price);
update_post_meta(PRODUCT_ID '_regular_price', $new_price, $old_price);
}
}
add_action('cred_before_save_data', 'es_donation_amount', 10, 2);
Replace: FORM_ID, CUSTOM_FIELD, and PRODUCT_ID for your specific site.
Fatal error: Uncaught ArgumentCountError: Too few arguments to function es_donation_amount(), 1 passed in /home/website_username/public_html/wp-includes/class-wp-hook.php on line 286 and exactly 2 expected in /home/website_username/public_html/wp-content/themes/page-builder-framework-child/functions.php:87 Stack trace: #0 /home/website_username/public_html/wp-includes/class-wp-hook.php(286): es_donation_amount(Array) #1 /home/website_username/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #2 /home/website_username/public_html/wp-includes/plugin.php(465): WP_Hook->do_action(Array) #3 /home/website_username/public_html/wp-content/plugins/cred-frontend-editor/application/models/form/post.php(519): do_action('cred_before_sav...', Array) #4 /home/website_username/public_html/wp-content/plugins/cred-frontend-editor/application/models/form/base.php(441): CRED_Form_Post->save_form(711) #5 /home/website_username/public_html/wp-content/plugins/cred-frontend-editor/application/controllers/form_buil in /home/website_username/public_html/wp-content/themes/page-builder-framework-child/functions.php on line 87