From c6c655cdea1d5d7d34be1bd714b5cc7b86302c0b Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sun, 27 Mar 2016 15:58:34 +0100 Subject: [PATCH] can now remove items from a cart --- fulcrm_shoppingcart.module | 63 +++++++++++++++++++++++++++ fulcrm_shoppingcart_cart_item.tpl.php | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/fulcrm_shoppingcart.module b/fulcrm_shoppingcart.module index dd6e930..7208bcb 100644 --- a/fulcrm_shoppingcart.module +++ b/fulcrm_shoppingcart.module @@ -33,6 +33,12 @@ function fulcrm_shoppingcart_menu() { 'access callback' => 'user_access', 'access arguments' => array('access content'), ); + $items[ 'fulcrm/cart/remove/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_remove', + 'page arguments' => array(3,4), + 'type' => MENU_CALLBACK, + 'access callback' => 'user_access', + 'access arguments' => array('access content'), + ); $items[ 'fulcrm/cart' ] = array( 'page callback' => 'fulcrm_shoppingcart_cart', 'type' => MENU_CALLBACK, 'access callback' => 'user_access', @@ -102,6 +108,11 @@ function fulcrm_shoppingcart_buy_url( $product_id ) { return url( 'fulcrm/buy/' . $product_id . '/' . $hmac ); } +function fulcrm_shoppingcart_remove_url( $shoppingitem_id ) { + $hmac = fulcrm_crypto_object_id_hmac( 'shoppingitem', $shoppingitem_id ); + return url( 'fulcrm/cart/remove/' . $shoppingitem_id . '/' . $hmac ); +} + function fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, &$form_state ) { // $person_d = fulcrm_collection_form_values_to_d( 'person', $form_state[ 'values' ] ); @@ -299,6 +310,58 @@ function fulcrm_shoppingcart_buy( $product_id, $hmac ) { } } +function fulcrm_shoppingcart_remove_shoppingitem_confirm_submit( $form, &$form_state ) { + $api_data = fulcrm_apiv2_DELETE( $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'url' ] ); + + if ( $api_data[ 'success' ] ) + drupal_set_message( t('Removed "%item" from cart.', array( '%item' => ( array_key_exists( 'name', $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'd' ] ) ? $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'd' ][ 'name' ] : $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'product' ][ 'name' ] ) ) ), 'status' ); + + $form_state[ 'redirect' ] = 'fulcrm/cart'; +} + + +function fulcrm_shoppingcart_remove_shoppingitem_confirm( $form, &$form_state, $shoppingitem ) { + $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ] = $shoppingitem; + + return confirm_form( $form, + t('Remove from cart?'), + 'fulcrm/cart', + t('Are you sure you want to remove "%item" from your shopping cart?', array( '%item' => ( array_key_exists( 'name', $shoppingitem[ 'd' ] ) ? $shoppingitem[ 'd' ][ 'name' ] : $shoppingitem[ 'product' ][ 'name' ] ) ) ), + t('Remove'), + t('Cancel') + ); +} + +function fulcrm_shoppingcart_remove( $shoppingitem_id, $hmac ) { + if ( fulcrm_crypto_check_object_id_hmac( 'shoppingitem', $shoppingitem_id, $hmac ) ) { + $shoppingitem_data = fulcrm_apiv2_GET( 'shoppingitem/' . $shoppingitem_id . '/', + $query = array( 'expand' => implode( ',', array( 'd', + 'product', + 'content_object', + 'shoppingcart', + ) ) ) ); + + if ( $shoppingitem_data[ 'success' ] ) { + if ( $shoppingitem_data[ 'data' ][ 'shoppingcart' ][ 'completed' ] ) { + drupal_set_message( t('Shopping cart has been ordered. You can\'t remove things now!'), 'error' ); + drupal_goto( '' ); + return; + } + + if ( $shoppingitem_data[ 'data' ][ 'shoppingcart' ][ 'abandoned' ] ) { + drupal_set_message( t('Sorry, your shopping cart has timed out. Please restart your purchase.'), 'error' ); + drupal_goto( variable_get( 'fulcrm_shoppingcart_proceed_url', '' ) ); + return; + } + + return drupal_get_form( 'fulcrm_shoppingcart_remove_shoppingitem_confirm', $shoppingitem_data[ 'data' ] ); + } else { + drupal_set_message( t('Cannot remove item: it\'s already gone?'), 'error' );; + drupal_goto( 'fulcrm/cart' ); + } + } +} + function fulcrm_shoppingcart_cart() { $cart = fulcrm_shoppingcart_get_session_cart( false ); return theme( 'fulcrm_shoppingcart_cart', array( 'cart' => $cart ) ); diff --git a/fulcrm_shoppingcart_cart_item.tpl.php b/fulcrm_shoppingcart_cart_item.tpl.php index 5294029..3f0788d 100644 --- a/fulcrm_shoppingcart_cart_item.tpl.php +++ b/fulcrm_shoppingcart_cart_item.tpl.php @@ -1,5 +1,5 @@ - +