Skip Navigation

[Résolu] Cred, Woocommerce and Name Your Price

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to set WooCommerce Product price using CRED form

Solution:
To set Woocommerce product price you should use "cred_before_save_data" hook.

You can find the proposed solution with the following link:
https://toolset.com/forums/topic/cred-woocommerce-and-name-your-price/#post-368189

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-api/#cbsd

This support ticket is created Il y a 8 années et 2 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 réponses, has 3 voix.

Last updated by Minesh Il y a 4 années et 5 mois.

Assisted by: Minesh.

Auteur
Publications
#365998

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?

Other people have had the same question.
Here: https://toolset.com/forums/topic/add-woocommerce-nameprice-field-to-cred-form/
And here: https://toolset.com/forums/topic/using-woo-commerce-name-your-price-extension/

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.

Is this possible?

#366144

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I think you should add a numeric field in a WooCommerce CRED form and update post meta
for "woocommerce price" field using "cred_before_save_data" hook.
=> https://toolset.com/documentation/user-guides/cred-api/#cbsd

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.

#368189

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.

#1378433

Hi,

This doesn't work,

I get the following error

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

#1378749

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

@ stephenn-2 - Please open a new ticket in the forum and we will be happy to help.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.