如何在数组中存储产品数量

2022-08-30 23:42:36

我有一个数据数组,它将所有产品的购物车中的所有项目合计为一个数字。

我一直在尝试找出一种方法来获取数据数组计数()购物车中所有不同项目的所有不同总数,并将它们呈现在我的数据层中,以逗号分隔。我希望这是有道理的。

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);

以上将返回:

dataLayer = [{“visitorLoginState”:“Logged out”,“visitorType”:“NOT LOGGED IN”,“visitorLifetimeValue”:0,“visitorExistingCustomer”:“No”,“u1”:2,“u2”:[“889623392590”,“889623135517”]

它显示了数据数组中 U1 变量的 2 个乘积和 u2 变量的两个 sku。

如果我对第一个SKU有多个产品,我希望它能分开数量。即.."u1":1,1,3

我会使用或某种类型的多维数组来获得我的需求吗?array_sum


答案 1

如果我对第一个SKU有多个产品,我希望它能分开数量。即..“u1”:1,1,3

我不太清楚sku和product之间的关系,以及数组中的哪些变量指的是哪个变量。我做出以下假设:1)A相当于一个元素2)A是唯一值product$itemssku$itemIds[]

我使用数组键作为跟踪每个唯一 SKU 的简单方法,并使用值来跟踪 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);
        }            
    }

答案 2

从代码来看,它似乎每次只返回一个产品,因为变量被覆盖以获得第一项。我已经添加了一个名为this的新变量,它将返回到输出数组,因为我怀疑这将始终等于1。$parent$itemProductCounts$dataitemProductCounts

<?php
if ($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;

话虽如此,上面的代码应该可以让你接近你需要的东西,你应该用正确的数组替换行,其中包含该SKU的产品计数。$itemProductCounts[$item->getSku()] = count($parent);