Tell us what you are trying to do?
I'm trying to create an invoice payment system with toolset.
I have 2 custom post types, invoices and payments. When someone creates a payment post with the payment form, they enter the invoice number and amount. The Payment form is linked to a product 'Invoice payment' with a price of $0.00. I want to take the amount that is entered in the custom field on the payment form and update the price of the product in the cart.
I'm following this thread: https://toolset.com/forums/topic/integrating-woocommerce-name-your-price-extenstion-into-cred/#post-1090318
And I've adapted this custom function:
function update_invoice_payment_price($post_id, $form_data) {
$product = 2810;
// if a specific form
if ($form_data['id']==2770) {
$new_price = get_post_meta($post_id, 'wpcf-payment_amount', true);
$old_price = get_post_meta($product, '_regular_price', true);
update_post_meta($product, '_price', $new_price, $old_price);
update_post_meta($product, '_regular_price', $new_price, $old_price);
}
}
add_action('cred_before_save_data', 'update_invoice_payment_price', 10, 2);
However, it's causing a fatal error on my site :
[10-Jan-2020 04:03:33 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function update_invoice_payment_price(), 1 passed in .../wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in ..../wp-content/themes/hello-elementor-child/functions.php:471
Please help shed some light on what's happening here.
I was able to get my custom code to work and I'm posting in case it helps others, as I've seen the topic on this forum several times.
In my above comment, I was returning a fatal error because cred_before_save_data only accepts 1 argument, unlike cred_save_data which accepts 2.
After fixing this, I was able to update the cart item price with the value of my form field by using the following code:
add_action('cred_before_save_data', 'get_invoice_amount', 10, 1);
function get_invoice_amount($form_data){
global $payment_amount;
//check to make sure we are using cred form w/ id of 2770
if ($form_data['id']==2770) {
//if the 'payment amount' Post variable is set
if ( isset($_POST['wpcf-p_invoice_amount']) ){
//set global $payment_amount variable to 'payment_amount' field value
$payment_amount = $_POST['wpcf-p_invoice_amount'];
}
}
}
// Set custom text and calculated price as custom cart data in the cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_new_price_in_cart_object', 30, 3 );
function save_new_price_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
global $payment_amount;
if( ($product_id != 2810) || (! isset( $payment_amount )) )
return $cart_item_data;
// Get an instance of the WC_Product object
$product = $variation_id > 0 ? wc_get_product($variation_id) :
wc_get_product($product_id);
$product_price = (float) $product->get_price(); // Get the product price
// Get payment amount
$amount = esc_attr( $payment_amount );
// Set the new calculated price as custom cart data for the cart item
//original product price should be set to $0.00
$cart_item_data['custom_data']['price'] = $product_price + $amount;
return $cart_item_data;
}
// Set the new calculated price of the cart item
add_action( 'woocommerce_before_calculate_totals', 'set_new_cart_item_price', 90, 1 );
function set_new_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( isset( $cart_item['custom_data']['price'] ) ) {
// Get the new calculated price
$new_price = (float) $cart_item['custom_data']['price'];
// Set the new calculated price
$cart_item['data']->set_price( $new_price );
}
}
}
This thread, while unresolved, was very helpful in coming to this solution:
https://toolset.com/forums/topic/cred-commerce-calculate-form-fields-and-sendoverwrite-total-to-cart-item/