我已经在购物车中为产品创建了产品附加组件。我将每个产品与保存到文件系统的图像相关联,因此当您查看订单时,可以看到使用该产品创建的图像。
保存图像后,购物车项将获得自定义键。 Cookie中使用了此自定义键,以通过结帐流程携带指向图像的路径
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 );
创建订单后,我会使用“自定义图像”的meta_key在woocommerce_item_meta表中添加新行。我遇到的问题是将订单商品与cart_item_key相关联。 (在woocommerce_thankyou钩中)
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))
例如,假设用户选择了3张图像。在购物车中,第一个产品的自定义键为1,第二个产品的自定义键为2,第三个产品的自定义键为3。
$woocommerce->cart->get_cart_contents()[cart_item_key]['unique_key'];
我可以在访问购物车时获得该唯一密钥。但是,一旦创建订单,order_item就没有该密钥。一阶,二阶和三阶不再具有自定义键。因此,我无法从具有关联密钥的cookie中获取图像。
$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));
是否有一种方法可以检索该购物车商品具有的密钥,因为订单商品似乎没有它?
答案 0 :(得分:0)
如果您只需要获取购物车商品密钥,请使用以下方式将其另存为自定义隐藏订单商品元数据:
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 );
}
代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。
要从订单商品中获取此购物车商品密钥,您将使用类似以下内容:
// 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 = get_meta( '_cart_item_key' );
}
我的观点:您正在使事情变得比应有的复杂得多……
1)您最好使用WC_Sessions
(如果确实需要),而不要使用Cookie。但是最好在产品页面的隐藏输入字段中添加所有必需的数据 ...
2)当使用woocommerce_add_cart_item_data
钩将产品添加到购物车时,最好将所有必需数据设置为自定义购物车项目数据(因此不需要Cookie或其他东西) )。
3)另外,出于多种原因,您也不应该使用woocommerce_thankyou
操作挂钩添加订单项自定义元数据,因为它具有专门的挂钩:创建订单后,您可以使用woocommerce_checkout_create_order_line_item
操作挂钩,以将您的自定义购物车商品数据保存为订单商品元数据。
使用您提供的代码,我无能为力。