diff --git a/fulcrm_shoppingcart.admin.inc b/fulcrm_shoppingcart.admin.inc index d2222c7..fd93fa9 100644 --- a/fulcrm_shoppingcart.admin.inc +++ b/fulcrm_shoppingcart.admin.inc @@ -10,8 +10,8 @@ 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_checkout_product', $form_state[ 'values' ][ 'donation_checkout_product' ] ); - variable_set( 'fulcrm_shoppingcart_donation_solicited_product', $form_state[ 'values' ][ 'donation_solicited_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' ] ) ); drupal_set_message( t('Settings saved.'), 'status' ); } diff --git a/fulcrm_shoppingcart.module b/fulcrm_shoppingcart.module index 25d5e0e..b82f4ba 100644 --- a/fulcrm_shoppingcart.module +++ b/fulcrm_shoppingcart.module @@ -156,6 +156,9 @@ function fulcrm_shoppingcart_theme( $existing, $type, $theme, $path ) { 'fulcrm_shoppingcart_buy_eventbookingtype' => array( 'variables' => array( 'user_login_form' => null, 'fulcrm_shoppingcart_buy_eventbookingtype_form' => null ), // data returned from fulcrm API 'template' => 'fulcrm_shoppingcart_buy_eventbookingtype', ), + 'fulcrm_shoppingcart_buy_donation_page' => array( 'variables' => array( 'product' => null, 'fulcrm_shoppingcart_buy_donation_form' => null ), // data returned from fulcrm API + 'template' => 'fulcrm_shoppingcart_buy_donation_page', + ), ); } @@ -682,62 +685,67 @@ function fulcrm_shoppingcart_buy_eventbookingtype( $person, $eventbookingtype, $ return drupal_render( $form ); } -function fulcrm_shoppingcart_buy_donation_product_form_submit( $form, &$form_state ) { - // fulcrm_shoppingcart_buy_generic_product_form_submit( $form, $form_state ); - +function fulcrm_shoppingcart_buy_donation_form_submit( $form, &$form_state ) { $product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ]; + $donation_amount = floatval( $form_state[ 'values' ][ 'donation_amount' ] ); - $person_uuid = NULL; - if ( array_key_exists( 'person', $form_state[ 'fulcrm_shoppingcart' ] ) && - array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] ) ) - $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; + if ( $donation_amount > 0.0 ) { + $person_uuid = NULL; + if ( array_key_exists( 'person', $form_state[ 'fulcrm_shoppingcart' ] ) && + array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] ) ) + $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; - $cart = fulcrm_shoppingcart_get_session_cart(); + $cart = fulcrm_shoppingcart_get_session_cart(); - if ( is_array( $cart ) ) { - if ( $person_uuid ) { - $patch = FALSE; - if ( !array_key_exists( 'fulcrm', $cart[ 'd' ] ) || - !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ] ) || - !array_key_exists( $person_uuid, $cart[ 'd' ][ 'fulcrm' ][ 'person' ] ) ) { - $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ] = array( 'uuid' => $person_uuid ); - $patch = TRUE; + if ( is_array( $cart ) ) { + if ( $person_uuid ) { + $patch = FALSE; + if ( !array_key_exists( 'fulcrm', $cart[ 'd' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( $person_uuid, $cart[ 'd' ][ 'fulcrm' ][ 'person' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ] = array( 'uuid' => $person_uuid ); + $patch = TRUE; + } + if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_uuid; + $patch = TRUE; + } + if ( $patch ) { + $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => $cart[ 'd' ] ), + $query = array( 'expand' => 'd' ) ); + fulcrm_shoppingcart_invalidate_cache(); + } } - if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) || - !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ) { - $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_uuid; - $patch = TRUE; + + $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); + + $item = array( 'shoppingcart' => $cart[ 'url' ], + 'product' => fulcrm_apiv2_make_url( 'product', $product_id ), + 'quantity' => 1.0, + 'item' => 'donation', + 'item_net' => $donation_amount, + 'd' => array( 'name' => $shoppingitem_name, + ), + ); + + $api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) ); + fulcrm_shoppingcart_invalidate_cache(); + if ( $api_data[ 'success' ] ) { + drupal_set_message( t('Added your donation to your shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' ); + $form_state[ 'redirect' ] = 'fulcrm/cart'; + } else { + drupal_set_message( t('There was a problem adding your donation to your shopping cart.'), 'error' ); } - if ( $patch ) { - $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => $cart[ 'd' ] ), - $query = array( 'expand' => 'd' ) ); - fulcrm_shoppingcart_invalidate_cache(); - } - } - - $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); - - $item = array( 'shoppingcart' => $cart[ 'url' ], - 'product' => fulcrm_apiv2_make_url( 'product', $product_id ), - 'quantity' => 1.0, - 'd' => array( 'name' => $shoppingitem_name, - ), - ); - - $api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) ); - fulcrm_shoppingcart_invalidate_cache(); - if ( $api_data[ 'success' ] ) { - drupal_set_message( t('Added "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' ); - $form_state[ 'redirect' ] = 'fulcrm/cart'; } else { - drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + drupal_set_message( t('There was a problem adding your donation to your shopping cart.'), 'error' ); } } else { - drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + drupal_set_message( t('No donation added.'), 'warning' ); } } -function fulcrm_shoppingcart_buy_donation_product_form( $form, &$form_state, $person, $product ) { +function fulcrm_shoppingcart_buy_donation_form( $form, &$form_state, $person, $product ) { $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] = $person; $form_state[ 'fulcrm_shoppingcart' ][ 'product' ] = $product; @@ -747,12 +755,17 @@ function fulcrm_shoppingcart_buy_donation_product_form( $form, &$form_state, $pe '#markup' => '