2017-08-08 81 views
1

我一直在關注一個ecom網絡教程,我一直在更改代碼多次,但仍然沒有得到它的正確。問題是改變數量根據產品數量而不是產品ID旁邊的購物車旁的回聲。回聲推車與添加到購物車中的商品數量的關係

形象的例子: enter image description here

Shop.php代碼:

<a href="#tab2">CART <span class="badge"> 
<?php 
    if(isset($_SESSION["shopping_cart"])) { 
     echo count($_SESSION["shopping_cart"]); 
    } else { 
     echo '0';} 
?> 
</span></a> 

action.php的代碼:

<?php 
if (isset($_POST["product_id"])) { 
    $order_table = ''; 
    $message = ''; 
    if ($_POST["action"] == "add") { 
     if (isset($_SESSION["shopping_cart"])) { 
      $is_available = 0; 
      foreach ($_SESSION["shopping_cart"] as $keys => $values) { 
       if ($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"]) { 
        $is_available++; 
        $_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"]; 
       } 
      } 
      if ($is_available < 1) { 
       $item_array = array(
        'product_id'  => $_POST["product_id"], 
        'product_name'  => $_POST["product_name"], 
        'product_price'  => $_POST["product_price"], 
        'product_quantity' => $_POST["product_quantity"] 
       ); 
       $_SESSION["shopping_cart"][] = $item_array; 
      } 
     } else { 
      $item_array = array(
       'product_id'  => $_POST["product_id"], 
       'product_name'  => $_POST["product_name"], 
       'product_price'  => $_POST["product_price"], 
       'product_quantity' => $_POST["product_quantity"] 
      ); 
      $_SESSION["shopping_cart"][] = $item_array; 
     } 
    } 
} 
?> 
+0

這是一個Magento購物車嗎?我發現它出現在你的標籤中,人們正在回答Magento的答案,但是如果你是從Magento教程中獲得的,我可能會尋找一個新的教程,這不是處理Magento購物車的傳統方式。如果不是Magento購物車,請刪除該標籤,以免得到Magento特定的答案,它們將與您的問題無關。 – Rasclatt

回答

0

你要的數量從每個項目的存儲使用循環的購物車:

function getCartQty() 
    { 
     # If there is nothing in the cart, return 0 
     if(empty($_SESSION['shopping_cart'])) 
      return 0; 
     # Store array 
     $qty = array(); 
     # Loop items in cart 
     foreach($_SESSION["shopping_cart"] as $item){ 
      # Store the quantity of each item 
      $qty[] = $item['product_quantity']; 
     } 
     # Return the sum 
     return array_sum($qty); 
    } 
?> 
<a href="#tab2">CART <span class="badge"><?php echo getCartQty() ?></span></a> 
0

count()將返回數組中元素的數量。在存儲在$ _SESSION數組中的購物車數組上運行時,您正在統計獨特產品的數量。

由於每個產品都有數量值,因此您需要檢索這些數量的總和。這裏是你可以用它來執行計算的函數:

function my_shopping_cart_total_product_count() { 
    $product_count = 0; 

    if (isset($_SESSION['shopping_cart'])) { 
     $product_count = array_sum(array_column($_SESSION['shopping_cart'], 'product_quantity')); 
    } 

    return $product_count; 
} 

在上面的例子中我使用的是array_column獲得包含所有產品數量的數組。然後我通過array_sum運行得到總數。你當然可以簡單地使用foreach循環。

用法:

<a href="#tab2">CART <span class="badge"><?php echo my_shopping_cart_total_product_count(); ?></span></a> 

文檔:

附加說明:

action.php中的代碼非常值得關注。似乎缺乏用戶輸入消毒。我建議研究數據清理以提高安全性。

0

您可以更改Magento的後端設置,以獲得項目的總量,而不是數量不同的產品:

  1. 系統>配置>銷售>結帳。
  2. 我的購物車鏈接>顯示購物車摘要。
  3. 將其設置爲:顯示購物車中的物品數量。

這適用於默認的Magento主題,所以你可以使它適用於你自己的主題。

0

你也可以試試這個:

# return shopping cart items count means how many sku add to shopping cart 
Mage::helper('checkout/cart')->getItemCount() 

# return shopping cart items summary (suppose you add sku1 6 qty and sku2 3 qty = total 9 qty return) 
Mage::helper('checkout/cart')->getSummaryCount() 

來源:https://magento.stackexchange.com/a/23496/46249

注意:不要在Magento使用$_SESSION,有替代方法。

相關問題