woocommerce:需要将每个产品的购物车项目元保存到订单 [英] woocommerce : Need to save cart item meta of the each product to orders

查看:68
本文介绍了woocommerce:需要将每个产品的购物车项目元保存到订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我通过$woocommerce->cart->add_to_cart( 21, 1, 0, $item,$cart_item_data)

现在id为21的产品已添加到购物车.我将许多详细信息保存在$ cart_item_data中.我想要的是为此购物车产品创建订单时 $cart_item_data需要保存到db,并且在管理员的命令部分中,我可以使用此$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.

我知道如何保存订单商品meta.

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

但是我的问题是我需要从$cart_item_data获取值并将其保存在woocommerece_order_itemmeta表中.

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

注意:$ cart_item_data =是一个数组,我在添加到购物车时保存了一些自定义详细信息

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

请帮助解决此问题.

推荐答案

如果您已将自定义数据正确添加到购物车中,那么您将在下面的代码中将其保存在$ item中,并可以使用下面的代码进行进一步保存

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

这篇关于woocommerce:需要将每个产品的购物车项目元保存到订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆