簡體   English   中英

回顯購物車以及添加到購物車中的商品數量

[英]Echo cart with the number of quantity of items that are added to cart

我一直在關注ecom網絡教程,並且已經多次更改了代碼,但仍然沒有正確執行。問題是根據產品數量而不是產品ID來更改購物車旁邊回顯的編號。

圖片示例: 在此處輸入圖片說明

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;
       }
    }
}
?>

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

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>

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>

說明文件:

  • array_sum- http: //php.net/manual/zh/function.array-sum.php
  • array_column- http: //php.net/manual/zh/function.array-column.php

補充筆記:

action.php中的代碼非常令人擔憂。 似乎缺乏用戶輸入清理功能。 我建議研究數據清理以提高安全性。

您可以在Magento后端中更改設置,以獲取商品的總數量,而不是不同產品的數量:

  1. 系統>配置>銷售>結帳。
  2. 我的購物車鏈接>顯示購物車摘要。
  3. 將此設置為:顯示購物車中的項目數。

這適用於默認的Magento主題,因此您可以使其適合您自己的主題。

您也可以嘗試以下操作:

# 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 ,有其他方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM