I sorta found a workaround for this by hooking into hook_views_ajax_data_alter to do a str_replace
on the $command['data']
to change the add-to-cart form actions in the actual markup.
In my case, (using Better Exposed Filters to print out filter options as links instead of radio buttons) selecting each exposed filter option re-renders several product teasers each with add-to-cart forms, so there are several places in the $command['data']
markup that this str_replace
takes place.
This does successfully change the add-to-cart forms' #action from 'action="views/ajax"' to 'action="[current page url]"'.
However when you try to actually add an item to your cart, this seemingly correct form action still only refreshes the page and does not add the item to your cart.
Through trial and error, I discovered that if the new form action contains the exposed filter identifier and filter id number (taxonomy term name and term id, respectively, in my case) in the querystring like this: http://www.example.com/view-with-exposed-filter?filter-identifier=123; then and only then will it actually add the item to the cart.
However (as form actions do) this posts the form to that page with the querystring and subsequent exposed filter selections and view re-renderings (via AJAX) will not change the url to use the currently selected filter id number in the querysting, so when the add-to-cart form posts again, the item will not be added to the cart unless the item you're adding belongs to the filter id (taxonomy term) in the url.
FWIW, here's my code. Maybe someone can help me better understand why the AJAX rendered add-to-cart forms must supply the correct exposed form filter identifier in the form action to actually add the item to the cart instead of just refreshing the page... while this isn't the case on the initial (non-AJAX) load of a view with exposed form filters where add-to-cart forms work without the filter id specified in the form action.
/**
* Implements hook_views_ajax_data_alter()
*/
function hook_views_ajax_data_alter(&$commands, $view) {
// Get the variable options from the view object.
$options = $view->query->pager->display->display_options;
// Make sure this view is using ajax and BEF exposed filters
$use_exposed = (isset($options['use_ajax']) && isset($options['exposed_form']) && $options['exposed_form']['type'] == 'better_exposed_filters') ? TRUE : FALSE;
// In the command's data replace 'action="/views/ajax"' to 'action="[current page url]"'
// See: views_ajax_fade contrib module
if ($use_exposed) {
foreach ($commands as &$command) {
if (isset($command['method']) && $command['method'] == 'replaceWith') {
$path_alias = url(drupal_lookup_path('alias', current_path()));
//$path_alias = 'http://www.example.com/view-with-exposed-filter?filter-identifier=123';
$command['data'] = str_replace('action="/views/ajax"', 'action="'.$path_alias.'"', $command['data']);
}
}
}
}
References: