簡體   English   中英

如何從 WooCommerce 中的訂單商品中獲取購物車商品密鑰

[英]How to get cart item key from order item in WooCommerce

我為購物車中的產品創建了產品插件。 我將每個產品與保存到文件系統的圖像相關聯,因此當您 go 查看訂單時,您可以看到使用產品創建的圖像。

帶有圖像關聯的訂單

保存圖像后,購物車項目將獲得一個自定義鍵。 此自定義鍵在 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 表中添加一個新行,其中 meta_key 為“自定義圖像”。 我遇到的問題是將訂單商品與 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. 與其使用 cookies,不如使用WC_Sessions (如果確實需要)。 但是最好在產品頁面的隱藏輸入字段中添加所有必需的數據......

  2. 當使用woocommerce_add_cart_item_data鈎子將產品添加到購物車時,您最好將所有必需的數據設置為自定義購物車項目數據(因此不需要 cookie 或其他東西)

  3. 此外,您不應該使用woocommerce_thankyou操作掛鈎來添加訂單商品自定義元數據,原因有很多,因為有專門的掛鈎:創建訂單后,您可以使用woocommerce_checkout_create_order_line_item操作掛鈎,將您的自定義購物車商品數據保存為訂單商品元數據.

例如,使用您提供的代碼,我無能為力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM