Home Forums Add-ons Repeater Field Woocommerce Order: get_sub_field array


Woocommerce Order: get_sub_field array

  • Have setup ACF repeater field that stores various amount of tracking numbers in the order. Having 0 success with retrieving this information so need some advice.

    Am using this to put information in subfields and it does the job

    foreach ($base->DocumentLines->DocumentLine as $item) {
        foreach ($item->MiscData as $misc) {
            foreach ($misc->PackageNo as $package) {
                $trackno = (string)$package->TrackingNo;
                update_post_meta("$order_id", $field_rep, $count);
                $sub = $count +1;
                update_sub_field(array($field_key_rep, $sub, $field_key_sub), $trackno, "$order_id");
                $count = $count + 1;
                update_field($field_key, $trackno, "$order_id");

    This works well, but then i need to retrieve this numbers and write em out. They are getting included in an email so need to retrieve the data outside of order.

    Before rebuilding the function to be able to handle multiple numbers i did use a single field and could retrieve the information with

    get_post_meta($order_id, 'tracking', true);

    Feels like i have been trying everything now but got absolutely nothing.

    Image from one of the orders, in this one it’s 10 tracking numbers but it varies from 1 to 20.

  • Hi @mindgames

    You’ve already done the hard part, saving to a repeater. 🙂

    When trying to echo a repeater you should either save it as a variable and loop through it or use acfs ->have_rows functionality.
    You can read all about using the repeater field here:

  • @jonathan The feeling you get when spending 10 hours trying to solve something just to realize that you all the time used ‘trackingno’ instead of the correct ‘trackingNo’

    FML! 😀 , But alright now i got somewhere with this, will post my final code for this once am done.

  • So the final solution for my project was a function looking like this

    function trackingNo($postID) {
        $field_rep = 'trackingNo';
        $field_sub = 'no';
        if (have_rows($field_rep, $postID)) {
                $trackingNo = array();
            // loop through the rows of data
            while (have_rows($field_rep, $postID)):
                // Add to array
                $trackingNo[] = get_sub_field($field_sub);
            $foo = implode('&consignmentId=', $trackingNo);
            $bar = '';
            $value = $bar . $foo;
            return $value;

    If there is any room for improvement in the function please let me know. am pretty weak in PHP.

  • Hi,

    If it works!

    What throws me off a bit is that you’re “glueing” together all of your trackinNos with “&consignmentId=” which if you have multiple numbers should result in something like:

    To me that kind of URL makes no sense as you’d have multiple values for the same GET parameter in the URL.

    Otherwise I think your code looks good. I prefer to not use variables for strings if not needed (or if the code is very long and it’s easier because changing would require a change to many places. I would also suggest to use more descriptive variablenames than foo and bar which are used for generic examples.

    function trackingNo($postID) {
        if ( have_rows('trackingNo', $postID) ) {
            $trackingNo = array();
            // loop through the rows of data
            while ( have_rows('trackingNo', $postID) ): the_row();
                // Add to array
                $trackingNo[] = get_sub_field('no');
            $trackingNoString = implode('&consignmentId=', $trackingNo);
            $base = '';
            $value = $base . $trackingNoString;
            return $value;
  • @jonathan It’s for the Swedish ( inte sÃ¥ hightech ) , to prefill tracking numbers like this

    Thanks for the feedback, PHP is one of my weakest languages so it tend to be a bit messy, will update my code with the things you suggested.

  • No problem!

    Ah yeah I can see that from the base URL 🙂 Haven’t had to work with their “API” yet (fingers crossed).

    Best of luck in your project!

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Woocommerce Order: get_sub_field array’ is closed to new replies.