dsj1961061 2013-11-29 10:51
浏览 89

Woocommerce会话:woocommerce_get_cart_item_from_session

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?

  • 写回答

1条回答 默认 最新

  • dreamer2014520 2016-06-08 16:21
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊