CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Adding Prices With hook_civicrm_buildAmount
Pages: [1]

Author Topic: Adding Prices With hook_civicrm_buildAmount  (Read 1579 times)

lkacenja

  • I’m new here
  • *
  • Posts: 18
  • Karma: 2
Adding Prices With hook_civicrm_buildAmount
March 11, 2011, 08:59:34 am
Hi,

I've looked around a bit, and don't see any documentation for this problem. Asked briefly in IRC, but decided to follow up here. I'm running a brand new installation of Drupal 7 and CiviCRM 4. Building a Drupal Module that allows admins to associate a CiviEvent with a price set, and a membership type. Thus a certain event could offer one price for members, and another for non-members, presenting the user with the correct option based on their membership status. I'm nearly finished except that I am running in to a problem with  hook_civicrm_buildAmount.

I can't seem to add a price. The documentation (http://wiki.civicrm.org/confluence/display/CRMDOC33/CiviCRM+hook+specification) clearly shows adding a price to the list, so I'm thinking it's possible. From documentation:
Code: [Select]
// lets add an arbitrary amount here, just to show folks
// the power of a hook :)
$amount[1000] = array( 'value'    => 400,
   'label'     => 'Hook',
   'amount_id' => 1000 );
Looks pretty simple and arbitrary. Here is what I'm trying as of the moment:
Code: [Select]
function civicrm_membership_prices_civicrm_buildAmount ($pageType, &$form, &$amount) {
  $amount[4] = array(
    'id' => 4,
    'label'     => 'testing',
    'amount_id' => 225,
    'amount' => 150.00,
      'options' => array(
        4 => array(
        'id' => 4,
         'price_field_id' => 2,
         'name' => 'Event_Registration_Fee',
           'label' =>'test',
           'amount' => 150.00,
           'weight' => 3,
        ),
      ),
  );
  drupal_set_message('<pre>' . print_r($amount, true) . '</pre>');
}
I tried mimicking the example exactly, but had not luck. Here I am trying the structure found in the amount array. Maybe I'm missing something. Any thoughts or suggestions?
Leo Kacenjar
Web Developer
Open Media Foundation

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding Prices With hook_civicrm_buildAmount
March 11, 2011, 09:50:19 am

hey leo:

I know you can add / remove "amounts" from CiviEvent when not using pricesets (since amounts are basically just numbers!)

For pricesets, since each price field is linked to an actual database record, i suspect you are not able to add fields via the hook. I think the main purpose of the hook was to give people the ability to modify amounts within a price field. One potential workaround might be to add all possible fields and delete some of them via the hook.

I'm speculating a wee bit, but i do think i'm on the right track

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

lkacenja

  • I’m new here
  • *
  • Posts: 18
  • Karma: 2
Re: Adding Prices With hook_civicrm_buildAmount
March 16, 2011, 09:19:41 am
Thanks for the reply Lobo,

We ended up using normal fees instead. This granted the control we required (add/alter amounts). Here is the module:

http://drupalcode.org/sandbox/lkacenja/1094800.git/commit/458f95a

Creates a Drupal DB table to store event_id -> fee -> membership_id relationship. We then use several hooks and some core/template overrides to get the final effect. Maybe this would be useful for someone else.  :) Thanks again.
Leo Kacenjar
Web Developer
Open Media Foundation

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding Prices With hook_civicrm_buildAmount
March 16, 2011, 12:08:44 pm

hey leo:

any chance u can blog about it

Also can u explain why u needed core overrides. ideally we'd like to fix things so u can do this w/o overriding core :)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Adding Prices With hook_civicrm_buildAmount
March 17, 2011, 06:34:29 am
I have just finished doing something similar, but I did it by using groups and allowing the admin to assign permissions to price set options when they are created. Thus, my method will not work with standard prices, but only price sets.

I am considering submitting my code as a feature to CiviCRM, but I don't know which method makes the most sense in the long term. Any thoughts on this are welcome.
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding Prices With hook_civicrm_buildAmount
March 17, 2011, 10:45:21 am

quint:

how about packaging it as a module and then hosting it on drupal.org. That way the community can drive some features and better flush out (and contribute) more use cases

the past few modules released this way (event discount, webform) have been quite successful and have gotten a fairly broad exposure. Also gives us time to see what the various use cases are before potentially pulling it into core

finally, thanx for all your contributions and patches :) Definitely helps us and the community a whole lot

lobo
 
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

lkacenja

  • I’m new here
  • *
  • Posts: 18
  • Karma: 2
Re: Adding Prices With hook_civicrm_buildAmount
March 18, 2011, 07:52:34 am
Hi Again Lobo and qjensen,

Lobo: I'm always happy to support the Civi Community. I can blog something about this module if you think it would be useful for others. The core overrides were to add a membership type drop down to the fee form, and to change the prices on the event preview to reflect the users' membership. There may have been a way around using core overrides, but neither of us developers here at OMF are particularly well versed in html quickform or smarty.

qjensen: I think your method sounds interesting. We had a such a hard time manipulating the price sets that it made more sense for us to go the fees route. Also, our client needed this functionality to center around membership types, and for these relationships to be somewhat extensible. I feel like what we came up with was a pretty light weight solution (other than the overrides). How were you able to manipulate the price sets? Did your approach require any core overriding?
Leo Kacenjar
Web Developer
Open Media Foundation

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Adding Prices With hook_civicrm_buildAmount
March 18, 2011, 08:05:05 am
Lobo: That is a great idea. I will work on putting on d.o after the project is complete.

lkacenja: I didn't have to modify core after 3.3 added support for price sets to the buildAmount hook. In my first version (in late 2.x or early 3.0) I just created a hook for myself and added it to core so I would have the same functionality. That was a learning experience :) The latest version of my module is available on github at https://github.com/qjensen/CiviCRM-Member-Pricing.
 
My solution depends on adding members to groups for access control, but I wonder if memberships would be a better way to go. Smart groups don't work great because the must be manually updated or you don't get the results you expect (see discussion at http://forum.civicrm.org/index.php/topic,18657.msg77567.html#msg77567). Maybe we could collaborate and come up with a solution that incorporates the best of both?
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding Prices With hook_civicrm_buildAmount
March 18, 2011, 08:14:34 am

lkacenja / qjensen:

I think blogging about it and letting other folks know what u'll have done and how will help other people in the same boat (and i do think there are quite a few of them). Will also help the two of u'll (and others) to figure out how to combine forces and potentially come up with one module that meets both your needs without hacking core (and if needed we can add more hooks etc)

lkacenja: based on your brief description, i think u can accomplish both those tasks via hooks. BUT, we can hold off on any changes till we have a better idea of how you / qjensen will combine forces and then we can ensure that the "new" module is clean and does not need core overrides

really cool that we are seeing a lot more people extending civicrm :) definitely good signs for a healthy ecosystem

lobo


A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Adding Prices With hook_civicrm_buildAmount

This forum was archived on 2017-11-26.