如何从 WooCommerce 中的订单项获取购物车项密钥

How to get cart item key from order item in WooCommerce

我已经为购物车中的产品创建了产品插件。我将每个产品与保存到文件系统的图像相关联,因此当您查看订单时,您可以看到使用该产品创建的图像。

保存图像后,购物车项目会获得一个自定义键。此自定义密钥在 cookie 中用于通过结帐过程携带图像的路径

if (!function_exists('force_individual_cart_items')) {
    function force_individual_cart_items( $cart_item_data, $product_id ){
      $unique_cart_item_key = md5( microtime().rand() );
      $cart_item_data['unique_key'] = $unique_cart_item_key;

        if (isset($_COOKIE['CustomImagePath'])) {// if image path exists that needs to be saved
            $imagePath = $_COOKIE['CustomImagePath']; // get image path
            $imagePaths = (isset($_COOKIE['ImagePaths']) ? json_decode(stripslashes(html_entity_decode($_COOKIE['ImagePaths'])), true) : array());
            $imagePaths[$unique_cart_item_key] = $imagePath; //asscoiate image path with product cart key
            setcookie('ImagePaths', json_encode($imagePaths),0,'/'); // save association in image paths cookie
            unset($_COOKIE['CustomImagePath']);
            setcookie('CustomImagePath', null, -1, '/');
        }
      return $cart_item_data;
    }
}
add_filter( 'woocommerce_add_cart_item_data','force_individual_cart_items', 10, 2 );


创建订单后,我将新行添加到 woocommerce_item_meta table 中,其中 meta_key 为 "Custom Image"。我 运行 遇到的问题是将订单项与 cart_item_key 相关联。 (在 woocommerce_thankyou 钩子中)

        global $wpdb, $wp;
        $query = "SELECT order_item_id FROM wp_woocommerce_order_items WHERE order_id = $order_id";
        $result = $wpdb->get_results($query, true);
        $imagePaths = json_decode(stripslashes(html_entity_decode($_COOKIE['ImagePaths'])), true);

        foreach ($result as $order_item) {
            $item_id = $order_item->order_item_id;
        }
        $cart_item_custom_key = NOT AVAILABLE IN THE ORDER ITEM
        $filePath = $_COOKIE[$cart_item_custom_key];
        $wpdb->insert('wp_woocommerce_order_itemmeta', array('order_item_id'=>$post->ID, "meta_key"=>'Custom Image', 'meta_value'=>$filePath))

例如,假设用户选择了 3 张图片。在购物车中,第一个产品的自定义键值为 1,第二个产品的自定义键值为 2,第三个产品的自定义键值为 3。使用 $woocommerce->cart->get_cart_contents()[cart_item_key]['unique_key']; 当我有权访问购物车时,我可以获得该唯一密钥。但是,一旦创建订单,order_item 就没有该密钥。订单一、二和三不再有自定义键。所以我无法使用关联的密钥从 cookie 中获取图像。

$filePath = $_COOKIE[ ? KEY NO LONGER EXISTS ? ];
$wpdb->insert('wp_woocommerce_order_itemmeta', array('order_item_id'=>$post->ID, "meta_key"=>'Custom Image', 'meta_value'=>$filePath));

有没有办法检索购物车商品的密钥,因为订单商品似乎没有?

如果您只需要获取购物车项目密钥,请将其另存为自定义隐藏订单项目元数据,使用:

add_action('woocommerce_checkout_create_order_line_item', 'save_cart_item_key_as_custom_order_item_metadata', 10, 4 );
function save_cart_item_key_as_custom_order_item_metadata( $item, $cart_item_key, $values, $order ) {
    // Save the cart item key as hidden order item meta data
    $item->update_meta_data( '_cart_item_key', $cart_item_key );
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

要从订单商品中获取此购物车商品密钥,您将使用如下内容:

// Get the WC_Order Object from order ID (if needed)
$order = wc_get_order( $order_id );

// Loop though order items
foreach ( $order->get_items() as $item ){
    // Get the corresponding cart item key
    $cart_item_key = $item->get_meta( '_cart_item_key' );
}

我的看法:你让事情变得比它们应该的复杂得多:

  1. 最好不要使用 cookie,而应使用 WC_Sessions(如果确实需要)。但是最好在产品页面的隐藏输入字段中添加所有必需的数据…

  2. 您最好将 所有 所需数据设置为自定义购物车项目数据,当使用 woocommerce_add_cart_item_data 挂钩将产品添加到购物车时 (所以不需要 cookie 或其他东西).

  3. 此外,出于多种原因,您不应该使用 woocommerce_thankyou 操作挂钩来添加订单项自定义元数据,因为有专门的挂钩:创建订单后,您可以使用 woocommerce_checkout_create_order_line_item 操作挂钩,将您的自定义购物车项目数据保存为订单项目元数据

例如,使用您提供的代码,我无能为力。