php – 如何将产品数量存储在数组中

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

我一直试图找出一种方法来获取数据数组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);

以上将返回:

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

它显示了U1变量的两个产品和数据数组中的u2变量的两个sku。

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

我会使用array_sumor某种类型的多维数组来获取我的需求吗?

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

我不清楚的是,sku和产品之间的关系以及你的数组中的哪些变量是指哪个。我做出以下假设:
1)产品相当于一个$ items元素
2)sku是一个唯一的$ 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);
        }            
    }
http://stackoverflow.com/questions/40498285/how-to-store-product-quantities-in-an-array

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:php – 如何将产品数量存储在数组中