phpwordpresswoocommerceordersproduct-variations

How to get the variable product size information from Woocommerce orders?


I am using PHP code in the plugin ‘WP ALL EXPORT PRO’ and ACF 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. The code works great.

Unfortunately, only one information is not yet retrieved for the variable products T-SHIRTS / HOODIES / SWEATER: The ordered size of the variable product (variant sizes XS / S / M / L / XL / XXL). The sizes are created as ‘ATTRIBUTE’ in Woocommerce and should be integrated in the XML code structure. OVERVIEW

Product Attribute settings for size ("GRÖßE" with Values XS/S/M/L/XL/XXL):

Product Attribute setting

USED CURRENT CODE IN XML EDITOR / WP ALL EXPORT.

<?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 CURRENT PHP CODE IN FUNCTION EDITOR / WP ALL EXPORT.

<?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;
}

?>

How I can retrieve the "Size" value from the order item (product variation)?

This is the related code line (that is not working):

$order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product_id ) ) . "**LT**/size**GT**";

Solution

  • To get the size information from order items (product variation), you can simply use the WC_Product_Variation method get_attribute() from the product object.

    Based on your Product Attribute setting screenshot for Size, you can use the taxonomy which always starts with "pa_" + the slug, so it will be "pa_groesse" in your case, OR, you can also use the slug "groesse".

    So in your current code, try to replace the following line:

    $order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product_id ) ) . "**LT**/size**GT**";
    

    with (where "pa_groesse" is the attribute taxonomy for "Size"):

    if ( $size_value = $product->get_attribute('pa_groesse') ) {
        $order_details .= "**LT**size**GT**" . $size_value . "**LT**/size**GT**";
    }
    

    Or also (where "groesse" is the attribute slug for "Size"):

    if ( $size_value = $product->get_attribute('groesse') ) {
        $order_details .= "**LT**size**GT**" . $size_value . "**LT**/size**GT**";
    }
    

    It should work.