Jump to content
danjapro

Can someone assist with this foreach loop?

Recommended Posts

All I am Trying to do is Loop thru my Array fo results and out each one seperate if there is more than 1 data point:

Trying to display each of tracking # as separate line:

 getIsOrderShipped($_order->getId()); // Is oorder shipped or not
	```
$items = $_order->getAllVisibleItems();
$trackingNumbers = [];
	$trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
$trackNos = explode(",", $trackNo);
if (count($trackNos)) {
    for ($i=0; $i< count($trackNos); $i++) {
        $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
        $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
    }
}
	$trackingInfoInEmail = [];
$i = 0;
if (count($trackingNumbers) > 1) {
    foreach($trackingNumbers as $trackingNumber) {
        $i++;
        $trackingInfoInEmail[] = "<a href='" . $trackingNumber[$i]['url'] . "' target='_blank'>" . $trackingNumber[$i]['track_no'] . "</a>";
        return false;
    }
	}else{
        $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[]['url'] . "' target='_blank'>" . $trackingNumbers[]['track_no'] . "</a>";
}
$displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name
	if (!$isOrderShipped) {
    return false;
} else {
    if (count($items) > 0) {
        echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
    } else  {
        echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
    }
}
```
	

Share this post


Link to post
Share on other sites

As you "return false" a couple of times, I assume this code is within a function.

Are you aware that all processing within the function will cease on encountering a "return" statement?

Where is

$isOrderShipped

defined?

Share this post


Link to post
Share on other sites

IT worked before.

 

This is the Code base that is causing issues:

$trackingInfoInEmail = [];$i = 0;if (count($trackingNumbers) > 1) {    foreach($trackingNumbers as $trackingNumber) {        $i++;        $trackingInfoInEmail = "<a href='" . $trackingNumber[$i]['url'] . "' target='_blank'>" . $trackingNumber[$i]['track_no'] . "</a>";      }   }else{        $trackingInfoInEmail = "<a href='" . $trackingNumber[$i]['url'] . "' target='_blank'>" . $trackingNumber[$i]['track_no'] . "</a>"; }
	 
	

Share this post


Link to post
Share on other sites

It worked before what?

What issues is it causing?

What is or isn't happening?

You need to give us some information to go on. We are not looking over your shoulder watching what is happening, you need to tell us.

Share this post


Link to post
Share on other sites

I changed this, it works to a degree.
Now it is returning duplicated tracking numbers and each tracking number.

I need help, just get each one tracking number seperate. 

    $isOrderShipped = $adminHelper->getIsOrderShipped($_order->getId()); // Is oorder shipped or not
	    $items = $_order->getAllVisibleItems();
    $trackingNumbers = [];
	    $trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
    $trackNos = explode(",", $trackNo);
    if (count($trackNos)) {
        for ($i=0; $i< count($trackNos); $i++) {
            $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
            $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
        }
    }
	    $trackingInfoInEmail = [];
    if (count($trackingNumbers) > ) {
        for ($i=0; $i < count($trackingNumbers); $i++) {
            $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[$i]['url'] . "' target='_blank'>" . $trackingNumbers[$i]['track_no'] . "</a>";
        }
    }
    $displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name
    if (!$isOrderShipped) {
        return false;
    } else {
        if (count($items) > 1) {
            echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
        } else  {
            echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
        }
    }

Share this post


Link to post
Share on other sites
8 minutes ago, danjapro said:

if (count($trackingNumbers) > ) {

That line won't work. Do you have error reporting turned on?

17 minutes ago, danjapro said:

Now it is returning duplicated tracking numbers and each tracking number.

As I have no idea what your input data looks like, that doesn't mean much to me.

Share this post


Link to post
Share on other sites

I am having a hard time with this piece of code. 

 

Can you help me please.

It still return, duplicate. Tracking No#s.

 

$isOrderShipped = $adminHelper->getIsOrderShipped($_order->getId()); // Is oorder shipped or not$items = $_order->getAllVisibleItems();$trackingNumbers = [];$trackNo = $adminHelper->getOrderTrackingNo($_order->getId());$trackNos = explode(",", $trackNo);if (count($trackNos)) {    for ($i=0; $i< count($trackNos); $i++) {        $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);        $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);    }}$trackingInfoInEmail = [];if (count($trackingNumbers) > 0) {    for ($i=0; $i < count($trackingNumbers); $i++) {        $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[$i]['url'] . "' target='_blank'>" . $trackingNumbers[$i]['track_no'] . "</a>";    }}$displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Nameif (!$isOrderShipped) {    return false;} else {    if (count($items) > 1) {        echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);    } else  {        echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);    }}
	 
	

Share this post


Link to post
Share on other sites

Struggling with this piece of code:

Still getting duplicate Tracking #s:

	    $isOrderShipped = $adminHelper->getIsOrderShipped($_order->getId()); // Is oorder shipped or not
	    $items = $_order->getAllVisibleItems();
    $trackingNumbers = [];
	    $trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
    $trackNos = $adminHelper->getOrderTrackingNo($_order->getTrackingNumber());
    //$trackNos = explode(",", $trackNo);
    if (count($trackNos)) {
        for ($i=0; $i< count($trackNos); $i++) {
            $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
            $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
        }
    }
	    $trackingInfoInEmail = [];
    if (count($trackingNumbers) > 0) {
        //for ($i=0; $i < count($trackingNumbers); $i++) {
            $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[$i]['url'] . "' target='_blank'>" . $trackingNumbers[$i]['track_no'] . "</a>";
        //}
    }
    $displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name
	    if (!$isOrderShipped) {
        return false;
    } else {
    
        $i = 0;
        if (count($items) > 1) {
            echo "You may receive multiple shipments and emails.";
            foreach($items as $item) {
                $i++;
                echo "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
             }
        } else {
              echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
        }
        
    }
 
	

Share this post


Link to post
Share on other sites

What value are you expecting fot $i in this code (now the for loop is removed)?

 if (count($trackingNumbers) > 0) {
        //for ($i=0; $i < count($trackingNumbers); $i++) {
            $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[$i]['url'] . "' target='_blank'>" . $trackingNumbers[$i]['track_no'] . "</a>";
        //}
    }

 

Share this post


Link to post
Share on other sites

$i was to loop thru ea trackin# to count each one. But I just need it to get each 1 tracking per ea email sent.

not all tracking at same time in same email. thru an array,. dont need it that way

Share this post


Link to post
Share on other sites

I know what $i was doing before you removed the loop. The point is there is no loop now , therefore $i does not have a valid value - yet you are still using it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.