1
votes

I have created product addons for products in my cart. I am associating each product with an image saved to the filesystem, so when you go to view an order you can see the image that was created with the product.

order with image association

When an image is saved, the cart item gets a custom key. This custom key is used in a cookie to carry the path to the image through the checkout process

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 );


When the order is created I then add a new row into the woocommerce_item_meta table with the meta_key of "Custom Image". The issue I am running into is associating the order item with the cart_item_key. (in the woocommerce_thankyou hook)

        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))

For example let's say a user selects 3 images. In the cart the first product will have a custom key of 1, the second will have a custom key of 2, and the third a custom key of 3. Using $woocommerce->cart->get_cart_contents()[cart_item_key]['unique_key']; I can get that unique key while I have access to the cart. Once the order is created however, order_item does not have that key. Order one, two, and three no longer have custom keys. So I cannot get the image from the cookie with the associated key.

$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));

is there a way to retrieve the key that that cart item had, because the order item does not seem to have it?

1
Your question is a bt confuse and unclear. Also woocommerce_item_meta hook doesn't exist… You should provide in your question the code that is saving the image path to order item meta data, this way we could help. Note that StackOverFlow is mainly based on questions with provided code.LoicTheAztec
@LoicTheAztec I updated the post, lmk if it still isn't clearJacob Atwell Parry

1 Answers

3
votes

If you just need to get cart item key, save it as custom hidden order item meta data using:

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 );
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

To get this cart item key from the order item you will use something like:

// 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' );
}

My opinion: You are making things much more complicated than they should be:

  1. Instead of using cookies, you should better use WC_Sessions (if this is really needed). But it should be better to add all required data in hidden input fields on the product page…

  2. You should better set all required data as custom cart item data when the product is added to cart with woocommerce_add_cart_item_data hook (so no need of cookie or something else).

  3. Also you should not use woocommerce_thankyou action hook to add order item custom metadata for many reasons as there is specifically dedicated hooks for that: Once the order is created you can use woocommerce_checkout_create_order_line_item action hook, to save your custom cart item data as order item meta data.

With your provided code I can't help more than that for instance.