如何从WooCommerce订单中提取可变产品的尺码信息?
问题:WooCommerce可变产品订单导出时无法获取尺码属性
我使用WP ALL EXPORT PRO结合PHP与ACF,筛选包含指定产品的WooCommerce订单并导出XML给履约伙伴,目前代码运行正常,但T恤、卫衣这类可变产品的GRÖßE(尺码)属性无法获取。之前尝试用ACF的get_field('size', $product_id)方法无效,需要将该尺码值集成到XML结构中。
当前XML编辑器代码:
<?xml version="1.0" encoding="UTF-8"?> <orders> <!-- BEGIN LOOP --> <order> <commission>{Bestell ID}</commission> <production>2</production> <receiver> <line1>{Shipping First Name} {Shipping Last Name}</line1> <line2>{Shipping Company}</line2> <street>{Shipping Address 1}</street> <streetnumber>{Shipping Address 2}</streetnumber> <country_code>{Shipping Country}</country_code> <zip>{Shipping Postcode}</zip> <city>{Shipping City}</city> <email>{Customer Account Email Address}</email> </receiver> <items> [my_get_order_items({Bestell ID})] </items> </order> <!-- END LOOP --> </orders>
当前PHP函数代码:
<?php function my_get_order_items( $order_id ) { $order = wc_get_order( absint($order_id) ); // Get the WC_Order object if ( ! is_a($order, 'WC_Order') ) { return false; // Exit if not an order } $order_details = ""; // Initialize variable as string to store order details // Loop through order items foreach ( $order->get_items() as $item ) { if ( ! ( strpos($item->get_name(), 'KERAMIKTASSE') !== false || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false || strpos($item->get_name(), 'T-SHIRT') !== false ) ) { continue; } $product = $item->get_product(); // Get the product object $product_id = $item->get_product_id(); // Get the product object $order_details .= "**LT**item**GT**"; $order_details .= "**LT**ID**GT**" . $product->get_sku() . "**LT**/ID**GT**"; $order_details .= "**LT**produktname**GT**" . $item->get_name() . "**LT**/produktname**GT**"; $order_details .= "**LT**amount**GT**" . $item->get_quantity() . "**LT**/amount**GT**"; $order_details .= "**LT**upload**GT**" . maybe_serialize( get_field( 'upload', $product_id ) ) . "**LT**/upload**GT**"; $order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product_id ) ) . "**LT**/size**GT**"; $order_details .= "**LT**groesse**GT**" . maybe_serialize( get_field( 'produktgröße', $product_id ) ) . "**LT**/groesse**GT**"; $order_details .= "**LT**material**GT**" . maybe_serialize( get_field( 'material', $product_id ) ) . "**LT**/material**GT**"; $order_details .= "**LT**print**GT**" . maybe_serialize( get_field( 'print', $product_id ) ) . "**LT**/print**GT**"; $order_details .= "**LT**variante**GT**" . maybe_serialize( get_field( 'variante', $product_id ) ) . "**LT**/variante**GT**"; $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product_id ) ) . "**LT**/category**GT**"; //add options to the output $order_details .= "**LT**Options**GT**"; $order_details .= "**LT**Option**GT****LT**ID**GT**" . "1996" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_115', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2489" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_117', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2056" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_118', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**/Options**GT**"; $order_details .= "**LT**/item**GT**"; } return $order_details; } ?>
其中无效的尺码获取代码:
$order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product_id ) ) . "**LT**/size**GT**";
解决方案:从订单项直接获取变体尺码
可变产品的尺码属性存储在订单项(WC_Order_Item_Product对象)中,而非父产品的ACF字段,因此不能通过父产品ID调用ACF获取。可以通过以下方式获取:
核心修改逻辑
- 利用
$item->get_meta()方法直接获取订单项中的尺码属性值(需使用属性的slug,WooCommerce中属性slug以pa_开头,比如GRÖßE的slug为pa_größe) - 用
esc_html()转义输出内容,避免XML语法错误 - 针对简单产品设置默认空值,避免输出异常
修改后的完整PHP函数
<?php function my_get_order_items( $order_id ) { $order = wc_get_order( absint($order_id) ); if ( ! is_a($order, 'WC_Order') ) { return false; } $order_details = ""; foreach ( $order->get_items() as $item ) { if ( ! ( strpos($item->get_name(), 'KERAMIKTASSE') !== false || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false || strpos($item->get_name(), 'T-SHIRT') !== false ) ) { continue; } $product = $item->get_product(); $product_id = $item->get_product_id(); // 获取尺码属性值,使用属性的slug(需和WooCommerce后台一致) $size = $item->get_meta('pa_größe'); // 简单产品无尺码时设为空字符串 $size = !empty($size) ? esc_html($size) : ''; $order_details .= "**LT**item**GT**"; $order_details .= "**LT**ID**GT**" . $product->get_sku() . "**LT**/ID**GT**"; $order_details .= "**LT**produktname**GT**" . $item->get_name() . "**LT**/produktname**GT**"; $order_details .= "**LT**amount**GT**" . $item->get_quantity() . "**LT**/amount**GT**"; $order_details .= "**LT**upload**GT**" . maybe_serialize( get_field( 'upload', $product_id ) ) . "**LT**/upload**GT**"; // 替换无效的尺码获取代码 $order_details .= "**LT**size**GT**" . $size . "**LT**/size**GT**"; $order_details .= "**LT**groesse**GT**" . $size . "**LT**/groesse**GT**"; $order_details .= "**LT**material**GT**" . maybe_serialize( get_field( 'material', $product_id ) ) . "**LT**/material**GT**"; $order_details .= "**LT**print**GT**" . maybe_serialize( get_field( 'print', $product_id ) ) . "**LT**/print**GT**"; $order_details .= "**LT**variante**GT**" . maybe_serialize( get_field( 'variante', $product_id ) ) . "**LT**/variante**GT**"; $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product_id ) ) . "**LT**/category**GT**"; //add options to the output $order_details .= "**LT**Options**GT**"; $order_details .= "**LT**Option**GT****LT**ID**GT**" . "1996" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_115', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2489" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_117', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2056" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_118', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**/Options**GT**"; $order_details .= "**LT**/item**GT**"; } return $order_details; } ?>
注意事项
- 确认属性slug正确性:可在WooCommerce后台「产品 > 属性」页面查看
GRÖßE属性的slug,确保与代码中的pa_größe一致 - 若需要获取属性标签(如"GRÖßE")而非仅值,可使用
$item->get_formatted_meta_data()方法遍历所有属性
内容的提问来源于stack exchange,提问作者Orko22
相关产品推荐
相关产品推荐

