【问题标题】:PHP using multidimensional arrays with OOPPHP 使用带有 OOP 的多维数组
【发布时间】:2012-09-17 08:53:04
【问题描述】:

我不久前做了一个系统,其中涉及一个基本的购物车。购物车是一个非常简单的 OOP 系统,一次添加 1 个数量的商品。这是将商品添加到购物车的功能。

    function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ 

    if($this->itemqtys[$itemid] > 0)
            { // the item is already in the cart..
      // so we'll just increase the quantity

        $this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
        $this->_update_total();
    } else {
        $this->items[]=$itemid;
        $this->itemqtys[$itemid] = $qty;
        $this->itemprices[$itemid] = $price;
        $this->iteminfo[$itemid] = $info;
    }
    $this->_update_total();
} 

问题在于某些产品有变体(尺寸、颜色等),但是当他们在同一订单中选择不同的变体时,产品没有正确添加 - 所有相同的产品都属于一个变体.

我认为使用多维数组或数组中的数组来产生类似的东西是一个想法:

$this->iteminfo[$itemid][$var];

从产品中获取 var 不是问题,我只是在苦苦思索如何将产品及其变体添加到购物车中,并且只有在再次添加变体时才更新此产品/变体组合的数量。如果添加了不同的变体,它会将产品添加到购物车的新条目中吗?

希望这是有道理的:/ 谢谢

【问题讨论】:

    标签: php oop multidimensional-array shopping-cart


    【解决方案1】:

    这对我来说似乎不是很 OOP。您应该为购物车项目创建一个类,其中包含数量、价格、变化等,然后将此类的对象添加到购物车对象中的 items 数组。

    然后你的方法就变成了这样的东西:

    function add_item($itemid, $qty=1, $variation ="", $price = FALSE, $info = FALSE){ 
    
        // use itemid concatenated with variation 
        // to get a unique key for every item in your cart 
        if(array_key_exists($itemid . $variation, $this->items)){ 
    
            // the item is already in the cart..
            // so we'll just increase the quantity
            $item = $this->items[$itemid];
    
            // this assumes a method in the Item class to add to the quantity
            $item->addToQuantity($qty);
    
            // put the updated item in the cart
            $this->items[$itemid] = $item;
        } else {
            // create a new item object
            $item = new Item();
            $item->setId($itemid);
            $item->setQuantity($qty);
            $item->setPrice($price);
            $item->setInfo($info);
            $item->setVariation($variation);
    
            // add it to the items array using a unique key
            $this->items[$itemid . $variation] = $item;
        }
        $this->_update_total();
    } 
    

    【讨论】:

      【解决方案2】:

      我建议使用单维(al)数组,而不是用普通的旧整数和字符串填充它,而是在其中放置一个对象。这样会更加强大,并且您可以轻松循环以获取所需的任何内容 - 以及将来能够进一步扩展对象。

      这样的事情甚至可以工作:

      class itemToBuy
      {
          public $itemID;
          public $qty;
          public $price;
          public $info;
          public $whoKnowsWhat;
      }
      
      class yourShoppingCart
      {
          function add_item($newItem)
          {
              $itemInserted==false;
              $itemsInCart=count($this->itemqtys);
              for($i=0;$i<$itemsInCart;$i++)
              {
                  if($this->itemqtys[$i]->itemID==$newItem->itemID)
                  {
                      $this->itemqtys[$i]->qty+=$newItem->qty;
                      $itemInserted=true;
                  }
              }
              if(!itemInserted)
              {
                  $this->itemqtys[]=$newItem;
              }
              $this->_update_total();
          } 
      }
      

      然后只传递itemToBuy 的对象实例,而不是你现在传递的数组。

      【讨论】:

        【解决方案3】:

        您可以为每个项目的变量$this-&gt;itemqtys 设置一个变化值,然后您可以检查如下:

        <?php
        // add a extra $var for checking the variations
        function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE, $var=FALSE) {
            if( ( $this->itemqtys[$itemid] > 0 ) && ( $this->itemqtys[$itemid]['var'] == $var ) ) {
                  // item already in cart, update
            } else {
                  // add item to the cart, along with the variation.
            }
        }
        ?>
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-09
          • 1970-01-01
          • 1970-01-01
          • 2016-05-22
          相关资源
          最近更新 更多