Get Shipment Id Associated with Product

For a one order that ships in two separate shipments, each shipment gets a different shipment id but each order gets one order id. What I’m trying to do is like this:

Product #1 = shipment ID #55555
Product #2 = shipment ID #77777

My code so far gets me this:

Product #1 = shipment ID #55555
Product #2 = shipment ID #55555
Product #1 = shipment ID #77777
Product #2 = shipment ID #77777

It’s the closest I’ve come so far.

Here is my code:

[code]

<?php foreach ($_results as $shipid => $_result){ foreach($shipments as $shipment){ foreach ($shipment->getAllItems() as $_product){ } echo $_product->getName() . $shipid . "
"; } } ?>[/code]

What I’m I doing wrong?

Thanks

Try the following:

<?php 

foreach ($_results as $shipid => $shipment_S )
{
  foreach($shipments_S as $shipment)
  {
     foreach ($shipment->getAllItems() as $_product)
     {
        echo $_product->getName() . $shipid . "<br/>";
     }
  }
} 

?>

That makes my screen go blank.

In your original code, the echo isn’t inside the third loop, is that intentional? I’m surprised it doesn’t give you an error, as you’re referencing $_product which only exists inside that loop.

When I put it in the third loop like this:

[code]

<?php foreach ($_results as $shipid => $_result){ foreach($shipments as $shipment){ foreach ($shipment->getAllItems() as $_product){ } } echo $_product->getName() . $shipid . "
"; } ?>[/code]

It outputs this:

Product #2 = shipment ID #77777
Product #2 = shipment ID #55555

That’s not in the third loop, it’s now in the first loop. I meant

<?php 
  foreach ($_results as $shipid => $_result){   // open first loop
    foreach($shipments as $shipment){  // open second loop
       foreach ($shipment->getAllItems() as $_product){  // open third loop
         echo $_product->getName() . $shipid . "<br/>";
	}  // close third loop 
    }  // close second loop 
}   // close first loop
?>

OK try this:

 <?php 

error_reporting(-1); 
ini_set('display_errors', 'true');
function fred( $val )
{
echo '<pre>'; print_r( $val ); echo '</pre>';
}

fred($_results);
foreach ($_results as $shipid => $shipment_S )
{
  fred( $shipment_S);
  foreach($shipment_S as $shipment)
  {
    fred( $shipment);
    foreach ($shipment->getAllItems() as $_product)
     {
         fred( $_product);
        echo $_product->getName() . $shipid . "<br/>";
     }
  }
} 

Edit:
Just noticed an incorrectly named $shipments_S in my previous pos :frowning:

When I put it in the third loop like you suggest it gives me this:

Product #1 = shipment ID #55555
Product #2 = shipment ID #55555
Product #1 = shipment ID #77777
Product #2 = shipment ID #77777

@John_Betong

The result of your code:

[code]Array
(
[37006] => Array
(
[0] => Array
(
[title] => FedEx
[number] => 782565016659
)

    )

[37014] => Array
    (
        [0] => Mage_Shipping_Model_Tracking_Result_Status Object
            (
                [_data:protected] => Array
                    (
                        [carrier] => usps
                        [carrier_title] => USPS
                        [tracking] => 9400109699937421730979
                        [track_summary] => Your item was delivered in or at the mailbox at 10:48 am on March 19, 2016.
                    )

                [_hasDataChanges:protected] => 1
                [_origData:protected] => 
                [_idFieldName:protected] => 
                [_isDeleted:protected] => 
                [_oldFieldsMap:protected] => Array
                    (
                    )

                [_syncFieldsMap:protected] => Array
                    (
                    )

            )

    )

)
Array
(
[0] => Array
(
[title] => FedEx
[number] => 782565016659
)

)
Array
(
[title] => FedEx
[number] => 782565016659
)

Fatal error: Uncaught Error: Call to a member function getAllItems() on array in /home/angeecom/public_html/app/design/frontend/default/default/template/trackorder/trackdetail.phtml:126[/code]

That might help - I was about to comment that as we don’t know what’s in any of your data it’s a bit hard to figure out exactly how to get the result you require.

@droopsnoot

I could post the whole page if it would help. As I’ve tried everything I know (which isn’t to much. : )

The bit I can’t get is how the various bits of data relate to each other. You form a loop for the contents of $_results, then within that loop you also loop around the contents of $shipments - what’s in each of those things?

Here’s the whole page. It outputs something like this:

Shipment ID 37006
Fedex #12345678

Shipment ID 37014
USPS #12345678

then under address details etc it shows the items in the order

Product #1
Product #2
Product #3

What I’m trying to do is add the products that where shipped to the tracking number.

[code]

<?php $order = $this->getTrackOrder(); $shipTrack = array(); if ($order) { $shipments = $order->getShipmentsCollection(); foreach ($shipments as $shipment) { $increment_id = $shipment->getIncrementId(); $tracks = $shipment->getTracksCollection(); $trackingInfos = array(); foreach ($tracks as $track) { $trackingInfos[] = $track->getNumberDetail(); } $shipTrack[$increment_id] = $trackingInfos; } } $order_id = $order['increment_id']; $order_item = Mage::getModel("sales/order")->loadByIncrementId($order_id); $items = $order_item->getItemsCollection(); $order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id); $colspan = $rowspan = 2; $shipping_method = $order_details->_data["shipping_description"]; $shipping_address_data = $order_details->getShippingAddress(); $billingAddress = $order_details->getBillingAddress(); $shippcode = $order_details->getShippingAddress()->getCountry(); $billcode = $order_details->getBillingAddress()->getCountry(); $paymentmethod = $order->getPayment()->getMethodInstance()->getTitle(); $payarry = $order->getPayment()->debug(); foreach (Mage::getSingleton('payment/config')->getCcTypes() as $code => $name) { if ($payarry['cc_type'] == $code) { $options = $name; } } $pending = Mage::getStoreConfig('trackorder/trackorder_orderstatus/pending'); $processing = Mage::getStoreConfig('trackorder/trackorder_orderstatus/processing'); $complete = Mage::getStoreConfig('trackorder/trackorder_orderstatus/complete'); $canceled = Mage::getStoreConfig('trackorder/trackorder_orderstatus/canceled'); $closed = Mage::getStoreConfig('trackorder/trackorder_orderstatus/closed'); $pending_class = $processing_class = $complete_pclass = $canceled_class = $closed_class = ""; if ($order->getStatusLabel() == 'Pending') { $pending_class = ''; } if ($order->getStatusLabel() == 'Processing') { $processing_class = ''; } if ($order->getStatusLabel() == 'Complete') { $complete_pclass = ''; } if ($order->getStatusLabel() == 'Canceled') { $canceled_class = ''; } if ($order->getStatusLabel() == 'Closed') { $closed_class = ''; } ?> <?php $_results = $shipTrack; ?>

<?php echo $this->__('Tracking Information'); ?>

<?php if ($pending) echo $pending; else echo $pending_class; ?> <?php if ($processing) echo $processing; else echo $processing_class; ?> <?php if ($complete) echo $complete; else echo $complete_pclass; ?> <?php if ($canceled) echo $canceled; else echo $canceled_class; ?> <?php if ($closed) echo $closed; else echo $closed_class; ?>

<?php echo $this->__("Your Order Status is :") ?> <?php echo $order->getStatusLabel() ?>

<?php error_reporting(-1); ini_set('display_errors', 'true'); function fred( $val ) { echo '
'; print_r( $val ); echo '
'; } fred($_results); foreach ($_results as $shipid => $shipment_S ) { fred( $shipment_S); foreach($shipment_S as $shipment) { fred( $shipment); foreach ($shipment->getAllItems() as $_product) { fred( $_product); echo $_product->getName() . $shipid . "
"; } } } ?> <?php if (sizeof($_results) > 0): ?>
<?php foreach ($_results as $shipid => $_result): ?>
    <?php if ($shipid): ?>
        <h4><?php echo $this->__('Shipment #') . $shipid; ?></h4>
    <?php endif; ?>
    <?php if (sizeof($_result) > 0): ?>
        <?php
        $rowCount = sizeof($_result);
        $counter = 1;
        ?>

        <?php foreach ($_result as $track): ?>
            <table class="data-table track-data-table">
                <col width="15%" />
                <col />
                <tbody>
                    <?php if (is_object($track)): ?>
				
                        <tr>
                            <th><?php echo $this->__('Tracking Number:'); ?></th>
                            <td><?php echo $track->getTracking(); ?></td>
                        </tr>
                        <?php if ($track->getCarrierTitle()): ?>
                            <tr>
                                <th><?php echo $this->__('Carrier:'); ?></th>
                                <td><?php echo $track->getCarrierTitle(); ?></td>
                            </tr>
                        <?php endif; ?>
                        <?php if ($track->getErrorMessage()): ?>
                            <tr>
                                <th><?php echo $this->__('Error:'); ?></th>
                                <td class="error"><?php if ((bool) Mage::getStoreConfig('contacts/contacts/enabled')) : ?><a href="<?php echo $this->getUrl('contacts') ?>" onclick="this.target = '_blank'"><?php echo $this->__('Click here') ?></a><?php
                                        echo $this->__('  to get details or ');
                                    endif;
                                    echo $this->__('email us at ');
                                    ?><a href="mailto:<?php echo Mage::getStoreConfig('trans_email/ident_support/email'); ?>"><?php echo Mage::getStoreConfig('trans_email/ident_support/email') ?></a>

                                </td>
                            </tr>
                        <?php elseif ($track->getTrackSummary()): ?>
                            <tr>
                                <th><?php echo $this->__('Info:'); ?></th>
                                <td><?php echo $track->getTrackSummary(); ?></td>
                            </tr>
                        <?php elseif ($track->getUrl()): ?>
                            <tr>
                                <th><?php echo $this->__('Track:'); ?></th>
                                <td><a href="<?php echo $track->getUrl(); ?>" onclick="this.target = '_blank'"><?php echo $this->__('Click here to get details'); ?></a></td>
                            </tr>
                        <?php else: ?>
                            <?php if ($track->getStatus()): ?>
                                <tr>
                                    <th><?php echo $this->__('Status:'); ?></th>
                                    <td><?php echo $track->getStatus(); ?></td>
                                </tr>
                            <?php endif; ?>

                            <?php if ($track->getDeliverydate()): ?>
                                <tr>
                                    <th><?php echo $this->__('Delivered on:'); ?></th>
                                    <td><?php echo $this->formatDeliveryDateTime($track->getDeliverydate(), $track->getDeliverytime()); ?></td>
                                </tr>
                            <?php endif; ?>

                            <?php if ($track->getSignedby()): ?>
                                <tr>
                                    <th><?php echo $this->__('Signed by:'); ?></th>
                                    <td><?php echo $track->getSignedby(); ?></td>
                                </tr>
                            <?php endif; ?>

                            <?php if ($track->getDeliveryLocation()): ?>
                                <tr>
                                    <th><?php echo $this->__('Delivered to:'); ?></th>
                                    <td><?php echo $track->getDeliveryLocation(); ?></td>
                                </tr>
                            <?php endif; ?>

                            <?php if ($track->getShippedDate()): ?>
                                <tr>
                                    <th><?php echo $this->__('Shipped or billed on:'); ?></th>
                                    <td><?php echo $track->getShippedDate(); ?></td>
                                </tr>
                            <?php endif; ?>

                            <?php if ($track->getService()): ?>
                                <tr>
                                    <th><?php echo $this->__('Service Type:'); ?></th>
                                    <td><?php echo $track->getService(); ?></td>
                                </tr>
                            <?php endif; ?>

                            <?php if ($track->getWeight()): ?>
                                <tr>
                                    <th><?php echo $this->__('Weight:'); ?></th>
                                    <td><?php echo $track->getWeight(); ?></td>
                                </tr>
                            <?php endif; ?>

                        <?php endif; ?>
                    <?php elseif (isset($track['title']) && isset($track['number']) && $track['number']): ?>
                        <!--if the tracking is custom value-->
                        <tr>
                            <th><?php echo ($track['title'] ? $this->escapeHtml($track['title']) : $this->__('N/A')); ?>:</th>
                            <td><?php echo (isset($track['number']) ? $this->escapeHtml($track['number']) : ''); ?></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>

            <?php if (is_object($track) && sizeof($track->getProgressdetail()) > 0): ?>
                <table class="data-table" id="track-history-table-<?php echo $track->getTracking(); ?>">
                    <col />
                    <col width="1" />
                    <col width="1" />
                    <col />
                    <thead>
                        <tr>
                            <th><?php echo $this->__('Location') ?></th>
                            <th><?php echo $this->__('Date') ?></th>
                            <th><?php echo $this->__('Local Time') ?></th>
                            <th><?php echo $this->__('Description') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($track->getProgressdetail() as $_detail): ?>
                            <?php $_detailDate = (isset($_detail['deliverydate']) ? $this->formatDeliveryDate($_detail['deliverydate']) : '') ?>
                            <?php $_detailTime = (isset($_detail['deliverytime']) ? $this->formatDeliveryTime($_detail['deliverytime'], $_detailDate) : '') ?>
                            <tr>
                                <td><?php echo (isset($_detail['deliverylocation']) ? $_detail['deliverylocation'] : ''); ?></td>
                                <td><?php echo $_detailDate ?></td>
                                <td><?php echo $_detailTime ?></td>
                                <td><?php echo (isset($_detail['activity']) ? $_detail['activity'] : '') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>

                <script type="text/javascript">decorateTable('track-history-table-<?php echo $track->getTracking(); ?>');</script>
            <?php endif; ?>
            <?php if ($counter != $rowCount): ?>
            <?php endif; ?>
            <?php $counter++; ?>

        <?php endforeach; ?>
        <!--end for each tracking information-->
    <?php else: ?>
        <p><?php echo $this->__('There is no tracking available for this shipment.'); ?></p>
    <?php endif; ?>

<?php endforeach; ?>
<?php else: ?>
<div><?php echo $this->__('There is no tracking available.'); ?></div>
<?php endif; ?>

Order Information

<?php if ($this->getCanViewOrder() && $this->getCanPrintOrder()) : ?> <?php echo $this->__('here to print', $this->getPrintUrl()) ?> <?php ?> <?php endif; ?>
Date: <?php echo $order_item->getCreatedAtStoreDate(); ?>
Order ID: <?php echo $order_id; ?>
Order Total: <?php echo $order_item->getGrandTotal(); ?>

Payment & Shipping Information

Shipping Method: <?php echo $shipping_method; ?>
Payment Method: <?php echo $paymentmethod; ?>
<?php if ($paymentmethod == 'Credit Card (saved)') { ?>
Credit Card Type: <?php echo $options; ?>
Credit Card Number: <?php echo $payarry['cc_last4']; ?>
Credit Card Owner: <?php echo $payarry['cc_owner']; ?>
<?php } ?>

Shipping Address

Name: <?php echo $shipping_address_data['firstname'] . " " . $shipping_address_data['lastname']; ?>
Delivery Address: <?php echo $shipping_address_data['street']; ?>
Delivery City: <?php echo $shipping_address_data['city']; ?>
State: <?php echo $shipping_address_data['region']; ?>
Country: <?php echo Mage::app()->getLocale()->getCountryTranslation($shippcode); ?>
Postcode: <?php echo $shipping_address_data['postcode']; ?>
Telephone: <?php echo $shipping_address_data['telephone']; ?>

Billing Address

Name: <?php echo $billingAddress['firstname'] . " " . $billingAddress['lastname']; ?>
Delivery Address: <?php echo $billingAddress['street']; ?>
Delivery City: <?php echo $billingAddress['city']; ?>
State: <?php echo $billingAddress['region']; ?>
Country: <?php echo Mage::app()->getLocale()->getCountryTranslation($billcode); ?>
Postcode: <?php echo $billingAddress['postcode']; ?>
Telephone: <?php echo $billingAddress->getTelephone(); ?>
<?php echo $this->getChildHtml('totals'); ?> <?php foreach ($items as $i): $_product = Mage::getModel('catalog/product')->load($i->getProductId()); $options = $i->getProductOptions(); ?>
<?php echo $this->__('Product Name') ?> <?php echo $this->__('Unit Price') ?> <?php echo $this->__('Qty') ?> <?php echo $this->__('SKU') ?> <?php echo $this->__('Sub Total') ?>
<?php echo $i->getName();?>
<?php echo $i->getName();
                    $customOptions = $options['options'];
                    if (!empty($customOptions)) {
                        foreach ($customOptions as $option) {
                            ?>
                            <span class="bottom-align">
                                <?php
                                echo '<b>' . $option['label'] . '</b> :';
                                echo $optionValue = $option['value'];
                                ?></span>                                
                            <?php
                        }
                    }
                    ?>
					</div>
                </td>
                <td><?php echo $this->helper('checkout')->formatPrice($i->getPrice()); ?></td>
                <td><?php echo $i->getQtyOrdered(); ?></td>
                <td><?php echo $i->getSku(); ?></td>
                <td><?php echo $this->helper('checkout')->formatPrice($i->getRowTotal()); ?></td>
            </tr>
        <?php endforeach ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="a-right">
                <small>SubTotal:</small>
            </td>
            <td colspan="2" class="a-right">
                <small><?php echo $this->helper('checkout')->formatPrice($order_item->getSubtotal()); ?></small>
            </td>
        </tr>
        <tr class="shipping">
            <td class="a-right" colspan="4">
                <?php echo $this->__('Shipping & Handling(' . $order_item->getShippingDescription() . ')'); ?>
            </td>
            <td colspan="2" class="last a-right">
                <span class="subtotlal-value">
                    <?php echo $this->helper('checkout')->formatPrice($order_item->getShippingInclTax()); ?>
                </span>
            </td>
        </tr>
        <?php if ($order_item->getDiscountAmount() != 0): ?>
            <tr class="discount">
                <td class="a-right" colspan="4">
                    <?php echo $this->__('Discount'); echo  $order_item->getDiscountDescription() ? '('.$order_item->getDiscountDescription() .')' : "";  ?>
                </td>
                <td class="last a-right">
                    <span class="subtotlal-value">
                        <?php echo $this->helper('checkout')->formatPrice($order_item->getDiscountAmount()); ?>
                    </span>
                </td>
            </tr><?php
        endif;
        ?>
        <tr class="grand_total last">
            <td class="a-right" colspan="4"><strong><?php echo $this->__('Total') ?></strong></td>
            <td colspan="2" class="last a-right">
                <span class="subtotlal-value">
                    <strong><?php echo $this->helper('checkout')->formatPrice($order_item->getGrandTotal()); ?></strong>
                </span>
            </td>
        </tr>
    </tfoot>
</table>
[/code]

OK, so what’s the relationship between the list of items (which I think is loaded into the $items array at the start of the code) and the list of shipments? What stores the list of which item was sent on which shipment? I would suspect it would need to be one of the shipment-related things.

Does this help? http://magento.stackexchange.com/questions/15083/how-to-get-ordered-items-associated-with-tracking-numbers

@droopsnoot

The list of products say

Product #1
Product #2
Product #3

All have above one Order ID

If I ship product Product #1 it will get a Shipment ID for EACH order.

I would like it to be like this:

Shipment number one Shipment ID
Tracking Number, Shipping Company
Product #1 Name, picture etc

Shipment number two Shipment ID
Tracking Number, Shipping Company
Product #2 Name, picture etc
Product #3 Name, picture etc

That link I posted shows you how to list each shipment, each tracking number that is on each shipment, and each product that is on each shipment. It doesn’t seem to tell you how to link a product with a tracking number, which implies that maybe it isn’t possible.

@droopsnoot

Well it’s possible because magento does it in the shipment confirmation email. Here is the email that does it. but I just can’t seem to get it to work in this context.

[code]

<?php $_shipment = $this->getShipment() ?> <?php $_order = $this->getOrder() ?> <?php if ($_shipment && $_order): ?>
<?php $i=0; foreach ($_shipment->getAllItems() as $_item): ?>
<?php if($_item->getOrderItem()->getParentItem()) continue; else $i++; ?>
<tbody<?php echo $i%2 ? ' bgcolor="#F6F6F6"' : '' ?>>
    <?php echo $this->getItemHtml($_item) ?>
</tbody>
<?php endforeach; ?>
<?php echo $this->__('Item') ?> <?php echo $this->__('Sku') ?> <?php echo $this->__('Qty') ?>
<?php endif; ?>[/code]

I hadn’t realised it was Magento until I searched “GetAllItems()” to find that page, and while I’ve heard of it, I have no direct experience of it. Unfortunately the code you posted doesn’t help me to help as I can’t see what the output is. Maybe someone with more (or even some) Magento experience can help.

@droopsnoot

I appreciate your help and everyone’s but this is presenting to be a challenge. I’m going to dig into this a little more.

Thank