I used this to change the price of a product created with CRED.
add_action('woocommerce_before_calculate_totals', function(){
session_start();
if($form_id != 24662 && $form_id != 25474 && !isset($_SESSION['target_post_id']))return;
global $woocommerce;
$price = 0;
foreach ( $woocommerce->cart->get_cart() as $key => $cart_item ) {
$product_id = $cart_item['data']->get_id();
$post_id = $cart_item['cred_meta']['cred_post_id'];
//Fields
$adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
$adult_price = get_post_meta($post_id, 'wpcf-quote-person-price', true);
$price = $adult_price * $adult_num;
$cart_item['data']->set_price($price);
}
}, 999);
The problem is that when I submit the form on my pc it works perfectly.
But if the form page is opened in the browser’s privacy mode this code does not works.
In few word it works only on my computer, not in the computer of another user.
Can you hel me about why it changes?
Hi,
I assume this is a legacy of your previous thread:
https://toolset.com/forums/topic/booking-form-to-buy-a-product/
Those custom PHP codes is based on PHP session, that could be a problem when use privacy mode browser.
In your case, you can use meta of woocommerce item, for example, change the PHP codes as below:
add_action('woocommerce_before_calculate_totals', function($array){
global $woocommerce;
$form_ids_arr = array(24662, 25474); //here setup form IDs
foreach ( $woocommerce->cart->get_cart() as $key => $cart_item ) {
if(!isset($cart_item['cred_meta']['cred_form_id'])
|| !in_array($cart_item['cred_meta']['cred_form_id'], $form_ids_arr)){
continue;
}
if(isset($cart_item['cred_meta']['cred_post_id'])){
$post_id = $cart_item['cred_meta']['cred_post_id'];
}
$product_id = $cart_item['data']->get_id();
//Fields
$adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
$adult_price = get_post_meta($post_id, 'wpcf-quote-person-price', true);
$price = $adult_price * $adult_num;
$cart_item['data']->set_price($price);
}
}, 10);
Thank you Luo! It's perfect