WooCommerce导出XML时变量产品ACF字段为空问题求助
问题:WooCommerce变量产品ACF字段无法通过WP All Export Pro导出
我需要从WooCommerce店铺筛选包含指定简单/变量产品(带ACF信息)的订单,导出XML给代发货合作伙伴。目前使用WP ALL EXPORT PRO + ACF插件,XML能正常筛选订单和产品,但仅简单产品的ACF字段可导出,变量产品的ACF字段全部为空。已配置ACF字段组“DROPSHIPPING DDF DETAILS”及WooCommerce变量产品属性,仍无法解决。
当前代码
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 function my_get_order_items($Bestell_id) { // Check if the Bestell ID is valid if (!$Bestell_id) { return false; } // Fetch the order $order = wc_get_order($Bestell_id); if (!$order) { return false; } // Initialize the string to store order details $order_details = ""; // Loop through each order item foreach ($order->get_items() as $item_id => $item) { // Get the product object $product = $item->get_product(); // Skip if there's no product associated with the item if (!$product) { continue; } if (!(strpos($item->get_name(), 'KERAMIKTASSE') !== false || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false || strpos($item->get_name(), 'T-SHIRT') !== false)) { continue; } $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->get_id() ) ) . "**LT**/upload**GT**"; $order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product->get_id() ) ) . "**LT**/size**GT**"; $order_details .= "**LT**groesse**GT**" . maybe_serialize( get_field( 'produktgröße', $product->get_id() ) ) . "**LT**/groesse**GT**"; $order_details .= "**LT**material**GT**" . maybe_serialize( get_field( 'material', $product->get_id() ) ) . "**LT**/material**GT**"; $order_details .= "**LT**print**GT**" . maybe_serialize( get_field( 'print', $product->get_id() ) ) . "**LT**/print**GT**"; $order_details .= "**LT**variante**GT**" . maybe_serialize( get_field( 'variante', $product->get_id() ) ) . "**LT**/variante**GT**"; $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product->get_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->get_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->get_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->get_id() ) ) . "**LT**/Value**GT****LT**/Option**GT**"; $order_details .= "**LT**/Options**GT**"; $order_details .= "**LT**/item**GT**"; } return $order_details; } ?>
解决方案
问题核心:变量产品的ACF字段通常存储在产品变体ID上,而非父产品ID。原代码直接使用$product->get_id(),对于变体产品可能未正确指向存储ACF字段的ID。
修改函数编辑器中的代码,添加变体判断逻辑,确保获取正确的产品ID:
<?php function my_get_order_items($Bestell_id) { if (!$Bestell_id) { return false; } $order = wc_get_order($Bestell_id); if (!$order) { return false; } $order_details = ""; foreach ($order->get_items() as $item_id => $item) { $product = $item->get_product(); if (!$product) { continue; } if (!(strpos($item->get_name(), 'KERAMIKTASSE') !== false || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false || strpos($item->get_name(), 'T-SHIRT') !== false)) { continue; } // 关键修改:获取正确的产品ID // 如果ACF字段存储在变体上,用变体ID;如果存储在父产品上,替换为$product->get_parent_id() $product_id = $product->is_type('variation') ? $product->get_id() : $product->get_id(); $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**"; // 所有get_field的第二个参数替换为$product_id $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**"; $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; } ?>
额外检查项
- 确认ACF字段组的位置规则:确保字段组设置为应用到**产品变体(Product Variation)**类型,而非仅父产品。
- 测试单个变体产品:在WooCommerce后台查看变体的ACF字段是否已正确填写值。
内容的提问来源于stack exchange,提问作者Orko22
相关产品推荐
相关产品推荐

