now able to add a donation to an event booking during purchase

master
Marek Isalski 6 years ago
parent 83803d6dd1
commit 268695c321

@ -10,6 +10,7 @@ function fulcrm_shoppingcart_admin_form_submit( $form, &$form_state ) {
variable_set( 'fulcrm_shoppingcart_experience', $form_state[ 'values' ][ 'experience' ] );
variable_set( 'fulcrm_shoppingcart_donations', $form_state[ 'values' ][ 'donation_enabled' ] );
variable_set( 'fulcrm_shoppingcart_donation_cart_product', intval( $form_state[ 'values' ][ 'donation_cart_product' ] ) );
variable_set( 'fulcrm_shoppingcart_donation_checkout_product', intval( $form_state[ 'values' ][ 'donation_checkout_product' ] ) );
variable_set( 'fulcrm_shoppingcart_donation_solicited_product', intval( $form_state[ 'values' ][ 'donation_solicited_product' ] ) );
@ -80,6 +81,11 @@ function fulcrm_shoppingcart_admin_form( $form, &$form_state ) {
'#title' => 'Donations enabled?',
'#default_value' => variable_get( 'fulcrm_shoppingcart_donations', false ),
);
$form[ 'donation' ][ 'donation_cart_product' ] = array( '#type' => 'select',
'#title' => 'Donation Product for "Add to Cart"',
'#default_value' => variable_get( 'fulcrm_shoppingcart_donation_cart_product', '' ),
'#options' => $products,
);
$form[ 'donation' ][ 'donation_checkout_product' ] = array( '#type' => 'select',
'#title' => 'Donation Product for Checkout',
'#default_value' => variable_get( 'fulcrm_shoppingcart_donation_checkout_product', '' ),

@ -337,6 +337,42 @@ function fulcrm_shoppingcart_person_add( $collection_id, $hmac ) {
}
}
function _fulcrm_shoppingcart_buy_donation_create_shoppingitem( &$form_state, &$cart, $donation_for_shoppingitem ) {
if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$donation_amount = floatval( $form_state[ 'values' ][ 'donation_amount' ] );
if ( $donation_amount < 0.0 )
drupal_set_message(t('A negative donation would rather defeat the point!'), 'error', FALSE);
if ( $donation_amount <= 0.0 )
return;
$cart_product_id = variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 );
if ( $cart_product_id > 0 ) {
$name = 'Donation of ' . sprintf( '%.2f', $donation_amount ) . ' for "' . $donation_for_shoppingitem[ 'd' ][ 'name' ] . '"';
$item = array( 'shoppingcart' => $cart[ 'url' ],
'product' => fulcrm_apiv2_make_url( 'product', $cart_product_id ),
'quantity' => 1.0,
'item' => 'donation',
'item_net' => $donation_amount,
'content_object' => $donation_for_shoppingitem[ 'url' ],
'd' => array( 'name' => $name ),
);
$api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) );
fulcrm_shoppingcart_invalidate_cache();
if ( $api_data[ 'success' ] ) {
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_post_add_to_cart', $cart, $api_data[ 'data' ] );
foreach ( $hook_results as $hook_result )
return $hook_result;
drupal_set_message( t('Added "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' );
} else {
drupal_set_message( t('There was a problem adding your donation to the shopping cart.'), 'error' );
}
}
}
}
function _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( &$form_state, $cart, $person_uuid, $person_name, $product_id, $made_choices = array() ) {
$all_choices = array();
@ -377,9 +413,12 @@ function _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( &$form_s
drupal_set_message( t('Added "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' );
$form_state[ 'redirect' ] = 'fulcrm/cart';
_fulcrm_shoppingcart_buy_donation_create_shoppingitem( $form_state, $cart, $api_data[ 'data' ] );
} else {
drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' );
}
} else {
drupal_set_message( t('Cannot find your record\'s UUID. This order might have problems...'), 'error' );
}
}
@ -508,6 +547,32 @@ function fulcrm_shoppingcart_event_has_capacity( $event_id ) {
return TRUE;
}
function _fulcrm_shoppingcart_buy_donation_product_update_form( &$form, &$form_state, $donation_product_id ) {
if ( ( $donation_product_id > 0 ) && variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$product_data = fulcrm_apiv2_GET( 'product/' . $donation_product_id . '/',
$query = array( 'expand' => implode( ',', array( 'content_object',
'selector_object',
'person_ddatacollection',
'organisation_ddatacollection',
'thing_ddatacollection',
'transaction_ddatacollection',
'lineitem_ddatacollection',
) ) ) );
if ( $product_data[ 'success' ] ) {
$product = $product_data[ 'data' ] ;
$form[ 'donation' ] = array( '#type' => 'fieldset',
'#title' => 'Donation',
);
$form[ 'donation' ][ 'donation_amount' ] = array( '#type' => 'textfield',
'#title' => 'Donation Amount (' . $product[ '_price' ][ 'currency' ] . ')',
'#default_value' => sprintf( "%.2f", $product[ '_price' ][ 'item_net' ] ),
);
}
}
}
function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $person, $eventbookingtype, $product ) {
$form = array();
@ -618,6 +683,8 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $pe
$form[ 'eventslots' ][ '#title' ] = 'Itinerary';
$form[ 'eventslots' ][ '#type' ] = 'fieldset';
_fulcrm_shoppingcart_buy_donation_product_update_form( $form, $form_state, variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 ) );
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Add to Cart'),
@ -846,6 +913,8 @@ function fulcrm_shoppingcart_buy_generic_product_form( $form, &$form_state, $per
drupal_set_title( check_plain( $product[ 'data' ][ 'name' ] ) );
_fulcrm_shoppingcart_buy_donation_product_update_form( $form, $form_state, variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 ) );
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
'#value' => t('Add to Cart'),
@ -856,6 +925,8 @@ function fulcrm_shoppingcart_buy_generic_product_form( $form, &$form_state, $per
}
function _fulcrm_shoppingcart_buy( $product_id ) {
global $user;
$product_data = fulcrm_apiv2_GET( 'product/' . $product_id . '/',
$query = array( 'expand' => implode( ',', array( 'content_object',
'selector_object',
@ -915,6 +986,10 @@ function _fulcrm_shoppingcart_buy( $product_id ) {
$solicited_product_id = variable_get( 'fulcrm_shoppingcart_donation_solicited_product', 0 );
if ( ( $solicited_product_id > 0 ) && ( $product_data[ 'data' ][ 'id' ] == $solicited_product_id ) )
return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] );
$cart_product_id = variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 );
if ( ( $cart_product_id > 0 ) && ( $product_data[ 'data' ][ 'id' ] == $cart_product_id ) )
return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] );
}
switch ( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ? fulcrm_apiv2_url_to_type( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ) : null ) {
@ -1187,6 +1262,7 @@ function fulcrm_shoppingcart_checkout_failure( $transaction_id, $hmac ) {
function fulcrm_shoppingcart_user_logout( $account ) {
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
// XXX cancel transaction if one exists?
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
}
}

Loading…
Cancel
Save