1

I am trying to get some values from an array. my code below i need to get a comma separated values the values from the sku KEY, i am just not sure how to do it.

Example output I need: sku,sku,sku


$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);

$orderItems = array();
foreach($order->getItemsCollection() as $item)
{
    //$product = Mage::getModel('catalog/product')->load($item->getProductId());
    $row=array();
    $row['sku'] = $item->getProduct()->getData('model');
    $orderItems[]=$row;
}
echo print_r($orderItems);

1

Im not 100% what you are trying to do here, so you may need to give an example. If your intent is to simply collect all of the skus, I would first ver_dump($item->getData()) because your item may already have the sku, in which case your code would look something like:

foreach($order->getItemsCollection() as $item)
{
$orderItems['sku'][] = $item->getSku();
}

If you don't have the sku on your item an need to load the product, you can slightly alter the code to:

foreach($order->getItemsCollection() as $item)
{
$orderItems['sku'][] = $item->getProduct()->getSku();
}

If model and sku are different and you want an array with sku keys and model values, consider this code:

foreach($order->getItemsCollection() as $item)
{
$product = $item->getProduct();
$orderItems[$product->getSku()] = $product->getModel();
}

Again, you may want to just give a clearer example of what you're looking for if I didn't cover it.

EDIT:

Apologies. The way you originally set up your question led me to use a 2D array. This should cover your issue:

 foreach($order->getItemsCollection() as $item) { 
    $orderItems[] = $item->getProduct()->getSku(); 
 } 
 $comma_separated = implode(",", $orderItems); 
 echo $comma_separated;
| improve this answer | |
  • Thank you very much for the response here the second one produced this output: Array ( [sku] => Array ( [0] => skua[1] => skub ) ) when I ran it which is good but i need it to look like this: skua,skub,skuc, – user3931208 Mar 1 '16 at 22:41
  • Can you clarify exactly what you mean? When you say that you need it to be "Sku,sku,sku" do you mean that you mean that you want it to be a comma separated string? – Zach L Mar 1 '16 at 22:44
  • Yes exactly sorry new to php programming – user3931208 Mar 1 '16 at 22:44
  • You have two options, you can keep the code i gave and use implode... php.net/manual/en/function.implode.php or you can just concatenate the string as you go instead of loading it into an array. – Zach L Mar 1 '16 at 22:47
  • Like this? This just outputs ARRAY foreach($order->getItemsCollection() as $item) { $orderItems['sku'][] = $item->getProduct()->getSku(); } $comma_separated = implode(",", $orderItems); echo $comma_separated; – user3931208 Mar 1 '16 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.