AfromanJ
AfromanJ

Reputation: 3932

Eshop total cost of items in cart

I am wondering if it is possible to display the total cost of all the items in my Wordpress Eshop like the below image.

enter image description here

I currently have a function that returns the total cost off all item, I just need to include the extra option costs which can be selected by a user before adding the item to the cart, here is the function I added to my 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');
}

I am calling this function within my template using <?php display_my_cart_cost();?>.

I do not know much PHP, is it possible to just modify the above function to return the cost of all cart items?

Upvotes: 0

Views: 375

Answers (1)

TR1
TR1

Reputation: 323

It's better to NOT modify the functions that come with the plugin. It might create bugs or conflicts - if not now in a future upgrade.

As shown in the function get_myeshop_cart_itemcount() - I think you can get the price of each item by looping through the items in the cart. You would just need to know the label for the column holding the price data. A blind guess is that it might be called "price".

Something like the code below -

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

Upvotes: 1

Related Questions