Eshop购物车中商品的总成本

时间:2013-11-20 22:28:50

标签: php wordpress shopping-cart

我想知道是否可以在我的Wordpress Eshop中显示所有项目的总费用,如下图所示。

enter image description here

我目前有一个函数可以返回所有项目的总费用,我只需要包含额外的选项费用,用户可以在将项目添加到购物车之前选择,这是我添加到我的{ {1}}:

functions.php

我使用// Return the total cost in cart function get_myeshop_cart_itempricetotal(){ global $blog_id; $total_price = 0; if(isset($_SESSION['eshopcart'.$blog_id])) { $item_array = $_SESSION['eshopcart'.$blog_id]; foreach($item_array as $item) { $price = $item['qty'] * $item['price']; $total_price = $total_price + $price; } } return $total_price; } // Display cost function display_my_cart_cost() { $cart_cost = get_myeshop_cart_itempricetotal(); if ( $cart_cost > 0 ) print '£' . $cart_cost; else echo __('0','theme'); } 在我的模板中调用此函数。

我不太了解 PHP ,是否可以修改上述功能以返回所有购物车项目的费用?

1 个答案:

答案 0 :(得分:1)

最好不要修改插件附带的功能。它可能会产生错误或冲突 - 如果现在不在将来的升级中。

如函数get_myeshop_cart_itemcount()所示 - 我认为您可以通过循环浏览购物车中的商品来获取每件商品的价格。您只需要知道保存价格数据的列的标签。盲目猜测它可能被称为“价格”。

类似下面的代码 -

function get_myeshop_cart_itempricetotal(){
    global $blog_id;
    $total_items_in_cart = 0;
        if(isset($_SESSION['eshopcart'.$blog_id])) {
            $item_array = $_SESSION['eshopcart'.$blog_id];
            foreach($item_array as $item) {
                   $price = $item['qty'] * $item['price']; //Check if column is called 'price'
                   $total_price += $price;
            }
        }
    return $total_price ;
}
相关问题