As this is custom data, it’s hard for me to tell you what could be causing the issue. You mentioned $_SESSION data though – are you aware WooCommerce does not use PHP Sessions any more?
Thread Starter
acn
(@acseven)
Thanks for your reply. I did read about that, I think regarding using wp transients, but frankly I couldn’t find a way to apply it to my situation. But the strange thing is that it really does run without issues whatsoever on my local server.
What I’m doing, in respect to managing the $_SESSION data, is fairly as described in this blog post:
https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/
Since sessions can work differently across servers, I think this is a good place to start. You don’t need sessions to add extra cart item data at all.
This is quite out of date, but look at how https://wordpress.org/plugins/woocommerce-product-gift-wrap/ does it.
Thread Starter
acn
(@acseven)
Thanks for the suggestion. I’ve changed from php sessions to WC based sessions, and the behavior is still the same: on my local server it works fine but on the production server it’s not.
Here’s some part of my code, the most pertaining to sessions at least. Again, this owrks fine on the local server:
if ( ! function_exists('my_ajax_custom_data_callback_inline')) {
function my_ajax_custom_data_callback_inline () {
global $woocommerce;
do_action( 'woocommerce_set_cart_cookies', true );
if ( !empty ( $_POST['my_post_data'] ) ) {
WC()->session->set( my_meta_data' , $_POST['my_post_data'] );
}
wp_die();
}
}
// AJAX hooks
add_action('wp_ajax_' . 'my_custom_data', 'my_ajax_custom_data_callback_inline', 1);
add_action('wp_ajax_nopriv_' . 'my_custom_data', 'my_ajax_custom_data_callback_inline', 1);
add_filter( 'woocommerce_add_cart_item_data', 'my_add_cart_item_data', 10, 2 );
function my_add_cart_item_data ( $cart_item_data, $product_id) {
global $woocommerce;
$new_value = array ();
$my_data_from_session = WC()->session->get( 'my_meta_data' );
if ( !empty ( $my_data_from_session ) ) {
$new_value['my_meta_data_in_cart'] = $my_data_from_session;
$merged_arrays = array_merge ( $cart_item_data, $new_value );
return $merged_arrays;
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'my_get_cart_items_from_session', 10, 3 );
function my_get_cart_items_from_session ( $cart_item, $values, $key ) {
if ( array_key_exists ( 'my_meta_data_in_cart' , $values ) ) {
$cart_item['my_meta_data_in_cart'] = $values['my_meta_data_in_cart'];
}
return $cart_item;
}
I’m really lost with this :/
One thing that may catch you off, sessions don’t persist without a ‘cart’. A cookie is set by WC when a cart has contents.
Does it work after you have a cart?
Have you looked in the database to see if any of your data is storing?
Does system status report state sessions table exists?
Thread Starter
acn
(@acseven)
Thanks for those pointers. I ended up going back up a bit to see if I was doing something wrong that seemed to be working properly.
So, with all client sessions cleared, transients cleared and empty cart, I was able to identify one thing that is going wrong, can’t say if it happened before or not. Considering the code I wrote before, with these hooks:
add_action('wp_ajax_' . 'my_custom_data', 'my_ajax_custom_data_callback_inline', 1);
add_action('wp_ajax_nopriv_' . 'my_custom_data', 'my_ajax_custom_data_callback_inline', 1);
add_filter( 'woocommerce_add_cart_item_data', 'my_add_cart_item_data', 10, 2 );
What I’m seeing is that the AJAX php callback is getting called after the woocommerce_add_cart_item_data filter. As I have custom data being set with the ajax call that is supposed to be later used when actually adding the data to cart, what happens is that the custom data is being added only to the next cart item addition.
Is there any way to streamline this properly?
– AJAX sets data in session:
WC()->session->set( my_meta_data' , $_POST['my_post_data'] );
– Data is merged when adding to Cart:
$my_data_from_session = WC()->session->get( 'my_meta_data' );
Thanks
Thread Starter
acn
(@acseven)
I think I’m getting somewhere. I ended up not using AJAX, it wasn’t necessary for what I’m doing. I’m still having issues with WPML compatibility and strange bugs on the production server, but I’m trying to sort them out.
Thanks for the pointers though