How to store product quantities in an array How to store product quantities in an array arrays arrays

How to store product quantities in an array


If i have multiple products for the first sku i want it to seperate the quantities. ie.. "u1":1,1,3

It is not exactly clear to me is the relationship between sku and product and which variables in your array refer to which. I make the following presumptions:1) A product is equivalent to one $items element2) A sku is a unique $itemIds[] value

I use the array key as a simple way to keep track for each unique sku and the value to keep track of the product count for the sku.

if ($order->getId()) {    $items = $order->getAllVisibleItems();    $itemIds = array();    $itemNames = array();    $itemPrices = array();    $itemMargins = array();    $itemTypes = array();    $itemGenders = array();    $itemSports = array();    $itemCategoryIds = array();    $itemCategoryNames = array();    // My addition (UPDATE: fixed to the correct variable name)    $uniqueItemIds = array();    /** @var Mage_Sales_Model_Quote_Item $item */    foreach ($items as $item) {        // Get the parent item - it is NOT included in the quote due to        // customizations made by the OrganicInternet module for simple        // product pricing. So I had to come up with another way to get it.        $options = $item->getProductOptions();        $parent = $item->getProduct();        if (array_key_exists('info_buyRequest', $options)) {            if (array_key_exists('cpid', $options['info_buyRequest'])) {                $parentId = $options['info_buyRequest']['cpid'];                $parent = Mage::getModel('catalog/product')->getCollection()                    ->addAttributeToSelect('name')                    ->addAttributeToSelect('season')                    ->addAttributeToSelect('gender')                    ->addAttributeToSelect('sport')                    ->addAttributeToFilter('entity_id', $parentId)                    ->getFirstItem();            }        }        // *******************************        // My addition / changes        $sku = $item->getSku();        $itemIds[] = $sku;        // I don't use this but keep $itemIds for compatibility                    // use the array key to track counts for each sku        if (!isset($uniqueItemIds[$sku])){            $uniqueItemIds[$sku] = 1;   // UPDATE: fixed to start at 1 not 0        } else {            $uniqueItemIds[$sku]++;        }        // *******************************        $itemNames[] = $parent->getName();        $itemPrices[] = $item->getBasePrice() ?: 0;        $itemMargins[] = $this->_calculateMargin($parent, null, $item);        $itemTypes[] = $parent->getAttributeText('season');        $itemGenders[] = $parent->getAttributeText('gender');        $itemSports[] = $parent->getAttributeText('sport') ?: 'Other';        $categories = $this->_getAllCategoryIdsAndNames($item->getProduct());        $itemCategoryIds[] = $categories['id'];        $itemCategoryNames[] = $categories['name'];    }    // show # Products    // "u1":1,1,3 NOTE: this should be a string => "u1":"1,1,3"    $data['u1'] = "";    foreach ($uniqueItemIds as $key => $val)        // show unique skus in u2        $data['u2'][] = $key;        // show counts for each sku in u1        if (strlen($data['u1'] == 0)){            $data['u1'] = (string)$value;        } else {           $data['u1'] .= ("," . $value);        }                }


How about something like...

if ($order->getId()) {    .....    .....    .....    /** @var Mage_Sales_Model_Quote_Item $item */    $sku_based_array = array();    foreach ($items as $item) {        ......        ......        ......        $categories = $this->_getAllCategoryIdsAndNames($item->getProduct());        $itemCategoryIds[] = $categories['id'];        $itemCategoryNames[] = $categories['name'];        if (isset($sku_based_array[$item->getSku()])) {            $sku_based_array[$item->getSku()] = $sku_based_array[$item->getSku()]++;        } else {            $sku_based_array[$item->getSku()] = 1;        }    }    // # Products    $data['u1'] = array_values($sku_based_array);


Looking at the code it looks like it will only every return one product as the $parent variable is overwritten to get a first item. I have added a new variable named $itemProductCounts this will be returned to the output $data array as itemProductCounts I suspect this will always equal one.

<?phpif ($order->getId()) {    $items              = $order->getAllVisibleItems();    $itemIds            = array();    $itemNames          = array();    $itemPrices         = array();    $itemMargins        = array();    $itemTypes          = array();    $itemGenders        = array();    $itemSports         = array();    $itemCategoryIds    = array();    $itemCategoryNames  = array();    $itemProductCounts  = array();    /** @var Mage_Sales_Model_Quote_Item $item */    foreach ($items as $item) {        // Get the parent item - it is NOT included in the quote due to        // customizations made by the OrganicInternet module for simple        // product pricing. So I had to come up with another way to get it.        $options    = $item->getProductOptions();        $parent     = $item->getProduct();        if (array_key_exists('info_buyRequest', $options)) {            if (array_key_exists('cpid', $options['info_buyRequest'])) {                $parentId = $options['info_buyRequest']['cpid'];                $parent = Mage::getModel('catalog/product')->getCollection()                    ->addAttributeToSelect('name')                    ->addAttributeToSelect('season')                    ->addAttributeToSelect('gender')                    ->addAttributeToSelect('sport')                    ->addAttributeToFilter('entity_id', $parentId)                    ->getFirstItem();            }        }        $itemIds[]                          = $item->getSku();        $itemNames[]                        = $parent->getName();        $itemPrices[]                       = $item->getBasePrice() ?: 0;        $itemMargins[]                      = $this->_calculateMargin($parent, null, $item);        $itemTypes[]                        = $parent->getAttributeText('season');        $itemGenders[]                      = $parent->getAttributeText('gender');        $itemSports[]                       = $parent->getAttributeText('sport') ?: 'Other';        $categories                         = $this->_getAllCategoryIdsAndNames($item->getProduct());        $itemCategoryIds[]                  = $categories['id'];        $itemCategoryNames[]                = $categories['name'];        $itemProductCounts[$item->getSku()] = count($parent);    }    // # Products    $data['u1'] = count($items);    $data['itemProductCounts'] = $itemProductCounts;

With that all being said, the code above should get you close to what you need, you should replace the line $itemProductCounts[$item->getSku()] = count($parent); with the correct array with the product counts for that SKU.