user5742826
user5742826

Reputation:

woocommerce : Need to save cart item meta of the each product to orders

currently i add product to the cart by $woocommerce->cart->add_to_cart( 21, 1, 0, $item,$cart_item_data)

now product with id 21 is added to cart . I save many details in $cart_item_data .What i want is when the order is created for this cart product then the $cart_item_data need to save to the db , and in the order section of admins i can see the details of each product with this $cart_item_data.

I know how to save order item meta .

add_action('woocommerce_add_order_item_meta',function($item_id, $values, $cart_item_key){

wc_add_order_item_meta( $item_id, 'Reference', 12345 , false ); 

        },10,2);

But my problem is i need to get values from $cart_item_data and save in woocommerece_order_itemmeta table .

Note : $cart_item_data=is an array in which i saved some custom details during the time of add to cart

Please help to solve this .

Upvotes: 1

Views: 1618

Answers (1)

Vidish Purohit
Vidish Purohit

Reputation: 1078

If you have correctly added custom data to cart for your product, then you will have it in $item in below code and you can use below code to save further.

add_action('woocommerce_add_order_item_meta','add_order_item_meta',1,2);

function add_order_item_meta($item_id, $values) {

    if(isset($values['_my_custom_info']) && !empty($values['_my_custom_info'])) {
        // Get the custom array
        $arrCustomInfo = $values['_my_custom_info'];

        // For each custom element
        foreach($arrCustomInfo AS $key => $arrInfo) {

            if(isset($arrInfo['quantity']) && !empty($arrInfo['quantity'])) {
                // Save variation addon info
                $strKey = $arrInfo['name'] . ' X ' . $arrInfo['quantity'];

                // Save custom order item meta
                wc_add_order_item_meta($item_id, $strKey . ' ', wc_price($arrInfo['price'] * $arrInfo['quantity']));
                wc_add_order_item_meta($item_id, 'Product Image ', $arrInfo['image']);
            }
        }
    }
}

Upvotes: 1

Related Questions