Hi all,

I'm trying to show both discount and normal prices of a product. Not only when the viewer actually has the discount, but also when the viewer doesn't - showing a discount price to a non-member of the shop hopefully triggers him to become one. I know there are plenty of modules to display discount prices together with normal ones when the user is entitled to the discount, but I couldn't get it done the other way around..

One of the things I tried was to create a price formatter and therein invoke a component that calculates the discount price via the option 'Calculate a value'. Ive read the rules_invoke_component returns some value(s), but here it doesn't.
Another option seemed to have an extra price field on the product and save the discount price there, but that wouldn't work when multiple discount rules are in place.

I was looking into the direction of executing a rule but not saving the value onto the product - as seems the case when using the 'Multiply the unit price by some amount'-option. But that price in all cases is also the price for the viewing user, which is not always the case.

Can anyone help redirect me? Thanks in advance!

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

I'm not really sure we have much to offer than a confirmation - you will need to write a custom price formatter for this. The challenge, of course, is that price calculation happens for the current user, not some other hypothetical user. You'll have to figure out how you can trigger a condition in your product pricing rules to execute for the normal condition (member?) and for your forced evaluation so you can get the right prices for your display formatter.

hhschoone’s picture

Thanks for the quick reply! I now have built such a custom formatter, using Rules to calculate the discount-price for people who aren't entitled to that discount. However, I'm not sure if I implemented it the right way.

I want the callback function multipriceformatter_calculate_full_discount_price() to add the full discount price as an extra parameter to the product for it to be available in hook_field_formatter_view(). Now I did this via a plain SESSION, but it doesn't seem to me to be the correct way.
I know there is a way to make the callback function return its value, but then it is still only available to Rules via the Rules UI, am I right? If I could just access the return value somewhere in code..

Is there any other way? Thanks in advance, again :)

/** 
 * hook_rules_action_info()
 *
 * Create custom Rules action to save discount value.
 * see callback (multipriceformatter_calculate_full_discount_price) for more info.
 */
function multipriceformatter_rules_action_info() {
  $defaults = array(
    'group' => t('Commerce pricing')
  );

  $actions = array(
    'multipriceformatter_calculate_full_discount_price' => $defaults + array(
      'label' => t('Calculate Full Discount Price'),
      'parameter' => array(
        'line_item_label' => array( // Line item LABEL necessary to store full discount price in SESSION
          'type' => 'text',
          'label' => t('Enter line item label'),
          'description' => t('Line item label - NOT line item id, that one is always empty..'),
        ),
        'calculate_full_discount_price' => array(
          'type' => 'text',
          'label' => t('Full discounted product price'),
          'description' => t('The full discounted price should be calculated in an action BEFORE this action.'.
                              'Then the outputted value is available here.'),
        ),
      ),
    )
  );  
  
  return $actions;
}

/** 
 * Callback as defined by multipriceformatter_rules_action_info()
 * The function name is necessarily the same as the action-name
 *
 ***
 * Store full_discount_price in a session, to access it
 * later on for display
 * see: multipriceformatter_field_formatter_view
 */
function multipriceformatter_calculate_full_discount_price($line_item_label, $calculate_full_discount_price){
  // Use $line_item_label as unique id for the calculated price. Line_item_label equals
  // the product SKU
  
  // There exists some functionality to return variables from this function,
  // but I don't know where these values end up - they're probably only available
  // to Rules UI..
  
  // Also, don't use variable_set/_get methods because that stores into the database, and we need to
  // unset those variables when deleting products/disabling the module/etc. etc.

  // Bottomline: SESSION is the easy way :)
  $_SESSION['discount_prices'][$line_item_label] = $calculate_full_discount_price;  
}

p.s. I know this is more of a support request for Rules, but since it's about the same thing as before, I hope you can answer it here.