Discussions

Donation and a Custom Price when Adding to Cart

I have watched rfay's screencast on creating a donation form with custom line items. I have actually created it on the current version of our site. It works wonderfully, however, in the new version of the site the form is a lot more advanced. Because of this I created a custom module to build the form (you can see it here: http://dev.feedingmatters.org/donate)

My question: is it possible to get this custom form's "donation" field to play nice with the rule I've already set up from the screen cast?

Right now it adds to the cart, but with a price of 0 using hook_cart_product_add, which is the products price so I am on the right track. I just need to know how to get it to talk to the rule.

Here is my code and rule if that helps

Module Code:

<?php
function donate_menu() {
    $menu['donate'] = array(
      'title' => 'Donate',
      'page callback' => 'drupal_get_form',
    'page arguments' => array('donate_form'),
      'access callback' => true,
    );

    return $menu;
}

function donate_form($form,&$form_submit) {
drupal_add_js(drupal_get_path('module', 'donate') . '/donate.js');

$state_list = array(
0 => "Select a State",
'AL' => "Alabama", 
'AK' => "Alaska", 
'AZ' => "Arizona", 
'AR' => "Arkansas", 
'CA' => "California", 
'CO' => "Colorado", 
'CT' => "Connecticut", 
'DE' => "Delaware", 
'DC' => "District Of Columbia", 
'FL' => "Florida", 
'GA' => "Georgia", 
'HI' => "Hawaii", 
'ID' => "Idaho", 
'IL' => "Illinois", 
'IN' => "Indiana", 
'IA' => "Iowa", 
'KS' => "Kansas", 
'KY' => "Kentucky", 
'LA' => "Louisiana", 
'ME' => "Maine", 
'MD' => "Maryland", 
'MA' => "Massachusetts", 
'MI' => "Michigan", 
'MN' => "Minnesota", 
'MS' => "Mississippi", 
'MO' => "Missouri", 
'MT' => "Montana",
'NE' => "Nebraska",
'NV' => "Nevada",
'NH' => "New Hampshire",
'NJ' => "New Jersey",
'NM' => "New Mexico",
'NY' => "New York",
'NC' => "North Carolina",
'ND' => "North Dakota",
'OH' => "Ohio", 
'OK' => "Oklahoma", 
'OR' => "Oregon", 
'PA' => "Pennsylvania", 
'RI' => "Rhode Island", 
'SC' => "South Carolina", 
'SD' => "South Dakota",
'TN' => "Tennessee", 
'TX' => "Texas", 
'UT' => "Utah", 
'VT' => "Vermont", 
'VA' => "Virginia", 
'WA' => "Washington", 
'WV' => "West Virginia", 
'WI' => "Wisconsin", 
'WY' => "Wyoming"
);

$form['text'] = array(
'#markup' => "<p>Make a tax-deductible contribution to Feeding Matters! We welcome all levels of donations, whether you are a long-time supporter or are giving for the first time—thank you for taking your place at the table to improve the lives of infants and children who struggle to eat.</p>"
);

$form['amount'] = array(
'#type' => 'fieldset',
'#weight' => 1,
'#collapsible' => FALSE,
'#collapsed' => FALSE
);

$form['amount']['header'] = array(
'#markup' => "<h3>Enter Amount</h3>"
);

  $form['amount']['donation'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter Amount'),
    '#required' => TRUE
  );
 
  $form['amount']['tax_credit'] = array(
    '#type' => 'checkbox',
    '#title' => t('AZ Tax Credit'),
    '#required' => FALSE
  );

  $form['amount']['email_card'] = array(
    '#type' => 'checkbox',
    '#title' => t('Email a Card'),
    '#required' => FALSE
  );
 
  $form['amount']['mail_card'] = array(
    '#type' => 'checkbox',
    '#title' => t('Mail a Card'),
    '#required' => FALSE
  );

  $form['amount']['email_a_card'] = array(
'#type' => 'fieldset',
'#weight' => 1,
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
 
  $form['amount']['email_a_card']['cards'] = array(
    '#type' => 'radios',
    '#required' => FALSE,
    '#options' => array(
    0 => '<img src="/sites/all/themes/feedingmatters/images/card1.jpg" alt="Card 1" />',
    1 => '<img src="/sites/all/themes/feedingmatters/images/card2.jpg" alt="Card 2" />'
    ),
    '#default_value' => 0,
  );  

  $form['amount']['email_a_card']['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Recipient Email Address'),
    '#required' => FALSE
  );   

  $form['amount']['email_a_card']['email_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('E-Card Subject'),
    '#required' => FALSE
  );   

  $form['amount']['email_a_card']['email_message'] = array(
    '#type' => 'textfield',
    '#title' => t('E-Card Message'),
    '#required' => FALSE
  );   

  $form['amount']['email_a_card']['email_date'] = array(
    '#type' => 'date',
    '#title' => t('Send E-Card on'),
    '#required' => FALSE
  );   
 
  $form['amount']['mail_a_card'] = array(
'#type' => 'fieldset',
'#weight' => 2,
'#title' => t('Recipient Information'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
 
  $form['amount']['mail_a_card']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => FALSE
  );   

  $form['amount']['mail_a_card']['street1'] = array(
    '#type' => 'textfield',
    '#title' => t('Address Line 1'),
    '#required' => FALSE
  );   

  $form['amount']['mail_a_card']['street2'] = array(
    '#type' => 'textfield',
    '#title' => t('Address Line 2'),
    '#required' => FALSE
  );   

  $form['amount']['mail_a_card']['city'] = array(
    '#type' => 'textfield',
    '#title' => t('City'),
    '#required' => FALSE
  );   

  $form['amount']['mail_a_card']['state'] = array(
    '#type' => 'select',
    '#title' => t('State'),
    '#options' => $state_list,
    '#required' => FALSE,
    '#attributes' =>array('placeholder' => t('Your Street Address'))
  );   

  $form['amount']['mail_a_card']['zipcode'] = array(
    '#type' => 'textfield',
    '#title' => t('Zip Code'),
    '#required' => FALSE,
  );   
 
  $form['amount']['mail_a_card']['card_info'] = array(
'#type' => 'fieldset',
'#weight' => 1,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#title' => t('Card Content'),
);

$form['amount']['mail_a_card']['card_info']['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Personal Message'),
    '#required' => FALSE,
  );

 
  $form['multiply'] = array(
'#type' => 'fieldset',
'#weight' => 2,
'#collapsible' => FALSE,
'#collapsed' => FALSE
);

$form['multiply']['header'] = array(
'#markup' => "<h3>Multiply Your Impact</h3>"
);

 
  $form['multiply']['recurring'] = array(
    '#type' => 'checkbox',
    '#title' => t('Make this a Monthly Donation'),
    '#required' => FALSE
  );
 
  $form['multiply']['honor'] = array(
    '#type' => 'checkbox',
    '#title' => t('Make this Donation'),
    '#required' => FALSE
  );
 
  $form['multiply']['honor_of'] = array(
    '#type' => 'select',
    '#title' => t(''),
    '#options' => array(
1 => t("in honor of:"),
2 => t("in memory of:")
),
    '#required' => FALSE
  );

  $form['multiply']['honor_name'] = array(
    '#type' => 'textfield',
    '#required' => FALSE,
    '#attributes' =>array('placeholder' => t('Enter Name Here'))
  );
 
  $form['multiply']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Checkout'),
    '#validate' => array('donate_validate'),
    '#submit' => array('donate_add_to_cart')
  );

  return $form;
}

function donate_validate($form, $form_state) {

}

function donate_add_to_cart($form, $form_state) {
donate_cart_product_add(1,1); // set to 1 because that is the donation pid, and always a qty of 1
}

function donate_cart_product_add($pid,$quantity=1) {
if ($product = commerce_product_load($pid) ) {
  global $user;
  $uid = $user->uid;
  $line_item = commerce_product_line_item_new($product, $quantity);
  $line_item = commerce_cart_product_add($uid, $line_item, FALSE);

}

Rule Export

{ "rules_update_price_to_donation_amount" : {
    "LABEL" : "Update price to donation amount",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "field_donation_amount" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:field-donation-amount" ],
            "op" : "*",
            "input_2" : "100"
          },
          "PROVIDE" : { "result" : { "donation_in_dollars" : "Donation amount in dollars" } }
        }
      },
      { "commerce_line_item_unit_price_amount" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : [ "donation-in-dollars" ],
          "component_name" : "base_price",
          "round_mode" : "0"
        }
      }
    ]
  }
}
Posted: Jun 15, 2013

Comments