Login Form


Board index » Loaded Commerce Support » General Support

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: 6.5 CE order total coupon
PostPosted: Sun May 27, 2012 12:14 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
Okay, I've been looking into coupons again since my version doesn't do coupons well for products that are already discounted (it adds the coupon discount on top of the original product price where I want it to do it against the discount price)...

To that end was looking through code by code to see if I can find something simple that would be a fix and I ran across this section of code:
Code:
      if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
      //v5.14 id coupon total > 0
      $p_processed = false;
      if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
      if ($get_result['coupon_type']=='S' && $get_result['coupon_amount'] > 0 ) $c_deduct = $order->info['shipping_cost'] + $get_result['coupon_amount'];
      if ($get_result['coupon_minimum_order'] <= $this->get_order_total()) {
        if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
          if ($get_result['restrict_to_categories']) {
            $cat_ids = split("[,]", $get_result['restrict_to_categories']);
            for ($j=0; $j < sizeof($order->products); $j++) {
              $my_path = tep_get_product_path(tep_get_prid($order->products[$j]['id']));
              $sub_cat_ids = split("[_]", $my_path);
              for ($iii = 0; $iii < count($sub_cat_ids); $iii++) {
                for ($ii = 0; $ii < count($cat_ids); $ii++) {
                  if ($sub_cat_ids[$iii] == $cat_ids[$ii]) {
                    if ($get_result['coupon_type'] == 'P') {
                      $p_processed = true; 
                      $t = tep_get_prid($order->products[$i]['id']);
                      $pr_c = $this->product_price(tep_get_prid($order->products[$j]['id']));
                      $pr_c_specail = $this->get_products_special_price($pr_ids[$ii]);
                      $pr_c -= $pr_c_specail;
                      $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100;
                      $od_amount = $od_amount + $pod_amount;
                      continue 3;
                    } else {
                      $od_amount = $c_deduct;
                      continue 3;


Now is my logic wrong or do these two lines not do anything:
$t = tep_get_prid($order->products[$i]['id']);
$pr_c_specail = $this->get_products_special_price($pr_ids[$ii]);

the $i is not a valid variable so $t never equates to anything, and neither is $pr_ids (at least that I see).

Could someone give me a sanity check?
p.s. I changed the $i to $j and it seems to be right.

Thanks,
Mike

p.p.s. Also I hate how special is misspelled in this code.


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Mon May 28, 2012 8:15 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 3175
Location: New Zealand
That's pretty generous, coupon discount on top of a special price. If you have to track it, why not remove the special price and bump up the coupon discount?

The calculate_credit function is bound to be buggy, no question. Just seeing those deprecated splits you know that LC 6.5 isn't yet php 5.3 aware.

I'd like to know what that $t line does too, but the second line is in the $ii loop, so the $ii is the index of the array, so I think legit. Looks to me like it subtracts the special price from the products price and calculates the coupon discount on that figure, which sounds like its what you want it to do. But it's wrapped up in the restricted to categories / products loops, so is it doing what you want for your circumstances? Probably not.

Why not separate out the function, plug in some test numbers for variables and take it for a spin?
It'll probably be the first time for quite a while anyone will have.

Simon

_________________
www.codemehappy.com
No PMs thanks - link in signature below.
For Cre Loaded Commerce tips, quotes on coding work, free how-to articles


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Mon May 28, 2012 9:48 am 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
soundzgood2 wrote:
That's pretty generous, coupon discount on top of a special price. If you have to track it, why not remove the special price and bump up the coupon discount?


The retail price is a 'game we have to play' and so is the special price. Everyone in my industry sells the products for 15% or 20% off suggested retail (depending on the manufacturer limit) so everyone has the same product for the same price all the time. So the special price is everyone's normal price.
If we have a product for $100 and are allowed to discount it 20% the price is $80 on every competitors price all the time ---> if we have it for $100 all the time (removing the special price) we would never sell that product; they'd just go to our competitors.

So.... while we are not allowed to advertise the product for less then the maximum discount, and we want to show the discount off MSRP, what we are "allowed" to do is offer, through other avenues (like email, facebook, ect. marketing), coupons that make us cheaper then our competitors.


soundzgood2 wrote:
The calculate_credit function is bound to be buggy, no question. Just seeing those deprecated splits you know that LC 6.5 isn't yet php 5.3 aware.

Here is to hoping 5.3 doesn't come on too fast or LC will be a dead product unless updated very quickly.

soundzgood2 wrote:
I'd like to know what that $t line does too, but the second line is in the $ii loop, so the $ii is the index of the array, so I think legit. Looks to me like it subtracts the special price from the products price and calculates the coupon discount on that figure, which sounds like its what you want it to do. But it's wrapped up in the restricted to categories / products loops, so is it doing what you want for your circumstances? Probably not.


The second line does nothing either
Code:
$pr_c_specail = $this->get_products_special_price($pr_ids[$ii]);

but when I changed the $t line to use the $j variable then put that $t in so the code is:
Code:
$pr_c_specail = $this->get_products_special_price($t);

$t then equals the product id and $pr_c_specail then has values populated.


soundzgood2 wrote:
Why not separate out the function, plug in some test numbers for variables and take it for a spin?
It'll probably be the first time for quite a while anyone will have.
Simon


On this point I'll just say I am a complete untrained hack. Here is what I ended up with on my first attempt:

Code:
      if ($get_result['coupon_minimum_order'] <= $this->get_order_total()) {
        if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
          if ($get_result['restrict_to_categories']) {
            $cat_ids = split("[,]", $get_result['restrict_to_categories']);
            for ($j=0; $j < sizeof($order->products); $j++) {
              $my_path = tep_get_product_path(tep_get_prid($order->products[$j]['id']));
              $sub_cat_ids = split("[_]", $my_path);
              for ($iii = 0; $iii < count($sub_cat_ids); $iii++) {
                for ($ii = 0; $ii < count($cat_ids); $ii++) {
                  if ($sub_cat_ids[$iii] == $cat_ids[$ii]) {
                    if ($get_result['coupon_type'] == 'P') {
                      $p_processed = true; 
// $i was wrong so changed to $j for the next line below   $t = tep_get_prid($order->products[$i]['id']);
                      $t = tep_get_prid($order->products[$j]['id']);
                      $pr_c = $this->product_price(tep_get_prid($order->products[$j]['id']));
                      $pr_c_specail = $this->specialPrice=tep_get_products_special_price($t); // 2nd attempt seems to work (right product price)
//                        $pr_c -= $pr_c_specail;  // orig
   $pr_c_specail += $pr_c_specail;    // my change to add up discounted product prices
//original discount math            $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100);
                      $pod_amount = ((tep_round($pr_c_specail*10,2)/10*$c_deduct/100)/2); // used $pr_c_specail after my addition above but it was double so I also added the /2 to devide it in half!!
                      $od_amount = $od_amount + $pod_amount;
                  continue 3;
                    } else {
                      $od_amount = $c_deduct;
                      continue 3;


But as indicated I'm a hack and while it worked for a quantity of 1 of the products in the cart when the product was 2 or more the discount was only taken on one. So I tried to multiply by the $qty in that code section i.e. $pr_c_specail = $pr_c_specail * $qty; but of course at that place in the code $qty equals nothing.

So that said to me I have no clue what I'm doing... lol (but oh so close).

Mike


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Mon May 28, 2012 10:25 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 3175
Location: New Zealand
Php 5.3 is already here - has been for over a year at least. php 5.4 is being worked on now for release soon I think.

You assume the people who wrote the code did know what they're doing? Well actually they probably did more than the cre codejammers who followed; it's when all the added conditions get thrown in, that the plot was lost (eg category / product restrictions)

Simon

_________________
www.codemehappy.com
No PMs thanks - link in signature below.
For Cre Loaded Commerce tips, quotes on coding work, free how-to articles


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Mon May 28, 2012 11:58 am 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
Today is our day to be closed so I am going to go to a car show and later I'll see if I can hack in some code into that section of code that I posted to find the quantity of each product in the cart... If I can get that to work, even with this being a hack, I'll be satisfied for this round of code adaptations.

Thanks!
Mike


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Mon May 28, 2012 5:31 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
Back from the car show and just added in:
Code:
$howmany = tep_get_prid($order->products[$j]['qty']);
$pr_c_specail = $pr_c_specail * $howmany;


and it looks like it works... I'll test this completely later (maybe tomorrow when I am actually *at* work or before we publish the next coupon).

Mike


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Mon May 28, 2012 6:29 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
Just because others may want to use this, here is what I did.... (now this works for me and takes a discount percentage off the already discounted price versus the full original non-discounted price, others use at your own risk). Also if someone wishes to optimize the code feel free! lol.

Code:
function calculate_credit($amount) {

  //print('amount : '.$amount.'<br>');

  global $customer_id, $order, $cc_id;
  $cc_id = $_SESSION['cc_id'];
  $od_amount = 0;
  $get_products_special_price = 0;
  if (isset($cc_id) ) {
    $coupon_query = tep_db_query("select coupon_code,coupon_sale_exclude from " . TABLE_COUPONS . " where coupon_id = '" . $cc_id . "'");
    if (tep_db_num_rows($coupon_query) !=0 ) {
      $coupon_result = tep_db_fetch_array($coupon_query);
      $this->coupon_code = $coupon_result['coupon_code'];
      //$this->sale_exclusion = $coupon_result['coupon_sale_exclude'];
      $coupon_get = tep_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS ." where coupon_code = '". $coupon_result['coupon_code'] . "'");
      $get_result = tep_db_fetch_array($coupon_get);
      $c_deduct = $get_result['coupon_amount'];
      if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
      //v5.14 id coupon total > 0
      $p_processed = false;
      if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
      if ($get_result['coupon_type']=='S' && $get_result['coupon_amount'] > 0 ) $c_deduct = $order->info['shipping_cost'] + $get_result['coupon_amount'];
      if ($get_result['coupon_minimum_order'] <= $this->get_order_total()) {
        if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
          if ($get_result['restrict_to_categories']) {
            $cat_ids = split("[,]", $get_result['restrict_to_categories']);
            for ($j=0; $j < sizeof($order->products); $j++) {
              $my_path = tep_get_product_path(tep_get_prid($order->products[$j]['id']));
              $sub_cat_ids = split("[_]", $my_path);
              for ($iii = 0; $iii < count($sub_cat_ids); $iii++) {
                for ($ii = 0; $ii < count($cat_ids); $ii++) {
                  if ($sub_cat_ids[$iii] == $cat_ids[$ii]) {
                    if ($get_result['coupon_type'] == 'P') {
                      $p_processed = true; 
// orig code line - $i was wrong so changed it to $j on next line       $t = tep_get_prid($order->products[$i]['id']);
                      $t = tep_get_prid($order->products[$j]['id']); //
                      $pr_c = $this->product_price(tep_get_prid($order->products[$j]['id']));
                        $pr_c_specail = $this->specialPrice=tep_get_products_special_price($t);  // 2nd attempt seems to work (right product price)
$howmany = tep_get_prid($order->products[$j]['qty']);
$pr_c_specail = $pr_c_specail * $howmany;
//                      $pr_c -= $pr_c_specail;  // orig
   $pr_c_specail += $pr_c_specail;    // my change to add up discounted product prices
//original discount math            $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100);
                      $pod_amount = ((tep_round($pr_c_specail*10,2)/10*$c_deduct/100)/2); // used $pr_c_specail after my addition above but it was double so I also added the /2 to divide it in half!!
                      $od_amount = $od_amount + $pod_amount;
                      continue 3;
                    } else {
                      $od_amount = $c_deduct;
                      continue 3;
                    }
                  }
                }
              }
            }
          } //endif $get_result['restrict_to_products']
         
          if ($get_result['restrict_to_products']) {
            $pr_ids = explode(",", $get_result['restrict_to_products']);
            for ($ii = 0; $ii < count($pr_ids); $ii++) {
              for ($i=0; $i < sizeof($order->products); $i++) {
                if ($pr_ids[$ii] == tep_get_prid($order->products[$i]['id'])) {
                  if ($get_result['coupon_type'] == 'P') {   
                    $p_processed = true;                       
                    $pr_c = $this->product_price($pr_ids[$ii]); //Fred 2003-10-28, fix for the row above, otherwise the discount is calc based on price excl VAT!
                    $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100;
                    $od_amount = $od_amount + $pod_amount;
                  } else {                   
                    $od_amount = $c_deduct;
                  }
                }
              }
            }
          }
        } else {
          if ($get_result['coupon_type'] !='P') {
            $od_amount = $c_deduct;
          } else {
            $od_amount = $amount * $get_result['coupon_amount'] / 100;
          }
        }
      }
    }

    if ($od_amount>$amount) $od_amount = $amount;

  }

//print('1233od_amount : '.$od_amount.'<br>');

  return $od_amount;
}


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Tue May 29, 2012 9:19 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
Oops.... That worked for me IF I didn't have any products outside of allowed categories but if I had any products in the "valid products" it still took the discount off the original price of those items. So changed the "restrict to products" section like this:

Code:
                if ($pr_ids[$ii] == tep_get_prid($order->products[$i]['id'])) {
                  if ($get_result['coupon_type'] == 'P') {   
                    $p_processed = true;                       
                    $pr_c = $this->product_price($pr_ids[$ii]); //Fred 2003-10-28, fix for the row above, otherwise the discount is calc based on price excl VAT!
// next 4 lines added
$pr_c_specail = $this->specialPrice=tep_get_products_special_price($pr_ids[$ii]); // 2nd attempt seems to work (GIVES right product price) but need to make the math work below
$howmany = tep_get_prid($order->products[$i]['qty']);
$pr_c_specail = $pr_c_specail * $howmany;
$pr_c_specail += $pr_c_specail;    // my change to add up discounted product prices
// ORIG                    $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100;
                      $pod_amount = ((tep_round($pr_c_specail*10,2)/10*$c_deduct/100)/2); // used $pr_c_specail after my addition above but it was double so I also added the /2 to devide it in half!!
                    $od_amount = $od_amount + $pod_amount;
                  } else {                   


So that's that I guess.... Again this might just be a hack for myself but might (I hope) help someone else.


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Wed May 30, 2012 9:07 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 3175
Location: New Zealand
I tried replicating the issue you mention in a 6.5 B2B cart - it works fine with items on special and using a $ deduction coupon. Comparison of the ot_coupon.php code between CE and B2B shows they're the same.

How is your coupon setup? There are many factors that could be in play here - although $t is probably redundant and there's definitely deprecated code (and way too many coder comments throughout the files), I think the hack you've written isn't necessary in a 6.5 cart and the cause could be elsewhere.

Simon

_________________
www.codemehappy.com
No PMs thanks - link in signature below.
For Cre Loaded Commerce tips, quotes on coding work, free how-to articles


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Wed May 30, 2012 4:46 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
You are probably 100% right. I've 'integrated' salemaker into 6.5 just before this and that was most likely the cause. Sorry I didn't think about that for this issue since the products are not covered under the salemaker products. Hmmmm.... I have to make sure I didn't hork something up really bad if my integration affected coupons on products that are not covered under salemaker and I don't see any calls to salemaker in the coupons routine.

I know Simon, I know... there was a reason they removed it. (But I'll just say at least I am attempting to move this one store up to 'current' code - I've still got one store with 6.3.3 and it'll have to stay that way unless there is a quick and dirty way to replicate salemaker functionality since we get feeds weekly that are automatically put on "special" with salemaker effortlessly - which I can't seem to get done automatically with regular specials -- human intervention is required after each feed upload. Hence the reason I was trying to integrate salemaker into 6.5 CE.)

I'll spin the store through it's paces each day and see if I find something wrong.... But I'll say that so far with the final code I posted, yesterdays tests, and today's sales everything seems to be working 100%.

Mike


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Thu May 31, 2012 3:06 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 3175
Location: New Zealand
Do 'Specials' and 'Specials by Categories' not do what SM does? Yeah, no doubt SM is affecting the final order total area.

Simon

_________________
www.codemehappy.com
No PMs thanks - link in signature below.
For Cre Loaded Commerce tips, quotes on coding work, free how-to articles


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Thu May 31, 2012 4:53 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
Nope.

Specials (even specials by categories) are a product by product special - but salemaker is a whole category special (different logic and different database tables).
I'll see if I can explain better how salemaker works better/faster and has invaluable functions.

So I have categories that have a % discount applied.
When the weekly feed comes in, new products are automatically added to the appropriate categories.
Those new products, by the fact of being in those categories automatically have the % applied because of the checkout logic checking the "salemaker_sales" table in the database and applying the discount.

Now with specials or specials by category, no discount is applied until you run the specials or specials by category and this then adds an entry for each product the special is applied to, into the "specials" database table - meaning: every product that has a special applied has to have an entry in the database; new products do not have an entry until someone puts it there by running specials.

Basically salemaker sees if the product is in a category that has a discount attached to it and then applies a discount to every product in that category.

"Specials by category" is a misnomer; salemaker is actually specials by category.

Hard to write but I hope it is clear.

Mike


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Fri Jun 01, 2012 4:54 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 3175
Location: New Zealand
Whatabout the coupon facility where you can discount an entire category and track the sales ... does this not do what salemaker does (and slightly more as it's 1) measureable - is SM? 2) sales-driven ) ?

Simon

_________________
www.codemehappy.com
No PMs thanks - link in signature below.
For Cre Loaded Commerce tips, quotes on coding work, free how-to articles


Top
 Profile  
 
 Post subject: Re: 6.5 CE order total coupon
PostPosted: Fri Jun 01, 2012 3:58 pm 
Offline
CRE Expert

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 605
Location: CO
soundzgood2 wrote:
Whatabout the coupon facility where you can discount an entire category and track the sales ... does this not do what salemaker does (and slightly more as it's 1) measureable - is SM? 2) sales-driven ) ?

Simon


Could you elaborate on "the coupon facility..." part?
To that end wouldn't the products displayed price be the full price until the coupon is applied? Wouldn't that look to the world like my products would be full retail? (remember earlier in this thread where we discount everything to 15 or 20% off suggested retail using salemaker? - that gives a product look of:
MSRP Our Price
$100 $80

As shown by the flop of a companies new low price tactic (USA Store JC Penny) customers love to see themselves getting a discount off MSRP and go completely crazy to buy a product when they get even a bigger discount off that discount.


Mike


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

Board index » Loaded Commerce Support » General Support

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
It is currently Wed May 22, 2013 12:34 am
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group