Php question

Hi,
Been trying for hours but I can’t get this. The value in $supplier_code below outputs a value for each item. That value can be the same for some items or different.

example:

102
103
102
102

Here’s how echo $supplier_code will output it:
102103102102

What I’m trying to get is how many different supplier_code(s) there are and echo only them. Like with the numbers above I want it to echo only:
102
103

foreach ($itemsCollection as $item){
   $product = Mage::getModel('catalog/product')->load($item->getProductId());
   $supplier_code = $product->getAttributeText('supplier_code');

echo $supplier_code;

}

Thank you

How does it it look if you append a carriage return, line feed to the echo?

If I do this:

echo $supplier_code;
echo "<br />";

102<br/>103<br/>102<br/>102<br/>

in the source code.

And as far as displaying stacked instead of in a line, that works?

if so, that leaves the issue of removing (and possibly sorting?) relipcate values. I think you’re going to need an array somewhere so you can use something like array_unique

Well what I’m trying to do is something like this:

foreach supplier to get the shipping cost (another variable)

maybe something like this:

foreach supplier{
echo $shippingcost;
}

The way its doing now is it gets the shipping cost multiple times. Its okay as long as they buy something from different suppliers but if they buy multiple items from the same supplier the shipping gets repeated.

It’s getting more complicated than I thought starting out. : )

Coding does have a way of doing that :wink:

If this is for more than simply massaging output, then it may be worthwhile to look more closely at the Mage class. I am unfamiliar with Mage, but I would hope there was a way to get values directly from “suppliers” (where there would be one each) and not need to get them from “products” (where there could be multiple occurrences).

Yes maybe so. I’ll check into it. Thanks

$uniqueSupplierCodes = [];
foreach ($itemsCollection as $item){
   $product = Mage::getModel('catalog/product')->load($item->getProductId());
   $supplier_code = $product->getAttributeText('supplier_code');
   if(!in_array($supplier_code,$uniqueSupplierCodes)) {
      $uniqueSupplierCodes[] = $supplier_code;
   }
}
print_r($uniqueSupplierCodes);

Ya ZooKeeper has the idea. Don’t echo the supplier code immediately, just store it and build up another array of those values.
Then there are lots of techniques to filter an array to unique values, just Google that one.

Finally, echo the unique array.

That works. But how can I add the values together? I’ve been trying everything I can find but nothing is working. Here’s a couple things I tried but didn’t work:

   if(!in_array($supplier_code,$uniqueSupplierCodes)) {
      $uniqueSupplierCodes[] = $supplier_code;
	  $a = compact($supplier_shipping_cost);
	  echo $a;
	  print_r($a);
   }
    if(!in_array($supplier_code,$uniqueSupplierCodes)) {
      //$uniqueSupplierCodes[] = $supplier_code;
	  $a = compact($supplier_shipping_cost);
	  $supplier_shipping_cost = (array)$arr;
	  print_r($arr);
   }
    if(!in_array($supplier_code,$uniqueSupplierCodes)) {
      //$uniqueSupplierCodes[] = $supplier_code;
	  $supplier_shipping_cost;
	  $total_supplier_shipping_cost += $total_shipping_cost;
	  echo $total_supplier_shipping_cost;
   }

thank you

Where do $supplier_shipping_cost and $total_shipping_cost coming from? Those variables are not in any of your previous snippets.

I have those actually in another post. I’ve been working on this for a while:)

Here is my code so far:

 <?php
    $order = Mage::getSingleton('sales/order');
    $order->loadByIncrementId(
      Mage::getSingleton('checkout/session')->getLastRealOrderId()
    );
    $totalData = $order->getData();
    $allitems  = $order->getAllVisibleItems();
    $ShippingAmount = $order->getShippingAmount();
    $itemsCollection = $order->getItemsCollection();
    $total_ordered_items = $order->getData('total_qty_ordered');
    $num_of_ordered_items = 0;
    $supplier_shipping_cost = 0;
    $totalprofit = 0;
$uniqueSupplierCodes = [];
foreach ($itemsCollection as $item){
   $product = Mage::getModel('catalog/product')->load($item->getProductId());
   $supplier_shipping_cost = $product->getData('supplier_shipping_cost');
   $supplier_code = $product->getAttributeText('supplier_code');
   $cost = $product->getData('cost');
   $price = $product->getFinalPrice();
   $num_of_ordered_items = number_format((float)$item->getQtyOrdered());
   $total_cost = $cost * $num_of_ordered_items; 
   $total_price = $price * $num_of_ordered_items;
   $subtotal_profit = $total_price - $total_cost;
   $profit_foreach_group = $subtotal_profit - $supplier_shipping_cost;
   $totalprofit += $profit_foreach_group;
    if(!in_array($supplier_code,$uniqueSupplierCodes)) {
      //$uniqueSupplierCodes[] = $supplier_code;
	 echo  $supplier_shipping_cost;
	  $total_supplier_shipping_cost += $total_shipping_cost;
	  echo $total_supplier_shipping_cost;
   }
}
?>

You can see that $totalprofit += $profit_foreach_group; works in this code but it doesn’t work in the if statement.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.