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

…衆ロ難τιáo~ 提交于 2019-12-11 06:03:25

问题


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 .


回答1:


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


来源:https://stackoverflow.com/questions/39873176/woocommerce-need-to-save-cart-item-meta-of-the-each-product-to-orders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!