Hi,

Currently the cart block shows the total without any discounts applied.

Is it possible to either:

1. Show only the discounted total in the cart block

2. Show the total discounts as well as the undiscounted total that shows at the moment.

3. Show if any discounts are applicable to the products in the cart so we can say "discounts apply" in the cart block.

My client is finding it confusing that you click on a product that says it is $75.00 on sale for $65.00. Once you add it to your cart $75.00 shows as the cart total.

Thanks for your help.

Andrew

CommentFileSizeAuthor
#1 cart_page.jpg91.26 KBryangroe

Comments

ryangroe’s picture

StatusFileSize
new91.26 KB

The latest version of the module should show the same discounts on the cart page as it does on the the product page. Attached is an example showing the discounts in the cart page.

a_lawry’s picture

Thanks for the reply.

I was actually referring to the cart block not the cart page.

Everything is ok on the cart page once you go to it but while you are browsing around the store adding products to the cart the total shown on the cart block is the subtotal (ie it would show $400 in your example).

How can I get the discounts total for the cart block?

Thanks for your help.

mrwhizkid’s picture

I'm also looking for a solution to this issue. It can be confusing for customers when they don't see the accurate amount in the cart.

hackboy2600’s picture

I too am looking for a solution to this, I have tried a couple of times to add a hook into the Discount module but have come up with no luck.
Is there a way to swap the price in the block with the discounted price?
I have tried that once but I don't know if Ubercart was able to recognize the discount field or if i was even using the right one.
If anyone knows the variable name for the discounted price couldn't we just add that into the cart.module file replacing $price?

IT Guy 3000’s picture

Yes, seeing the discount price in the cart block would be great. It's a bit confusing to customers as is. Subscribing.

a_lawry’s picture

I'm now working around this in my theme.

I've done the following:

1. Copied theme_uc_cart_block_content($help_text, $items, $item_count, $item_text, $total, $summary_links) from the uc_cart module into my template.php (and renamed it).

2. Made a new function based on uc_discounts_cart_pane($items) that only returns the total of the discounts. I've called it uc_discounts_cart_block_discounts($items).

3. Edited theme_uc_cart_block_content() in my theme to add a Total discounts line which displays the total.

4. Edited theme_uc_cart_block_content() in my theme to display $total - $total_discounts as the total line.

That's it. Now my cart block is displaying the correct discounted total. I tried displaying the itemised discounts in the cart block as well but the messages were too long.

mikec’s picture

Would you mind sharing your work around, @a_lawry? I tried to do the same thing, but I can't call my created uc_discounts_cart_block_discounts($items) from my renamed theme_uc_cart_block_content(). The call to

$discounts = get_discounts_for_order($order, $errors, $warnings, $messages);

raises an SQL error and returns an empty $discounts array.

Zalatar’s picture

Yes this would be great to see implemented into the module.
If nothing else maybe a_lawry can post some detailed directions for all of us.
Z

a_lawry’s picture

Hi,

Here's what I have done so far.

This is from my template.php my theme is called menkom_jaguar. It is a copy of the function theme_uc_cart_block_content() from the uc_cart module.


function menkom_jaguar_uc_cart_block_content($help_text, $items, $item_count, $item_text, $total, $summary_links) {
  $output = '';

  // Add the help text if enabled.
  if ($help_text) {
    $output .= '<span class="cart-help-text">'. $help_text .'</span>';
  }

  // Add a wrapper div for use when collapsing the block.
  $output .= '<div id="cart-block-contents">';

  // Add a table of items in the cart or the empty message.
  $output .= theme('uc_cart_block_items', $items);

// THIS IS MY NEW FUNCTION TO GET THE TOTAL DISCOUNTS
  $discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents());

  //$output .= $discount_info['body'];

  $output .= "<div class=\"cart-block-discounts-total\"><label>Total discounts:</label><span class=\"uc_price\">" . uc_currency_format($discount_info['
total']) . "</span></div>";

  $output .= '</div>';


  // Add the summary section beneath the items table.
  $output .= theme('uc_cart_block_summary', $item_count, $item_text, $total - $discount_info['total'], $summary_links);

  return $output;
}

And I've also added a new function to the module which you can see in the above code:

This function returns an array with the keys 'total' and 'details'. I've returned 'details' in case I want to put the full details of the discounts into the block. But I found this doesn't look that good at the moment. So you could simplify it to just return the total.


function uc_discounts_cart_block_discounts($items)
{
        global $user;

        //Create phony order object to call to get_discounts_for_order
        $order = new stdClass();
        $order->uid = $user->uid;
        $order->products = $items;

        $errors = array();
        $warnings = array();
        $messages = array();
        //echo "Calculating discounts";
        $discounts = get_discounts_for_order($order, $errors, $warnings, $messages);

        //If there are no discounts, do not alter cart
        if (count($discounts) == 0)
                return array();

        //Calculate subtotal with discounts
        $subtotal = 0;
        foreach ($items as $item)
        {
                //$subtotal += $item->price * $item->qty;
                $subtotal += get_discounts_taxed_price($item->price, $item) * $item->qty;
        }
        $total_discount_amount = 0;
        foreach ($discounts as $discount)
                $total_discount_amount += $discount->amount;
        $subtotal_including_discounts = $subtotal - $total_discount_amount;

        //Add total discount message
        $messages[] = "<strong>" . t("Total discount") . ":</strong>&nbsp;"
                . uc_currency_format($total_discount_amount);

/*
        //Add new subtotal message
        $messages[] = "<strong>" . t("Subtotal including discounts") . ":</strong>&nbsp;"
                . uc_currency_format($subtotal_including_discounts);
*/

        //Start row index at item count + 2 (1 for subtotal row in cart form, 1 more for our first row)
        $i = count($items) + 2;

        //Create table to hold discount messages
        $body = "<table class='cart-block-discounts'>";

        foreach ($messages as $message)
        {
                $evenOddClass = ( ($i % 2) == 0 ) ? "even" : "odd";
                $body .= sprintf("<tr class='%s'><td class='%s'>", $evenOddClass, "uc-cart-block-discounts-cell")
                        . $message . "</td></tr>";
                $i += 1;
        }
        //Close table
        $body .= "</table>";

        $result = array();
        $result['total'] = $total_discount_amount;
        $result['details'] = $body;

        return $result;
}


mikec’s picture

Thanks a ton, @a_lawry. This is the line I that you corrected for me:

$discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents());

I didn't know about the uc_cart_get_contents() function, and based on the two function's prototypes, I tried to pass $items into uc_discounts_cart_block_discounts, which didn't work. Everything works well now.

totnorbi’s picture

To which .module file do I have to copy the new function? uc_cart?

jdln’s picture

subscribing

pauljb’s picture

subscribe

jdln’s picture

a_lawry, ive added the first load of code from post 9 to my template.php file and changed menkom_jaguar to my themes name.

Where does the 2nd load of code go?
Thanks

jdln’s picture

I messaged a_lawry about where to put his code and he said this;

‘The first lot of code needs to go in your theme and renamed.

The second lot of code is placed in the uc_discounts (alternative) module. It increases the functionality of the module a bit to return the discounts as an array of information. You can then output that information anywhere on your site.’

Im assuming the uc_discounts module is the file called uc_discounts.module.

I get a white screen unless I remove the opening and closing php tags from both pieces of code. Im assuming this is because the files im copying to already have content so already have opening php tags.

So I think ive done everything right but their is no (visible) change to my site. To find the discounted price I need to find the number from an array. Ive added <?php dprint_r($GLOBALS) ?> to my page.tpl.php.

Ive then added some products to my cart. I can see the price is £15 but £12 with discounts. Ive gone to a different page and now searched for the text ‘12’ in the arrays. However I can’t find it anywhere.

Have I added the code correctly so far? Is so, how do I find out how to call the discounted value?

Thanks

a_lawry’s picture

You need to clear the theme registry to make sure the theme function is recognised.

jdln’s picture

That hasn't changed anything. Im using the zen theme so ive set this;

'For easier theme development, the theme registry is being rebuilt on every page request. It is extremely important to turn off this feature on production websites.'

Thanks

jdln’s picture

Ive made a fresh install of drupal and ubercart to try and get this working. With both the garland theme and a zen sub theme, as soon as I add the first load of code to template.php I get a white screen!

This is so frustrating as its the last thing I need to do for my site.

jdln’s picture

Im not getting the blank screen if I change zensubtheme_my_1_uc_cart_block_content to zensubtheme_my_1_theme_uc_cart_block_content

However, I dont know if the code is doing anything. Their is no visible change to my site. If I add this to my page template;

 <?php
print '<pre>';
print htmlspecialchars(print_r(get_defined_vars(), TRUE), ENT_QUOTES);
print '</pre>';
?>

On the shopping cart page I can see the 'Subtotal including discounts' number in the code. However when I search for this number on other pages it isnt there.

If the fix was working, would the cart block appear differently or do I have to theme it? If i do have to theme it, how can I check if its working?

Thanks

mhel’s picture

@jdln I maybe reading it wrong, but on #18 both codes should be applied at the same time, one for the template the other to the uc_discounts.module .

On #19 rename your function back to your first one without _theme_ and try commenting this line:
$subtotal += get_discounts_taxed_price($item->price, $item) * $item->qty; on the second code in #9. I myself can't find where get_discounts_taxed_price() is defined.

Clear your cache first. admin->settings->performance .

If that works, you might want to add this if ($discount_info) just below this line $discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents()); at the first code on #9 above.

Note: php tags not included, it's just for posting clarity :-)

Edit 1: Forgot to thank a_lawry for the mods, thanks :)
Edit 2: Corrected the line for if condition.

jdln’s picture

Im still getting the white screen.

If I use dreamweaver to search for the text 'uc_cart_block_content' in my source code of my entire site the only instance found is in the template.php where I just added it. Should it already exist somewhere else?

Thanks

a_lawry’s picture

It's a while since I looked at any of this.

Yes that's correct the first lot of code added to your theme and the second to the uc_discounts module.

theme_uc_cart_block_content() is on line 667 of uc_cart.module in the ubercart/uc_cart module. That is not the latest version of ubercart so it may been rearranged lately. That is the theme function that displays the ubercart cart block and the one that you need to override in your theme to make changes to it.

Make sure you add the second lot of code to the module and that is working first. If drupal can't find that function the theme function will cause a white screen because it can't find the function and throws an error.

jdln’s picture

OK. Ive got it working if I removed this from uc_discounts.module (hallelujah!);

$subtotal += get_discounts_taxed_price($item->price, $item) * $item->qty; 

Mhel, what does this do?

<?php
if ($discount_info)
?>

One last thing, I want to get the pre discount price in the shopping cart block aswell. By pasting the following into my template.php I can add a table row in the correct place, but how do i call the original price?

function THEMENAME_uc_cart_block_summary($item_count, $item_text, $total, $summary_links) {
  $context = array(
    'revision' => 'themed-original',
    'type' => 'amount',
  );
  // Build the basic table with the number of items in the cart and total.
  $output = '<table class="cart-block-summary"><tbody><tr>'
           .'<td class="cart-block-summary-items">'. $item_text .'</td>'
           // row needs to be added here.
           .'<td class="cart-block-summary-total"><label>'. t('')
           .'</label> '. uc_price($total, $context) .'</td></tr>';

  // If there are products in the cart...
  if ($item_count > 0) {
    // Add a view cart link.
    $output .= '<tr class="cart-block-summary-links"><td colspan="2">'
             . theme('links', $summary_links) .'</td></tr>';
  }

  $output .= '</tbody></table>';

  return $output;
}

Thanks so much to everyone, the help is hugely appreciated.

mhel’s picture

That line just hides the label "Total discounts" if discount is not available.
The full line should look like this I added the braces for clarity.

if ($discount_info) {
  $output .= "<div class=\"cart-block-discounts-total\"><label>Total discounts:</label><span class=\"uc_price\">" . uc_currency_format($discount_info['
total']) . "</span></div>";
}

For your second question, I'm not sure what you mean. On my setup I can see the pre-discount price like this (for a 10% discount).
item 1            $249.50
Qty. Discount  $24.95
Total             $224.55

jdln’s picture

Ah, thanks for clearing up my first question.

As for the second question. Im viewing my cart block collapsed so currently it says;
15 Items £27.00 View cart

Ive removed the "Total discounts" text. 27 is the price with discounts applied. What I want to do is print the price before discounts (Ill use css to put a line through it);

15 Items £30.00 £27.00 View cart

Thanks again

mhel’s picture

In that case you probably don't need your THEMENAME_uc_cart_block_content(), and just use THEMENAME_uc_cart_block_summary(). Make the changes from your original post with the lines with php tags. (take the tag off when you use it, that's the only way I know how to emphasize it inside code tags)

function THEMENAME_uc_cart_block_summary($item_count, $item_text, $total, $summary_links) {
  $context = array(
    'revision' => 'themed-original',
    'type' => 'amount',
  );
  <?php $discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents()); ?>
  // Build the basic table with the number of items in the cart and total.
  $output = '<table class="cart-block-summary"><tbody><tr>'
           .'<td class="cart-block-summary-items">'. $item_text .'</td>'
           // row needs to be added here.
           .'<td class="cart-block-summary-total"><label>'. t('')
        <?php  .'</label> '. uc_currency_format($total) . ' / ' . uc_price(($total - $discount_info['total']), $context) .'</td></tr>'; ?>

  // If there are products in the cart...
  if ($item_count > 0) {
    // Add a view cart link.
    $output .= '<tr class="cart-block-summary-links"><td colspan="2">'
             . theme('links', $summary_links) .'</td></tr>';
  }

  $output .= '</tbody></table>';

  return $output;
}

On my setup it looks like this:

22 items     Total: $315.50 / $283.95

I'm guessing my responses is more suitable posted in a forum than in issues I'm new to php/drupal/ubercart pls. pardon me :)

jdln’s picture

With your code im getting this;
3 Items £27.00 / £24.00

But my subtotal is £30 and the subtotal including discounts is £27.00.

If I only add the 2nd load of code then I get this;
3 Items £27.00 / £27.00

Thanks

mhel’s picture

Try not using your THEMENAME_uc_cart_block_content(), I mean discard that whole function if you're still using it.
If you examine that function you will see that it already use $total - $discount_info['total'] which get pass as a parameter to THEMENAME_uc_cart_block_summary and that is already the discounted value.

jdln’s picture

Using your code from 26 as well as removing what you suggested in 28 makes it works.
Thanks again.

jdln’s picture

Oop, just realized when a discount is being applied i get this;
1000 Items £100.00 / £90.00

So far so good, but when a discount is not being applied I get this;
1 Item £0.10 / £0.10

When a discount is not being applied, how can I stop the price being shown twice?

This is what I have in my template.php

function zensubtheme_my_1_uc_cart_block_summary($item_count, $item_text, $total, $summary_links) {
  $context = array(
    'revision' => 'themed-original',
    'type' => 'amount',
  );
$discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents());
  // Build the basic table with the number of items in the cart and total.
  $output = '<table class="cart-block-summary"><tbody><tr>'
           .'<td class="cart-block-summary-items">'. $item_text .'</td>'
           // row needs to be added here.
           .'<td class="cart-block-summary-total"><label>'. t('')
.'</label> '. uc_currency_format($total) . ' / ' . uc_price(($total - $discount_info['total']), $context) .'</td></tr>';

  // If there are products in the cart...
  if ($item_count > 0) {
    // Add a view cart link.
    $output .= '<tr class="cart-block-summary-links"><td colspan="2">'
             . theme('links', $summary_links) .'</td></tr>';
  }

  $output .= '</tbody></table>';

  return $output;
}

And this is what I have in my ub_discounts.module;

function uc_discounts_cart_block_discounts($items)
{
        global $user;

        //Create phony order object to call to get_discounts_for_order
        $order = new stdClass();
        $order->uid = $user->uid;
        $order->products = $items;

        $errors = array();
        $warnings = array();
        $messages = array();
        //echo "Calculating discounts";
        $discounts = get_discounts_for_order($order, $errors, $warnings, $messages);

        //If there are no discounts, do not alter cart
        if (count($discounts) == 0)
                return array();

        //Calculate subtotal with discounts
        $subtotal = 0;
        foreach ($items as $item)
        {
                //$subtotal += $item->price * $item->qty;
                //$subtotal += get_discounts_taxed
        }
        $total_discount_amount = 0;
        foreach ($discounts as $discount)
                $total_discount_amount += $discount->amount;
        $subtotal_including_discounts = $subtotal - $total_discount_amount;

        //Add total discount message
        $messages[] = "<strong>" . t("Total discount") . ":</strong>&nbsp;"
                . uc_currency_format($total_discount_amount);

/*
        //Add new subtotal message
        $messages[] = "<strong>" . t("Subtotal including discounts") . ":</strong>&nbsp;"
                . uc_currency_format($subtotal_including_discounts);
*/

        //Start row index at item count + 2 (1 for subtotal row in cart form, 1 more for our first row)
        $i = count($items) + 2;

        //Create table to hold discount messages
        $body = "<table class='cart-block-discounts'>";

        foreach ($messages as $message)
        {
                $evenOddClass = ( ($i % 2) == 0 ) ? "even" : "odd";
                $body .= sprintf("<tr class='%s'><td class='%s'>", $evenOddClass, "uc-cart-block-discounts-cell")
                        . $message . "</td></tr>";
                $i += 1;
        }
        //Close table
        $body .= "</table>";

        $result = array();
        $result['total'] = $total_discount_amount;
        $result['details'] = $body;

        return $result;
}

If I add this; if ($discount_info) below this $discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents()); in my template.php, then when discounts are applied the block summary looks normal, but the summary isn't shown if no discounts are being applied.

So, what I need to do is stop one of these being shows when no discounts are being applied;
uc_currency_format($total) or uc_price(($total - $discount_info['total']), $context)

Could someone tell me how to do this? This really should be the last change.
Thanks again

jdln’s picture

It would be best if the uc_currency_format($total) wasn't shown when no discounts were applied, as ive added a span tag to this to apply a css linethrough to it.

mhel’s picture

Try the code below, I also changed the uc_currency to uc_price to do the formatting.

function zensubtheme_my_1_uc_cart_block_summary($item_count, $item_text, $total, $summary_links) {
  $context = array(
    'revision' => 'themed-original',
    'type' => 'amount',
  );
$discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents());
  // Build the basic table with the number of items in the cart and total.
  $output = '<table class="cart-block-summary"><tbody><tr>'
           .'<td class="cart-block-summary-items">'. $item_text .'</td>'
           // row needs to be added here.
           .'<td class="cart-block-summary-total"><label>'. t('') . '</label> ';
  
  if ($discount_info){   
     $output .=  uc_price($total, context) . ' / ' . uc_price(($total - $discount_info['total']), $context) .'</td></tr>';
  } else {
     $output .=  uc_price($total,  $context) .'</td></tr>';
  }

  // If there are products in the cart...
  if ($item_count > 0) {
    // Add a view cart link.
    $output .= '<tr class="cart-block-summary-links"><td colspan="2">'
             . theme('links', $summary_links) .'</td></tr>';
  }

  $output .= '</tbody></table>';

  return $output;
}
jdln’s picture

Your code works for no discounts but gave me a white screen if their were discounts. I played around a bit and got it working with this;

  if ($discount_info){  
     $output .=  uc_currency_format($total, context) . ' / ' . uc_price(($total - $discount_info['total']), $context) .'</td></tr>';

Which ive added my css to;

     $output .= '<div id ="linethrough">'. uc_currency_format($total, context) .'</div>'.' / '. uc_price(($total - $discount_info['total']), $context) .'</td></tr>';

All working brilliantly! Thanks a lot.

davident’s picture

I had this same issue and have found that changing one line in the override function will correct the issue.

<?php
function subtheme_uc_cart_block_content() {
  global $user;

  $output .= '<div id="cart-block-contents">';
  //$discount_info = uc_discounts_cart_block_discounts(uc_cart_get_contents());
  $items = uc_cart_get_contents();

  $item_count = 0;
  if (!empty($items)) {
    $output .= '<table class="cart-block-table">'
              .'<tbody class="cart-block-tbody">';
    $total=0;
    foreach ($items as $item) {
      $display_item = module_invoke($item->module, 'cart_display', $item);
      if (!empty($display_item)) {
        $output .= '<tr class="cart-block-item"><td class="cart-block-item-qty">'. $display_item['qty']['#default_value'] .' x</td>'
                  .'<td class="cart-block-item-title">'. $display_item['title']['#value'] .'</td>'
                  .'<td class="cart-block-item-price">'. uc_currency_format($display_item['#total']) .'</td></tr>';
        if ($display_item['options']['#value']) {
          $output .= '<tr><td colspan="3">'. $display_item['options']['#value'] .'</td></tr>';
        }
      }
      //$total += ($item->price) * $item->qty;
      $total += ($display_item['#total']) * $item->qty;
      $item_count += $item->qty;
    }
	$item_text = format_plural($item_count, '@count Item', '@count Items');
    $output .= '<tr class="cart-block-items-total">'
            . $item_text .'</tr></tbody></table>';
  }
  else {
    $output .= '<p class="empty">'. t('There are no products in your shopping cart.') .'</p>';
  }

  $output .= '</div>';
  $view = l(t('Checkout'), 'cart', array('rel' => 'nofollow'));
  if (variable_get('uc_checkout_enabled', TRUE)) {
    $checkout = ' ('. l(t('Checkout'), 'cart/checkout', array('rel' => 'nofollow')) .')';
  }
  
  $output .= '<table class="cart-block-summary-table"><tbody class="cart-block-summary-tbody">'
            .'<tr class="cart-block-summary-tr"><td class="cart-block-summary-total">'
            . t('Total:') .'</td><td class="cart-block-summary-total-num">'. uc_currency_format($total) .'</td><div class="clear"></div></tr>';
  
  if ($item_count > 0) {
    $output .= '<tr><td colspan="2" class="cart-block-summary-checkout">'. $view .'</td></tr>';
  }
  $output .= '</tbody></table>';

  return $output;
}

Here are the lines of interest.
//$total += ($item->price) * $item->qty;
//$total += ($display_item['#total']) * $item->qty;

Line 1 is the default setup that displays the total without discounts. Since each line item does show its correct discount, we just use that value to calculate the total, instead of $item->price.

jdln’s picture

The code causes a white screen with the latest version of the module.

jrust’s picture

Status: Active » Closed (duplicate)

Duplicate of a newer 2.x issue: #909942: Discounts in Cart Block