Ive been trying (and failing) to get discounts to show in the shopping cart block here;
http://drupal.org/node/507820

Although im doing it wrong, it seems their aren't too many steps to achieve this. Could someone write a how to theming guide for this?

Id be happy to rewrite it, add screen shots etc, but at the moment I cant figure how to get this to work.

Thanks

Comments

gillisig’s picture

I was dealing with the same thing.. and after a few hours i figured it out.. Im gonna try to write it down now in a simple manner so you can possibly write a tutorial or something.

Be aware that this is absolutely the first time I try to describe something like this or contribute in anyway.. i have only been using drupal for 2 weeks or so.



Basicly I added the following code to /modules/ubercart/uc_cart/uc_cart.module at line 688
(right after "function theme_uc_cart_block_content($help_text, $items, $item_count, $item_text, $total, $summary_links) {" and what follows that line.. so actually right after after "return $output;")

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





Then at the bottom of my template.php file (which is in my case located at /themes/THEME_NAME/template.php) I added the next code, be aware that where i write THEME_NAME you will have to replace it with your theme name

function THEME_NAME_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());
  if ($discount_info) 

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



I hope this works and is more clear than the nonsense where you had to go back and forth in the other forum to get the info you needed and puzzle it together like I did, the reason the codes didnt work was that somethings needed to be changed slightly but with these codes all you should have to do is paste them at the correct place and rename the beginning of the second code with your theme name.

I hope this helps someone

Regards,
Gilli Hustad


P.S. Thanks should go to a_lawry for figuring all this out and writing this code, I simply took it and tried to make it more clear and simple.

abemonkey’s picture

Version: 6.x-1.0-beta9 » 6.x-2.2

Thank YOU!!! I couldn't get this figured out before I got these instructions. I have one issue. The discount amount isn't showing after the "Total Discounts:" line. I am running version 6.x-2.2. What version are you running? Do you have any ideas of what I might need to change. The only other variable that I can think of is that some of my products with discounts have attributes. Thanks for any feedback! Also I'm running drupal 6.19 not 6.20 as I had a major issue when I attempted to update to 6.20 and had to restore from my backup to get back up and running again. Don't think that would make any difference in this case though.

gillisig’s picture

Im running version 6.x-2.4 but actually im encountering that error also.. I just chose to ignore it since i mostly wanted the correct total amount to show up, but I am pretty sure that it has something to do with the following line since I removed it from the original code because it was keeping the code from working for some reason.

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

But since theres only 2 weeks since I started using Drupal and php I am not gonna try to crack this problem for now :)

Regards,
Gilli Hustad

abemonkey’s picture

I'm better at JavaScript than PHP so I guess I'll just solve this that way but I'd like to fix the PHP code to do what I want if anyone figures this out let me know! Thanks!

abemonkey’s picture

For those of you who want the total discount amounts to show in the block I used the jquery calculation plugin which can be found here. Just add the plugin to your theme's .info file and use this code:

var preDiscountTotal = $(".cart-block-item-price .uc-price").sum();
var discountedTotal = $(".cart-block-summary-total .uc-price").sum();
var totalDiscounts = parseFloat(preDiscountTotal) - parseFloat(discountedTotal);
$('.cart-block-discounts-total label').append("$"+ totalDiscounts.toFixed(2));
jdln’s picture

Hi abemonkey, I like your thinking, ive been struggling with this issue for ages and jquery is often an easier way to go.

The calculation plug in looks quite simple but where do you get the data to use for the sum? My site has a 10% discount for orders over a certain amount. Where do I grab this 10% from and how can I only have the jquery run if the order is large enough to get a discount?

Thanks

mttjn’s picture

Very nice - thanks so much for writing that up.
Couple of problems: (1) some missing brackets in the if() statement (2) did you try adding products (and so qualifying for a discount) and then removing all products? You may find your shopping cart block disappears! These are fixed with the code below. (Note: this post only refers to the code that goes in the template.tpl.php file; the code for uc_cart.module is the same)

function THEMENAME_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());
  
  	if ($discount_info) 
	{ // <- **ADDED 
  		//$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;
  	} // <- **ADDED 
  	else // <- ** THIS ELSE STATEMENT ADDED (so even if we don't have any $discount_info, we're still returning something)
  	{
  		$output .= theme('uc_cart_block_summary', $item_count, $item_text, $total, $summary_links); 
  		return $output;
  	}
}

One thing I would like to know: does anyone know how to write the code for uc_cart.module as a custom module, rather than adding it directly to uc_cart.module? Thanks.

mttjn’s picture

The solution I've just posted works... but agreed: jQuery would be a whole lot easier! We just need to pass the discounted total variable from uc_discounts.module to the theme, pick it with jQuery, and then switch the figure that's showing. Im sure this can be done, but im not sure how... if anyone can shine a light on this(?), would appreciate it.

nitebreed’s picture

Subscribing