function commerce_cart_blocks_ajax_callback in Commerce Cart Blocks 8
Ajax callback for variation product form.
Parameters
array $form: The form array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Return value
\Drupal\Core\Ajax\AjaxResponse The AJAX response.
1 string reference to 'commerce_cart_blocks_ajax_callback'
File
- ./
commerce_cart_blocks.module, line 265 - Contains commerce_cart_blocks.module.
Code
function commerce_cart_blocks_ajax_callback(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$ajax_settings = $form_state
->getValue('commerce_cart_blocks_ajax', [
'modal_enabled' => TRUE,
'modal_title' => 'Cart',
'append_block_id' => '',
]);
// Update cart blocks after adding an item to cart.
if (!$form_state
->getErrors()) {
$types = [
'commerce_cart_blocks_cart',
'commerce_cart_blocks_button',
];
foreach ($types as $block_type) {
$query = \Drupal::entityQuery('block')
->condition('plugin', $block_type);
foreach ((array) $query
->execute() as $block_id) {
$response
->addCommand(commerce_cart_blocks_render_cart_block_command($block_id));
}
}
// @todo Figure out why a page reload is sometimes required.
$cartCount = commerce_cart_blocks_cart_count();
$appendId = $ajax_settings['append_block_id'];
if ($cartCount === 1 && !empty($appendId)) {
$command = commerce_cart_blocks_render_cart_block_command_append('cart');
$appendCommand = new AfterCommand($appendId, $command);
$response
->addCommand($appendCommand);
}
if ($ajax_settings['modal_enabled']) {
$response
->addCommand(commerce_cart_blocks_show_cart_dialog_command($ajax_settings, $cartCount));
}
else {
$status_messages = [
'#type' => 'status_messages',
];
$messages = \Drupal::service('renderer')
->renderRoot($status_messages);
if (!empty($messages)) {
$response
->addCommand(new PrependCommand('.commerce-order-item-add-to-cart-form', $messages));
}
}
}
return $response;
}