Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions js/custom/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,30 @@
});
}

// Mousedown on hidden button to trigger price recalculation when postal code input is out of focus, because
// we can't override it in our theme hooks.
if ($('#edit-shipping-information-recalculate-shipping').length) {
// Do this on keyup because keydown will trigger and say the field is still full.
$('#shipping-information-wrapper').on('keyup', 'input', calculateHandler);
$('#shipping-information-wrapper').on('change', 'select', calculateHandler);
}
var recent = false;
function calculateHandler() {
if (!recent) {
// Loop through and ensure all the required fields are filled out.
var required_elements = $('input,select').filter('[required]:visible');
for (var i = 0; required_elements.length > i; i++) {
// If any aren't filled in return nothing.
if (required_elements[i]['value'] === '') {
return;
}
}
recent = true;
setTimeout(function () {
$('input[id^=edit-shipping-information-recalculate-shipping]').mousedown();
recent = false;
}, 2000);
}
}

})(jQuery, Drupal);
52 changes: 52 additions & 0 deletions orange_ecom_starter.theme
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Template\Attribute;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\image\Entity\ImageStyle;
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Url;
Expand All @@ -20,6 +21,9 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\comment\Entity\Comment;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Core\Render\Element;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Entity\Entity\EntityFormDisplay;

/**
* Implements hook_theme_suggestions_HOOK_alter().
Expand Down Expand Up @@ -263,6 +267,16 @@ function orange_ecom_starter_form_alter(&$form, FormStateInterface $form_state,
$form['login']['register']['register']['#button_type'] = 'primary';
}

if (isset($form['shipping_information']['recalculate_shipping'])) {
$form['shipping_information']['shipments'][0]['shipping_method']['widget'][0]['#ajax'] = [
'callback' => 'orange_ecom_starter_ajax_refresh_summary',
];
$form['shipping_information']['recalculate_shipping']['#attributes'] = [
'class' => array('hidden'),
];

}
// edit-shipping-information-shipping-profile-address-0-address-postal-code
// Login - Returning Customer.
if (isset($form['login']['returning_customer']['name'])) {
// Remove autofocus from username input.
Expand Down Expand Up @@ -548,3 +562,41 @@ function orange_ecom_starter_preprocess_swiftmailer(&$variables) {
function orange_ecom_starter_preprocess_commerce_cart_block(&$variables) {
$variables['attributes']['class'][] = 'cart--cart-block';
}

/**
* Ajax callback for refreshing the order shipping method.
*
* This will eventually be moved to the commerce shipping module instead.
*/
function orange_ecom_starter_ajax_refresh_summary(array $form, FormStateInterface $form_state) {
// Save the modified shipments.
$order = $form['sidebar']['order_summary']['summary']['#order_entity'];
$shipments = [];
$shipping = $form['shipping_information'];
foreach (Element::children($shipping['shipments']) as $index) {
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
$shipment = clone $shipping['shipments'][$index]['#shipment'];
$form_display = EntityFormDisplay::collectRenderDisplay($shipment, 'default');
$form_display->removeComponent('shipping_profile');
$form_display->removeComponent('title');
$form_display->extractFormValues($shipment, $shipping['shipments'][$index], $form_state);
$shipment->setShippingProfile($shipping['shipping_profile']['#profile']);
$shipment->save();
$shipments[] = $shipment;
}
$order->shipments = $shipments;
$order->save();

// Delete shipments that are no longer in use.
$removed_shipment_ids = $shipping['removed_shipments']['#value'];
if (!empty($removed_shipment_ids)) {
$shipment_storage = $this->entityTypeManager->getStorage('commerce_shipment');
$removed_shipments = $shipment_storage->loadMultiple($removed_shipment_ids);
$shipment_storage->delete($removed_shipments);
}
$response = new AjaxResponse();

$summary_element = $form['sidebar']['order_summary'];
$response->addCommand(new ReplaceCommand('[data-drupal-selector="edit-sidebar-order-summary"]', $summary_element));
return $response;
}
Loading