Navigation überspringen

[Gelöst] form work only on my pc

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

Display post custom fields in Woocommerce cart page.

Solution:

It needs custom codes, for example you can use Woocommerce action hook woocommerce_before_calculate_totals, like this:

https://toolset.com/forums/topic/form-work-only-on-my-pc/#post-1132566

Relevant Documentation:

This support ticket is created vor 6 Jahren, 3 Monaten. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von domenicoS vor 6 Jahren, 3 Monaten.

Assistiert von: Luo Yang.

Author
Artikel
#1132280

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?

#1132566

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);
#1134133

Thank you Luo! It's perfect