Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
How can i update the cart page form with ajax? For example, if i wanna remove one product. I have tryed to do sg without any luck. I get the following error: "Undefined index: form_build_id in ajax_get_form()" There is a module called Commerce Cart Ajax where it is solved by using a fake link and enabling ajax in views but i dont believe this is the only way to replace the cart page form via ajax.
function MYMODULE_form_views_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'views_form_commerce_cart_form_default')) {
$ajax = array(
'callback' => 'MYMODULE_form_views_form_commerce_cart_form_ajax_submit',
'method' => 'replace',
);
$form['actions']['submit']['#ajax'] = $ajax;
if (!empty($form['edit_delete'])) {
foreach (element_children($form['edit_delete']) as $k) {
$form['edit_delete'][$k]['#ajax'] = $ajax;
}
}
}
}
function MYMODULE_form_views_form_commerce_cart_form_ajax_submit(&$form, &$form_state) {
$form_id = $form['#id'];
//$form = drupal_render(drupal_rebuild_form($form['#form_id'], $form_state, $form));
$form = commerce_cart_view();
$commands = array();
$commands[] = ajax_command_replace('#' . $form_id, $form);
return array('#type' => 'ajax', '#commands' => $commands);
}
Thx!
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedThere might be a better fix in https://www.drupal.org/project/commerce_checkout_ajax.