Home › Forums › Add-ons › Repeater Field › 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:
http://www.advancedcustomfields.com/resources/repeater/
@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)):
the_row();
// Add to array
$trackingNo[] = get_sub_field($field_sub);
endwhile;
$foo = implode('&consignmentId=', $trackingNo);
$bar = 'http://url.com/tracktrace/TrackConsignments_do.jsp?&consignmentId=';
$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:
etc.
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.
<?php
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');
endwhile;
$trackingNoString = implode('&consignmentId=', $trackingNo);
$base = 'http://url.com/tracktrace/TrackConsignments_do.jsp?&consignmentId=';
$value = $base . $trackingNoString;
return $value;
}
}
?>
@jonathan It’s for the Swedish posten.se ( inte sÃ¥ hightech ) , to prefill tracking numbers like this https://posten.se/tracktrace/TrackConsignments_do.jsp?&consignmentId=916355578312&consignmentId=916355573216&consignmentId=91635557310&consignmentId=91635547833&consignmentId=916332157847&consignmentId=916331257855&consignmentId=
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!
The topic ‘Woocommerce Order: get_sub_field array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.