Skip to main content
Making it specific to question
Source Link
Zach L
  • 282
  • 1
  • 8

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;

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.

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;
Source Link
Zach L
  • 282
  • 1
  • 8

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.