我有一個數據數組,將所有產品中的所有商品作爲一個數字進行彙總。如何將產品數量存儲在一個陣列中
我一直在想辦法獲得一個數據數組count()所有不同的總數的購物車中的所有不同的項目,並讓他們呈現在我的數據層逗號分隔。我希望這是有道理的。
if ($order->getId()) {
$items = $order->getAllVisibleItems();
$itemIds = array();
$itemNames = array();
$itemPrices = array();
$itemMargins = array();
$itemTypes = array();
$itemGenders = array();
$itemSports = array();
$itemCategoryIds = array();
$itemCategoryNames = 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'];
}
// # Products
$data['u1'] = count($items);
上面將返回:
數據層= [{ 「visitorLoginState」: 「註銷」, 「visitorType」: 「沒有登錄」, 「visitorLifetimeValue」:0 「visitorExistingCustomer」: 「否」,「u1」:2,「u2」:[「889623392590」,「889623135517」]
它顯示了總共2個產品的U1變量和兩個sku的u2變量的數據陣列。
如果我有多個產品的第一個sku我希望它分開的數量。即.. "u1":1,1,3
我會使用array_sum
或某種類型的多維數組來獲得我的需求嗎?
是否有必要將所有這些數據存儲在一個巨大的多嵌套數組中?充其量,這很難遵循。如果將其分解到更原子級別,您可能會有更輕鬆的時間。 – Matt1776
@ Matt1776,我願意提供建議或任何其他方式獲取數據。 – thismethod
我提出了一些重構,我不知道你的問題或數據是否足以提出這樣的建議,除了你可能想要考慮使用多個數組或哈希/地圖/字典對象或者只是嵌套較少的數據。當它嵌套在一個對象內時,跟蹤數據變得非常困難。重新考慮如何存儲和檢索數據,解決方案將變得更加清晰。 – Matt1776