简体   繁体   English

回显购物车以及添加到购物车中的商品数量

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

I have been following a ecom web tutorial and I have been changing the code many times but still did not get it right.The problem is to change the number that echo next to the cart based on quantity of product instead of product id. 我一直在关注ecom网络教程,并且已经多次更改了代码,但仍然没有正确执行。问题是根据产品数量而不是产品ID来更改购物车旁边回显的编号。

image example: 图片示例: 在此处输入图片说明

Shop.php code: 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 code: 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;
       }
    }
}
?>

You want to store the quantities from each item from the cart using a loop: 您要使用循环存储购物车中每个项目的数量:

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() is going to return the number of elements in an array. count()将返回数组中元素的数量。 When run on your shopping cart array stored in the $_SESSION array, you're counting the number of unique products. 在$ _SESSION数组中存储的购物车数组上运行时,您要计算的是唯一商品的数量。

As each product has a quantity value, you need to retrieve the sum of those. 由于每种产品都有数量值,因此您需要检索这些数量的总和。 Here's a function you can use to perform that calculation: 您可以使用以下函数来执行该计算:

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

In the example above I'm using array_column to get an array containing all of the product quantities. 在上面的示例中,我使用array_column获取包含所有产品数量的数组。 I'm then running that through array_sum to get the total. 然后,我通过array_sum运行它以获取总数。 You could of course simply use a foreach loop. 您当然可以简单地使用foreach循环。

Usage: 用法:

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

Documentation: 说明文件:

Additional notes: 补充笔记:

The code in action.php is quite concerning. action.php中的代码非常令人担忧。 There appears to be a lack of user input sanitization. 似乎缺乏用户输入清理功能。 I'd recommend researching data sanitization in order to improve security. 我建议研究数据清理以提高安全性。

You can change settings in Magento backend to get total quantity of items instead of quantity of different products: 您可以在Magento后端中更改设置,以获取商品的总数量,而不是不同产品的数量:

  1. System > Configuration > Sales > Checkout. 系统>配置>销售>结帐。
  2. My Cart Link > Display Cart Summary. 我的购物车链接>显示购物车摘要。
  3. Set this to: Display number of items in cart. 将此设置为:显示购物车中的项目数。

This works on default Magento theme so you could make it work for your own theme. 这适用于默认的Magento主题,因此您可以使其适合您自己的主题。

You can also try this: 您也可以尝试以下操作:

# 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()

Source: https://magento.stackexchange.com/a/23496/46249 资料来源: https : //magento.stackexchange.com/a/23496/46249

Note: Do not use $_SESSION in Magento, there are alternative methods. 注意:不要在Magento中使用$_SESSION ,有其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM