如何使用 WooCommerce 检索cart_item_data? [英] How to retrieve cart_item_data with WooCommerce?

查看:33
本文介绍了如何使用 WooCommerce 检索cart_item_data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

add_to_cart 功能期间,有一个过滤器可以添加购物车项目数据".过滤器是woocommerce_add_cart_item_data.我希望将我的自定义插件数据存储在其中,以便存储与项目相关的数据,并且可以使用不同的数据添加多个产品.

这一切似乎都有效,但我无法检索数据.我想不通.数据在那里,我可以在序列化的字符串中看到它,但我无法将其拉出.

echo '

';var_dump(WC());foreach(WC()->cart->get_cart() 作为 $cart_item ) {var_dump( $cart_item );var_dump(WC()->cart->get_item_data($cart_item));}echo '</pre>';

WC() 的第一个转储有一个属性:session->_data->cart->(serialized data).虽然 _data 属性受到保护,但我可以在序列化数据中看到我的自定义字段.

$cart_item 是一个包含 product_id 和一些其他数据的数组,但它不包括我的自定义数据:(

最后,使用get_item_data() 方法 我以为我已经想通了.我传入了购物车项目对象,并且...一个空字符串.如果我传递密钥,而不是购物车项目本身,则相同.

我应该如何访问购物车项目数据?

<小时>

这是添加购物车项目数据"功能,它有效(或至少似乎有效):

function save_class_menu_selection( $cart_item_data, $product_id, $variation_id ) {如果 (!product_is_class( $product_id) ) 返回 $cart_item_data;//保存日期,或者给出致命警告.日期是必需的.如果 ( !empty($_REQUEST['class-date']) ) {$cart_item_data['class-date'] = stripslashes($_REQUEST['class-date']);返回 $cart_item_data;}别的{wp_die('<h2>Invalid Class Date Selected</h2><p>您尝试将课程添加到您的购物车,但选择的日期无效.请重试.</p>');出口;}}add_filter('woocommerce_add_cart_item_data', 'save_class_menu_selection', 10, 3);

解决方案

我今天遇到了同样的情况,经过一番研究,偶然发现了这个问题.经过一些逆向工程,我发现了这个问题,并希望为其他人提供一个解决方案,这些人也可能会遇到这个问题.

问题在于,当购物车项目从会话中恢复时,数据会被清理.因此,额外的购物车项目数据存储在会话中,但在下一个请求中不会恢复.

有一个过滤器woocommerce_get_cart_item_from_session".作为第一个参数,您将获得经过消毒的购物车项目(没有额外数据),第二个参数是存储到会话中的所有数据(包括额外数据).

解决方案是在那里挂钩并恢复您的自定义购物车项目数据.

示例代码:

add_filter('woocommerce_add_cart_item_data', function ( $cartItemData, $productId, $variationId ) {$cartItemData['myCustomData'] = 'someCustomValue';返回 $cartItemData;}, 10, 3 );add_filter('woocommerce_get_cart_item_from_session', 函数 ( $cartItemData, $cartItemSessionData, $cartItemKey ) {如果 ( isset( $cartItemSessionData['myCustomData'] ) ) {$cartItemData['myCustomData'] = $cartItemSessionData['myCustomData'];}返回 $cartItemData;}, 10, 3 );

要在购物车/结帐页面也显示数据,您可以使用以下代码:

add_filter( 'woocommerce_get_item_data', function ( $data, $cartItem ) {如果 ( isset( $cartItem['myCustomData'] ) ) {$数据[] = 数组('名称' =>'我的自定义数据','价值' =>$cartItem['myCustomData']);}返回 $data;}, 10, 2 );

现在的最后一件事是在下订单时保存数据:

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

您无需执行任何其他操作,只需在后端显示数据,所有订单项元数据都会自动显示.

During the add_to_cart function, there is a filter to add "cart item data". The filter is woocommerce_add_cart_item_data. I expected to store my custom plugin data in this, so that the data is stored relative to the item and multiple products can be added with different data.

This all seemed to work, but I am not able to retrieve the data. I can't figure it out. The data is there, I can see it in a serialized string, but I can't pull it out.

echo '<pre>';
var_dump( WC() );

foreach( WC()->cart->get_cart() as $cart_item ) {
  var_dump( $cart_item );
  var_dump( WC()->cart->get_item_data( $cart_item ) );
}
echo '</pre>';

The first dump of WC() has a property: session->_data->cart->(serialized data). The _data property is protected, though, but I can see my custom field inside the serialized data.

The $cart_item is an array with product_id and some other data, but it does not include my custom data :(

Finally, using the get_item_data() method I thought I had it all figured out. I passed in the cart item object, and... an empty string. Same if I pass the key, rather than the cart item itself.

How am I supposed to access the cart item data?


Here is the "Add cart item data" function, which works (or at least seems to work):

function save_class_menu_selection( $cart_item_data, $product_id, $variation_id ) {
  if ( !product_is_class( $product_id ) ) return $cart_item_data;

  // Save the date, or give a fatal warning. Date is required.
  if ( !empty($_REQUEST['class-date']) ) {
    $cart_item_data['class-date'] = stripslashes($_REQUEST['class-date']);
    return $cart_item_data;
  }else{
    wp_die('<h2>Invalid Class Date Selected</h2><p>You tried to add a class to your cart, but the date selected was invalid. Please try again.</p>');
    exit;
  }
}
add_filter( 'woocommerce_add_cart_item_data', 'save_class_menu_selection', 10, 3 );

解决方案

I was in the same situation today and stumbled over this question after some research. After some reverse engineering I found the problem and want to provide a solution for other which may also stumble over this question.

The problem is that the data gets sanitized when the cart items get restored from the session. So the extra cart item data IS stored into the session but on the next request it does not get restored.

There is a filter "woocommerce_get_cart_item_from_session". As first parameter you get the sanitized cart item (without extra data) and as second all data which got stored into the session (including extra data).

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.

这篇关于如何使用 WooCommerce 检索cart_item_data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆