2

I have created a plugin mainly following this guide, which simply adds a small bit of data to a given product.

I know Woocommerce have made some changes outlined here.

The problem I'm having is that when I add my item to the cart, and access the cart page, I am getting a blank screen. I believe the problem stems from the use of this filter:

add_filter('woocommerce_get_cart_item_from_session'...

If I comment the line with this filter, my checkout page works (but without the extra details added to my product). I can't work out why this filter is not working, or what problem it is having??

The woocommerce changes said:

WooCommerce 2.0 no longer uses the PHP session_start function, instead it makes use of WordPress’ transients, which is great, unless your code happens to rely on $_SESSION.

I'm not starting any new sessions as far as I can see (my code is 90% the same as the first link). Maybe this is a problem with my server? Any ideas?

gray
  • 798
  • 18
  • 31
  • Blank screen? Probably *fatal erroring*, what does the error_log say? – MackieeE Nov 29 '13 at 10:58
  • thanks, never had wordpress debugging turned on, so instead of an error, it just blanked the screen! – gray Nov 29 '13 at 11:34
  • now it's on, I get this: Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in /var/www/sitename/wp-includes/plugin.php on line 199 – gray Nov 29 '13 at 11:35
  • by the looks of it, the filter no longer exists? – gray Nov 29 '13 at 11:37
  • 1
    It doesn't look like it is indeed! http://docs.woothemes.com/document/hooks/ - I think you may need to use `woocommerce_order_get_items` and get the item you need. – MackieeE Nov 29 '13 at 12:03

1 Answers1

3

i was browsing a lot and i recommend that you read the following:

The solution is to hook in there and also restore your custom cart item data.

Example Code:

add_filter( 'woocommerce_add_cart_item_data', function ( $cartItemData, $productId, $variationId ) {
    $cartItemData['myCustomData'] = 'someCustomValue';

    return $cartItemData;
}, 10, 3 );

add_filter( 'woocommerce_get_cart_item_from_session', function ( $cartItemData, $cartItemSessionData, $cartItemKey ) {
    if ( isset( $cartItemSessionData['myCustomData'] ) ) {
        $cartItemData['myCustomData'] = $cartItemSessionData['myCustomData'];
    }

    return $cartItemData;
}, 10, 3 );
To also show the data at the cart/checkout page you can use the following code:

add_filter( 'woocommerce_get_item_data', function ( $data, $cartItem ) {
    if ( isset( $cartItem['myCustomData'] ) ) {
        $data[] = array(
            'name' => 'My custom data',
            'value' => $cartItem['myCustomData']
        );
    }

    return $data;
}, 10, 2 );

The final thing now is to save the data when the order is made:

add_action( 'woocommerce_add_order_item_meta', function ( $itemId, $values, $key ) {
    if ( isset( $values['myCustomData'] ) ) {
        wc_add_order_item_meta( $itemId, 'myCustomData', $values['myCustomData'] );
    }
}, 10, 3 );

You dont have to do anything else the show the data inside the backend, all order item meta data gets display automatically.

this is from

How to retrieve cart_item_data with WooCommerce?

you have to add this stuff to the functions.php file of your theme for example.

Community
  • 1
  • 1
Toskan
  • 13,911
  • 14
  • 95
  • 185