Customers will be purposed an optional insurance in the shopping cart, calculated as a percentage of Cart Total. I added this feature as an editable field in a footer view of the shopping cart, and I'm trying to setup ajax callback to update the cart.
EDIT (@people who already read the question) : I solved the initial bugs, so this is a clean update. You can jump to item 7 if you don't want to read the full implementation...
What I've done so far :
- Created an insurance taxonomy, each term is an insurance option with a % decimal fields (in order to allow insurance options changes at any time).
- Added this taxonomy to the order fields with a term reference field "Insurance" .
- Created a very simple view block called
cart_field_assurances
with that Insurance field, with editable format. - Installed Cart View Override, cloned shoppping cart form and set that clone as shopping cart
Created a
views-view--commerce-cart-form-assurances.tpl.php
template with :print $rows; print views_embed_view('cart_field_assurances', 'block');
Moved insurance view before
#edit-actions
div with Jqueryvar insurance = $('.view-id-cart_field_assurances').detach(); insurance.prependTo('#edit-actions');
Removed action buttons, added ajax to the field, declared insurance price component
function vts_insurance_form_views_form_cart_field_assurances_block_alter(&$form, &$form_state, $form_id) { $ajax = array( 'callback' => 'vts_insurance_cart_refresh', 'progress' => array('type' => 'throbber', 'message' => ''), ); $form['field_order_assurances'][0]['field_order_assurances']['und']['#ajax'] = $ajax; //Hide buttons $form['field_order_assurances'][0]['actions']['submit']['#attributes']['class'] = array('hidden'); $form['actions']['#attributes']['class'] = array('hidden'); } //ajax callback function vts_insurance_cart_refresh(&$form, &$form_state) { //Load the order $view = $form_state['build_info']['args'][0]; $order_id = $view->result[0]->order_id; $order = commerce_order_load($order_id); //set new value $order->field_order_assurances['und'][0]['tid'] = $form_state['values']['field_order_assurances'][0]['field_order_assurances']['und'][0]['tid']; commerce_order_save($order); } //insurance price component function vts_insurance_commerce_price_component_type_info() { return array( 'insurance' => array( 'title' => t('Insurances'), 'display_title' => t('Insurances'), 'weight' => -10, ), ); }
Set up a rule : Before saving a commerce order, calculate the insurance amount and add it to total as an Insurances price component
To illustrate, this is the shopping cart form, with the optional insurance field below
The question
How to refresh the display of "Cart Commerce Order total" shopping form view (and its price components of course) in vts_insurance_cart_refresh
ajax callback ?
commerce_order_save(my_order);
looks like a syntax error$my_order
vsmy_order
is missing a$
You should post your actual code if you want people to help debug it.commerce_cart.module
plus Commerce Cart Ajax.hook_commerce_checkout_pane_info
and a price component withhook_commerce_price_component_type_info