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' ]; - - $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(); - - 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(); + $donation_amount = floatval( $form_state[ 'values' ][ 'donation_amount' ] ); + + 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(); + + 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(); + } } - } - $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); + $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, - ), - ); + $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 "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' ); - $form_state[ 'redirect' ] = 'fulcrm/cart'; + $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' ); + } } 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' => '

' . check_plain( $product[ 'name' ] ) . '

', ); + $form[ 'donation_amount' ] = array( '#type' => 'textfield', + '#title' => 'Donation Amount (' . $product[ '_price' ][ 'currency' ] . ')', + '#default_value' => sprintf( "%.2f", $product[ '_price' ][ 'item_net' ] ), + ); + drupal_set_title( check_plain( $product[ 'data' ][ 'name' ] ) ); $form[ 'actions' ] = array( '#type' => 'actions' ); $form[ 'actions' ][ 'save' ] = array( '#type' => 'submit', '#value' => t('Add to Cart'), - '#submit' => array( 'fulcrm_shoppingcart_buy_donation_product_form_submit' ), + '#submit' => array( 'fulcrm_shoppingcart_buy_donation_form_submit' ), ); return $form; @@ -763,12 +776,9 @@ function fulcrm_shoppingcart_buy_donation( $person, $product ) { if ( array_key_exists( 'id', $person ) ) $person_id = $person[ 'id' ]; - $fulcrm_shoppingcart_buy_eventbookingtype_form = drupal_get_form( 'fulcrm_shoppingcart_buy_donation_form', $person, $product ); - - return theme( 'fulcrm_shoppingcart_buy_donation', array( 'fulcrm_shoppingcart_buy_donation_form' => $fulcrm_shoppingcart_buy_donation_form ) ); + $fulcrm_shoppingcart_buy_donation_form = drupal_get_form( 'fulcrm_shoppingcart_buy_donation_form', $person, $product ); - $form = drupal_get_form( 'fulcrm_shoppingcart_buy_eventbookingtype_form', $person, $eventbookingtype, $product ); - return drupal_render( $form ); + return theme( 'fulcrm_shoppingcart_buy_donation_page', array( 'product' => $product, 'fulcrm_shoppingcart_buy_donation_form' => $fulcrm_shoppingcart_buy_donation_form ) ); } function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_state ) { @@ -898,11 +908,13 @@ function _fulcrm_shoppingcart_buy( $product_id ) { return $hook_result; if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) { - switch ( $product_data[ 'data' ][ 'id' ] ) { - case variable_set( 'fulcrm_shoppingcart_donation_checkout_product', '' ): - case variable_set( 'fulcrm_shoppingcart_donation_solicited_product', '' ): + $checkout_product_id = variable_get( 'fulcrm_shoppingcart_donation_checkout_product', 0 ); + if ( ( $checkout_product_id > 0 ) && ( $product_data[ 'data' ][ 'id' ] == $checkout_product_id ) ) + return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] ); + + $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' ] ); - } } switch ( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ? fulcrm_apiv2_url_to_type( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ) : null ) { diff --git a/fulcrm_shoppingcart_buy_donation_page.tpl.php b/fulcrm_shoppingcart_buy_donation_page.tpl.php new file mode 100644 index 0000000..dfb48ee --- /dev/null +++ b/fulcrm_shoppingcart_buy_donation_page.tpl.php @@ -0,0 +1 @@ + diff --git a/fulcrm_shoppingcart_cart_item.tpl.php b/fulcrm_shoppingcart_cart_item.tpl.php index 7a00586..f1decf3 100644 --- a/fulcrm_shoppingcart_cart_item.tpl.php +++ b/fulcrm_shoppingcart_cart_item.tpl.php @@ -1,5 +1,5 @@ - +