I tried in many ways to filter out specific orders from WooCommerce that contain relevant simple & variable products (with ACF information) and then export them via XML to my fulfillment partner.
For that, I am using WP ALL EXPORT PRO and ACF plugins with a custom code:
USED IN XML EDITOR of WP ALL EXPORT PLUGIN
<?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>
USED IN FUNCTION PHP EDITOR OF WP ALL EXPORT PLUGIN
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;
}
This code works well so far and all relevant orders and all relevant products are filtered. The only problem I have is that only the ACF field information for simple products is exported. For variable products all ACF fields remain empty.
How I can finally transfer variable products + ordered product variants via xml – analogue to "simple products"?
How do I have to change my PHP code and/or ACF settings?
To solve the issue for your variable products where the ACF fields are empty, it requires using the parent variable product ID with ACF get_field()
function, instead of the product variation ID.
Also, there are some unnecessary things in your code.
Try the following PHP replacement code:
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 ID or the parent product 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**";
$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;
}
Now the ACF fields should no more be empty on variable products.