Add metas to Re-order on WooCommerce

When you do an re-order on WooCommerce it seems that the system does not copy all of the meta values added by you in the first order.

So i had a site that added some meta fields and when somebody reorder i did not found what meta data it was used. I figure out that when reorder the system does not copy all meta info. I fixed this with a simple function:

add_filter( 'woocommerce_order_again_cart_item_data', 'dex_order_again_cart_item_data', 10, 3 );

function dex_order_again_cart_item_data($cart_item_meta, $product, $order){
    //Create an array of all the missing custom field keys that needs to be added in cart item.
    $customfields = [
        'metakey1added', 'metakey2added',
    ];
    global $woocommerce;
    remove_all_filters( 'woocommerce_add_to_cart_validation' );
    if ( ! array_key_exists( 'item_meta', $cart_item_meta ) || ! is_array( $cart_item_meta['item_meta'] ) )
        foreach ( $customfields as $key ){
            if(!empty($product[$key])){
                $cart_item_meta[$key] = $product[$key];
            }
        }
    return $cart_item_meta;
}

The script will copy all of the meta to the current order. For my site this works like a charm.

Leave a Reply

Your email address will not be published. Required fields are marked *