Skip Navigation

[Resolved] Take the product price from custom field

This support ticket is created 5 years, 6 months ago. 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.

This topic contains 2 replies, has 2 voices.

Last updated by domenicoS 5 years, 6 months ago.

Author
Posts
#1121314

Hi,

I set a CRED form to allow user to buy a product.

I've a cutom posty type in which there are 2 data:
- the product to buy (A)
- the price to buy

I ask you help to change the price of the product taken the value from the data of the post and not from the the meata-data of woocommerce.

I used this code to change the right ID of the product so is possible to add to the cart the product displayed in the custom post (A). This works fine.

// Change the product ID
add_filter('cred_commerce_add_product_to_cart', function($product_id, $form_id, $post_id){
    if($form_id == 25474){
         
        if(isset($_POST['product_id'])){
            $product_id = $_POST['product_id'];
            $_SESSION['target_post_id']   = $post_id;
        }
    }
    return $product_id;
}, 20, 3);

Now I ask you help to change the price of the cart using the price value displayed in custom post (wpcf-quote-person-price) and not the price set in the woocommerce meta. (B) This doesn not works.

add_action('woocommerce_before_calculate_totals', function(){
    session_start();
    if($form_id != 25474 && !isset($_SESSION['target_post_id']))return;
    global $woocommerce;
     
    $post_id = $_SESSION['target_post_id']; 
    $price = 0;
    foreach ( $woocommerce->cart->get_cart() as $key => $cart_item ) {
         
        $product_id = $cart_item['data']->get_id();
        $person_total_price = get_post_meta($product_id, 'wpcf-quote-person-price', true);
 
        $cart_item['data']->set_price($person_total_price); 
    }
     
}, 999);
#1121906

I ask you help to change the price of the product taken the value from the data of the post and not from the the meata-data of woocommerce.

We cannot help with this.

We can help with how you take data from Toolset Content, in a Toolset Form, we can show how to update native Posts and other data with that data.
How to update complex WooCommerce data sets is something that the dedicated Support of WooCommerce should answer to you.
If you would ask WooCommerce Support to help you update a complex Post Data of Toolset (relationship for example) they'd ask you to ask us (and we could help).
Here, we cannot help.
https://toolset.com/toolset-support-policy/

Now I ask you help to change the price of the cart using the price value displayed in custom post (wpcf-quote-person-price) and not the price set in the woocommerce meta. (B) This doesn not works.

We can help you to get that data, and we can help to find the correct hook in the Toolset Form, where that data exists and can be used.

The data you plan to use seems stored in a Toolset Post Field.
wpcf-quote-person-price
You can get the value of that field with 2 methods:
- from the form input ($_POST['form_field_key'])
- from the post in the database (usually, get_post_meta() or types_render_meta())
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://developer.wordpress.org/reference/functions/get_post_meta/

You do that already with get_post_meta($product_id, 'wpcf-quote-person-price', true);
You mentioned that the field was stored in a Post Type, not in Products, but you seem to get products in the previous code that actually populates $product_id. So it could be that you try to access the wrong post type, if you have that field stored in a Post type that the form edits or creates, then you need to get that field value from that post type, and then update the WooCommerce product with it (or price, or any other data of WooCommerce)

Try to var_dump() $product_id, see what you receive.
hidden link
If that post id you receive is the one with the field, then all good. If not, look earlier in the code what you get.
Next var_dump() $person_total_price and see if it's the right value (should be, if all above is correct)
Now you have the value and can utilize WooCommerce API to update their data set

#1122604

My issue is resolved now. Thank you!

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