cache cart data to speed up page loads

master
Marek Isalski 7 years ago
parent 8564d3ee0a
commit a9d5ea9123

@ -13,6 +13,9 @@ function hook_fulcrm_shoppingcart_alter_person_add( $person ) {
function hook_fulcrm_shoppingcart_person_add( $person, $redirect_destination ) {
}
function hook_fulcrm_shoppingcart_person_add_guest( $person, $redirect_destination ) {
}
function hook_fulcrm_shoppingcart_pre_add_to_cart( $shoppingcart, $person, $product ) {
}
@ -144,12 +147,25 @@ function fulcrm_shoppingcart_theme( $existing, $type, $theme, $path ) {
);
}
function fulcrm_shoppingcart_invalidate_cache() {
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
$cid = 'fulcrm_shoppingcart:shoppingcart:' . $_SESSION[ 'fulcrm_shoppingcart_id' ];
cache_clear_all( $cid, 'cache' );
}
}
function fulcrm_shoppingcart_get_session_cart( $create = true ) {
global $user;
drupal_session_start();
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
$cid = 'fulcrm_shoppingcart:shoppingcart:' . $_SESSION[ 'fulcrm_shoppingcart_id' ];
if ( $cached = cache_get( $cid, 'cache' ) ) {
return $cached->data;
}
$api_data = fulcrm_apiv2_GET( 'shoppingcart/' . $_SESSION[ 'fulcrm_shoppingcart_id' ] . '/',
$query = array( 'expand' => implode( ',', array( 'shoppingitems',
'shoppingitems.d',
@ -157,11 +173,15 @@ function fulcrm_shoppingcart_get_session_cart( $create = true ) {
'shoppingitems.product' ) ) ) );
if ( $api_data[ 'success' ] ) {
if ( ( $api_data[ 'data' ][ 'completed' ] === null ) && ( $api_data[ 'data' ][ 'abandoned' ] === null ) ) {
cache_set( $cid, $api_data[ 'data' ], $bin = 'cache', $expire = time() + 300 );
return $api_data[ 'data' ];
} else {
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
}
} else {
if ( $api_data[ 'code' ] === 404 )
if ( $api_data[ 'code' ] === 404 ) {
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
}
}
}
@ -176,14 +196,22 @@ function fulcrm_shoppingcart_get_session_cart( $create = true ) {
$cart_data = array( 'person' => $person_url, 'd' => array( 'fulcrm_testing_data' => 'this is a test' ) );
$api_data = fulcrm_apiv2_POST( 'shoppingcart/', $cart_data );
$api_data = fulcrm_apiv2_POST( 'shoppingcart/', $cart_data, $query = array( 'expand' => 'd' ) );
if ( $api_data[ 'success' ] ) {
$_SESSION[ 'fulcrm_shoppingcart_id' ] = $api_data[ 'data' ][ 'id' ];
$cid = 'fulcrm_shoppingcart:shoppingcart:' . $api_data[ 'data' ][ 'id' ];
cache_set( $cid, $api_data[ 'data' ], $bin = 'cache', $expire = time() + 300 );
return $api_data[ 'data' ];
}
}
}
function fulcrm_shoppingcart_session_cart_count_items() {
if ( $cart_data = fulcrm_shoppingcart_get_session_cart(false) )
return count( $cart_data[ 'shoppingitems' ] );
return 0;
}
function fulcrm_shoppingcart_coupon_url( $transaction_id ) {
$hmac = fulcrm_crypto_object_id_hmac( 'transaction', $transaction_id );
return url( 'fulcrm/checkout/coupon/' . $transaction_id . '/' . $hmac );
@ -220,6 +248,27 @@ function fulcrm_shoppingcart_person_add_form_submit( $form, &$form_state ) {
}
}
function fulcrm_shoppingcart_person_add_guest_form_submit( $form, &$form_state ) {
$person_d = fulcrm_collection_form_values_to_d( 'person', $form_state[ 'values' ] );
$person_data = array( 'd' => $person_d );
if ( array_key_exists( 'name', $person_d ) ) {
if ( is_array( $person_d[ 'name' ] ) )
$person_data[ 'name' ] = $person_d[ 'name' ][ 0 ];
elseif ( is_string( $person_d[ 'name' ] ) )
$person_data[ 'name' ] = $person_d[ 'name' ];
}
$person_data[ 'uuid' ] = fulcrm_uuid_uuid4();
$person_data = module_invoke_all( 'fulcrm_shoppingcart_alter_person_add_guest', $person_data );
$api_data = fulcrm_apiv2_POST( 'person/', $person_data, $query = array( 'expand' => 'd' ) );
if ( $api_data[ 'success' ] ) {
module_invoke_all( 'fulcrm_shoppingcart_person_add', $api_data[ 'data' ], array_key_exists( 'destination', $_GET ) ? $_GET[ 'destination' ] : variable_get( 'fulcrm_shoppingcart_proceed_url', url('<front>') ) );
}
}
function fulcrm_shoppingcart_person_add_form( $form, &$form_state, $collection, $destination ) {
$form_state[ 'fulcrm_shoppingcart' ][ 'collection' ] = $collection;
$form[ 'title_event' ] = array( '#type' => 'markup',
@ -323,6 +372,7 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, &$form_sta
);
$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, $person_data[ 'data' ], $api_data[ 'data' ] );
foreach ( $hook_results as $hook_result )
@ -344,7 +394,16 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, &$form_sta
function fulcrm_shoppingcart_buy_eventbookingtype_login_form_submit( $form, &$form_state ) {
user_login_submit( $form, $form_state );
$form_state[ 'redirect' ] = fulcrm_shoppingcart_buy_url( $form_state[ 'fulcrm_shoppingcart_redirect_buy_product_id' ] );
$form_state[ 'redirect' ] = fulcrm_shoppingcart_buy_url( $form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] );
}
function fulcrm_shoppingcart_buy_eventbookingtype_login_or_add_person_form_submit( $form, &$form_state ) {
if ( $login ) {
user_login_submit( $form, $form_state );
$form_state[ 'redirect' ] = fulcrm_shoppingcart_buy_url( $form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] );
} elseif ( $add ) {
}
}
function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $person, $eventbookingtype, $product ) {
@ -357,15 +416,19 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $pe
if ( is_null( $person_id ) ) {
switch ( variable_get( 'fulcrm_shoppingcart_experience', 'force' ) ) {
case 'guest':
break;
case 'user':
if ( user_is_anonymous() ) {
$form = user_login( $form, $form_state );
$form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] = $product[ 'id' ];
$form[ '#submit' ][] = 'fulcrm_shoppingcart_buy_eventbookingtype_login_or_add_person_form_submit';
}
break;
case 'force':
if ( user_is_anonymous() ) {
drupal_set_message( t('You need to be logged in to continue shopping.'), 'error' );
drupal_set_message( t('You need to be logged in to continue shopping.'), 'error' ); // XXX change to be on form
$form = user_login( $form, $form_state );
$form_state[ 'fulcrm_shoppingcart_redirect_buy_product_id' ] = $product[ 'id' ];
$form['#submit'][] = 'fulcrm_shoppingcart_buy_eventbookingtype_login_form_submit';
$form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] = $product[ 'id' ];
$form[ '#submit' ][] = 'fulcrm_shoppingcart_buy_eventbookingtype_login_form_submit';
return $form;
} else {
drupal_set_message( t('Sorry! There was a problem looking up your record, and this means we cannot complete this order.'), 'error' );
@ -475,6 +538,7 @@ function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_stat
),
),
$query = array( 'expand' => 'd' ) );
fulcrm_shoppingcart_invalidate_cache();
}
$shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] );
@ -487,6 +551,7 @@ function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_stat
);
$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';
@ -538,10 +603,11 @@ function fulcrm_shoppingcart_buy( $product_id, $hmac ) {
return 'error while fetching information about this product!';
}
if ( user_is_logged_in() )
if ( user_is_logged_in() ) {
$person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' );
else
} else {
$person_id = null;
}
if ( $person_id ) {
$person_data = fulcrm_apiv2_GET( 'person/' . $person_id . '/',
@ -602,7 +668,7 @@ 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' ] );
fulcrm_shoppingcart_invalidate_cache();
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' );
@ -677,8 +743,10 @@ function fulcrm_shoppingcart_coupon_form_submit( $form, &$form_state ) {
$transaction_id = $form_state[ 'fulcrm_shoppingcart' ][ 'transaction_id' ];
$coupon_code = $form_state[ 'values' ][ 'coupon_code' ];
if ( $coupon_code && $transaction_id ) {
$paymentitem_data = fulcrm_apiv2_POST( 'transaction/' . $transaction_id . '/coupon/', array(), array( 'coupon_code' => $coupon_code,
'expand' => implode( ',', array( 'd' ) ) ) );
$paymentitem_data = fulcrm_apiv2_POST( 'transaction/' . $transaction_id . '/coupon/',
array(),
array( 'coupon_code' => $coupon_code,
'expand' => implode( ',', array( 'd' ) ) ) );
if ( $paymentitem_data[ 'success' ] ) {
if ( count( $paymentitem_data[ 'data' ] ) ) {
@ -735,6 +803,7 @@ function fulcrm_shoppingcart_checkout() {
return $hook_result;
$transaction_data = fulcrm_apiv2_POST( $cart[ 'url' ] . 'transaction/', array(), array( 'expand' => implode( ',', array( 'd' ) ) ) );
fulcrm_shoppingcart_invalidate_cache();
if ( $transaction_data[ 'success' ] ) {
$hmac = fulcrm_crypto_object_id_hmac( 'transaction', $transaction_data[ 'data' ][ 'id' ] );
@ -749,6 +818,7 @@ function fulcrm_shoppingcart_checkout() {
'lineitems',
'paymentitems',
) ) ) );
fulcrm_shoppingcart_invalidate_cache();
if ( $transaction_data2[ 'success' ] ) {
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_post_cart_to_transaction', $transaction_data2[ 'data' ] );
foreach ( $hook_results as $hook_result )
@ -766,6 +836,7 @@ function fulcrm_shoppingcart_checkout() {
function fulcrm_shoppingcart_checkout_success( $transaction_id, $hmac ) {
if ( fulcrm_crypto_check_object_id_hmac( 'transaction', $transaction_id, $hmac ) ) {
fulcrm_shoppingcart_invalidate_cache();
$transaction_data = fulcrm_apiv2_GET( 'transaction/' . $transaction_id . '/',
$query = array( 'expand' => implode( ',', array( 'd',
'lineitems',
@ -782,6 +853,10 @@ function fulcrm_shoppingcart_checkout_success( $transaction_id, $hmac ) {
foreach ( $hook_results as $hook_result )
return $hook_result;
drupal_set_message( t('Thank you for your order.'), 'status' );
} else if ( $transaction_data[ 'data' ][ 'submitted' ] ) {
drupal_set_message( t('Payment has not yet been processed.'), 'warning' );
} else {
drupal_set_message( t('This transaction has not yet completed.'), 'warning' );
}
return theme( 'fulcrm_shoppingcart_transaction_complete', array( 'transaction' => $transaction_data[ 'data' ] ) );
} else {
@ -792,6 +867,7 @@ function fulcrm_shoppingcart_checkout_success( $transaction_id, $hmac ) {
function fulcrm_shoppingcart_checkout_failure( $transaction_id, $hmac ) {
if ( fulcrm_crypto_check_object_id_hmac( 'transaction', $transaction_id, $hmac ) ) {
fulcrm_shoppingcart_invalidate_cache();
$transaction_data = fulcrm_apiv2_GET( 'transaction/' . $transaction_id . '/',
$query = array( 'expand' => implode( ',', array( 'd',
'lineitems',
@ -814,6 +890,7 @@ function fulcrm_shoppingcart_checkout_failure( $transaction_id, $hmac ) {
}
function fulcrm_shoppingcart_user_logout( $account ) {
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) )
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
}
}

Loading…
Cancel
Save