I have a data array that totals all the items in the cart for all the products as one number.

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

I've been trying to figure out a way to get a data array count() all the different totals of all the different items in the cart and have them presented in my data layer comma separated. I hope that makes sense.

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

The above will return:

以上将返回:

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

It shows a total of 2 products for the U1 variable and the two sku's for the u2 variable in the data array.

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

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

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

Would i use array_sumor some type of multi-dimensional array to acquire my needs?

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

5 个解决方案

#1


6

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

如果我有第一个sku的多个产品,我希望它分开数量。即..“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 element 2) A sku is a unique $itemIds[] value

我不完全清楚sku和产品之间的关系以及数组中哪些变量引用哪些变量。我做出以下假设:1)产品相当于一个$ items元素2)sku是唯一的$ itemIds []值

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.

我使用数组键作为跟踪每个唯一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);
        }            
    }

更多相关文章

  1. Contenteditable / Jquery / Ajax / PHP / mysql - 无法识别URL
  2. 更改数组键而不更改顺序
  3. Thinkphp中怎么接收GET变量?
  4. php吧字符串直接转换成数组处理
  5. 使用 PHP usort() 通过用户自定义的比较函数对数组进行排序
  6. PHP print_r 转换/还原为数组
  7. 本机PHP函数将授予我直接访问字符串部分而无需创建临时数组的权
  8. 快速使用数组的最近元素来确定新元素是否唯一
  9. 填充PHP数组:首先检查索引?

随机推荐

  1. CyclicBarrier如何使用?
  2. 面试官:如何实现一个乐观锁(小白都能看得懂
  3. volatile如何避免指令重排序?原来使用了内
  4. JDK 中有哪些同步容器?并发容器?
  5. DNS解析域名:发现域名和IP不一致,访问了该
  6. Condition实现等待、唤醒
  7. 今年写技术博文的几点思考
  8. Android(安卓)数据存储---SharedPreferen
  9. java中的编码转化方式都有哪些?(大厂高频面
  10. Java并发原子类有哪些?如何使用?