Can anyone help me with the flexicharge application. I have it setup so that the US is a region and it will charge a flat rate shipping fee. When the US is not chosen it uses another charge. That works great and is no problem. However, when I have a customer input a coupon code (ie. free shipping) It will charge the shipping fee again. So the checkout screen shows the product price, shipping, coupon, shipping again and then total. I cant figure out what I am doing wrong or the setting to correct this. Any help would be greatly appreciated. Under flexicharge defaults it gives the choice to select what to apply to and underneath the box it says it will be applied only once but this is simply not the case. Please help!

http://www.hallofcandles.com

If you want to see what im talking about choose a product check out and enter coupon code 1251-7128-8149.

Comments

runningstix’s picture

Also please dont make fun of my site. It is for my wifes business and was my first stab at e-commerce. Beginner Here

jeremdow’s picture

I'm having the same problem - working on it now.

Will post if I make any progress.

JMD

jeremdow’s picture

Okay, this seems to take care of it for me - if you don't mind hacking flexicharge a bit - I couldn't think of a way to handle it from the .inc's.

Basically, I just set it so that the flexicharge_checkoutapi hook can only run once. Before it finishes, it puts a flexicharge_applied value in the $txn, and then won't repeat the charges when coupons are applied.

So - if you want to try this - you just need to add these two lines of code to flexicharge_checkoutapi() in flexicharge.module:

Add this near the top:

<?php
  
  //only run once (fix double charge with coupons) - jmd
  if($txn->flexicharge_applied) {
    return;
  }

?>

And then this just before the function returns:

<?php

  //charges have been calculated - do not run again; -jmd
  $txn->flexicharge_applied = true;

?>

It works for me - but try at your own risk, I suppose.

JMD

runningstix’s picture

JMD I copied the code just as you have it and I am getting this error:

Parse error: syntax error, unexpected '<' in /home/.pikachy/runningstix/hallofcandles.com/modules/ecommerce/contrib/flexicharge/flexicharge.module on line 385

it seems it is in the actual code not the description of what it does. Any ideas what I am doing wrong? Wherever I put that code is the line that the error occurs on.

runningstix’s picture

JMD I copied the code just as you have it and I am getting this error:

Parse error: syntax error, unexpected '<' in /home/.pikachy/runningstix/hallofcandles.com/modules/ecommerce/contrib/flexicharge/flexicharge.module on line 385

it seems it is in the actual code not the description of what it does. Any ideas what I am doing wrong? Wherever I put that code is the line that the error occurs on.

finhirschoff’s picture

Works great! N.B. - only works if Flexicharge is set before Payment in the Screen Order section.

A clarification for other interested parties on how to properly hack this:

Function should look like this:

/**
 * Use checkoutapi to calculate charges, each method has it's own checkout
 * function
 */
function flexicharge_checkoutapi(&$txn, $op, $arg3 = null, $arg4 = null) {
  if ($txn == 'flexicharge') {
    return true;
  }
 
 //only run once (fix double charge with coupons) - jmd
  if($txn->flexicharge_applied) {
    return;
  }

  // cart_get_items() doesn't do a full node load, so we don't have
  // access to shipping data and therefore need to manually add
  // flexicharge data here.
  // NOTE: this is done like shipping.module
  if (isset($txn->items)) {
    foreach (array_keys($txn->items) as $nid) {
      flexicharge_nodeapi($txn->items[$nid], 'load', NULL);
    }
  }
  switch ($op) {
    case 'review':

      // Get saved charges from database.
      $charges = _flexicharge_load_charges();
      
      // Remove charges that does not apply by PRODUCT.
      foreach ($charges as $charge) {
        // If there is a product filter on the charge.
        if (!empty($charge->ptypes)) {
          // Check if any ptypes match the filter.
          foreach ($txn->items as $item) {
            if (empty($charge->ptypes[$item->ptype])) {
              unset($charges[$charge->chid]);
              break;
            }
          }
        }
      }

      global $user;
      // Remove charges that does not apply by ROLE.
      foreach ($charges as $charge) {
        // If there is a role filter on the charge.
        if (!empty($charge->roles)) {
          // Check if any roles match the filter.
          foreach ($user->roles as $role_id => $role) {
            if (empty($charge->roles[$role_id])) {
              unset($charges[$charge->chid]);
            }
          }
        }
      }
      // Call this to include() the method files.
      _flexicharge_partners();

      $return = array();
      $weight_offset = 1000; // Very heavy, we want to appear below all other charges.

      foreach ($charges as $charge) {
        // this is the operation we normally want for a misc charge
        $f = $charge->provider .'_flexicharge_review';
        if (function_exists($f)) {
          $charge_data = $f($txn, $charge);
        }
        // Note, we give flexicharge a heavy weight until it can be
        // properly integrated into the order of other misc charges
        // because some misc charges could be based on other charges.

        if (is_array($charge_data)) {
          $charge_data['weight'] += $weight_offset;
          $txn->misc[] = (object)$charge_data;
        }
        elseif ($charge_data) {
          $return[] = $charge_data;
        }
      }

//charges have been calculated - do not run again; -jmd
  $txn->flexicharge_applied = true;

      break;

    case 'review_submit':

      break;

  }

}
slombardi’s picture

great. It works for me as well.

jbomb’s picture

This is an awkward solution, but I haven't had any luck with the 'already_added' element....

function custom_flexicharge_review(&$txn, $charge) {
  switch ($charge->method) {
      case 'general':
         $misc = array(
            'type' => 'fl_'. $charge->chid,
            'description' => $charge->display,
            'operator' => $charge->operator,
            'rate' => $charge->rate,
            'subtotal_before' => $charge->subtotal_before,
            'subtotal_after' => $charge->subtotal_before,
            'already_added' => $charge->already_added,
            'weight' => $charge->position,
            'method' => $charge->method,
            'callback' => 'general_custom_flexicharge_calculate',
          );
      break;
  }
  
  // only process this once.
  if (is_array($txn->misc)) {
    foreach ($txn->misc as $flexi) {
      if ($flexi->type == 'fl_'. $charge->chid) {
	    return;  // already processed, do nothing
      }
    }
  }
  return $misc; // add flexicharge
}
research37’s picture

I'm having the same problem and posted a question on it a month ago. Did you have any luck sorting this out?