Jump to content

Can someone assist with this foreach loop?


danjapro

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);
    }
}
```
	

Link to comment
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>"; }
	 
	

Link to comment
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);
        }
    }

Link to comment
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.

Link to comment
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);    }}
	 
	

Link to comment
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);
        }
        
    }
 
	

Link to comment
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>";
        //}
    }

 

Link to comment
Share on other sites

  • 2 weeks later...

I was able to Get back the Array, but when the Second email kicks-off, still returning the same tracking number from the array and not going to the next(). Can someone tell me if there is a way to if check current(), then go to next()

 

if (!$isOrderShipped) {    return false;} else {    if (count($items) > 1 && count($trackingNumbers) >= 1) {        echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . next($trackingInfoInEmail);        next($trackingInfoInEmail);    } else if(count($items) == 1) {        if (count($trackingNumbers) == 1) {            echo "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . next($trackingInfoInEmail);            next($trackingInfoInEmail);        } else if (count($trackingNumbers) > 1){            echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . next($trackingInfoInEmail);            next($trackingInfoInEmail);        } else if(count($trackingNumbers)==0){            echo  "Your Order has been Shipped with" . $displayShippingMethod;        }    } else {        echo  "Your Order has been Shipped with" . $displayShippingMethod;    }}
	

Link to comment
Share on other sites

    $trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
      
      ...
    
    if (count($trackNos)) {
        for ($i=0; $i< count($trackNos); $i++) {
            $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
            $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
        }
    }

In the code above you have got a value for $trackNo.

In the loop you are putting that same value into each element of the array.

EDIT: Try doing a bit of debugging, like

echo '<pre>', print_r($trackingNumbers, 1), '</pre>';

to see what is in your arrays

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.