Skip to content

Commit 66493e8

Browse files
author
David Alexa
committed
first delivery method is checked by default, improved JS behaviour
1 parent 9f765fc commit 66493e8

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/Sylius/Bundle/CoreBundle/Checkout/Step/PaymentShippingStep.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ public function forwardAction(ProcessContextInterface $context)
5555

5656
$this->dispatchCheckoutEvent(SyliusCheckoutEvents::SHIPPING_INITIALIZE, $order);
5757
$formShippingPre = $this->createCheckoutShippingForm($order, null);
58-
$formShippingPre->handleRequest($request);
5958

6059
if ($formShippingPre->get('country')->getData() === null) {
6160
$choices = $formShippingPre->get('country')->getConfig()->getOption('choice_list')->getChoices();
6261
$formShippingPre->get('country')->setData(reset($choices));
6362
}
64-
65-
$this->dispatchCheckoutEvent(SyliusCheckoutEvents::SHIPPING_INITIALIZE, $order);
66-
$formShipping = $this->createCheckoutShippingForm($order, $formShippingPre->get('country')->getData());
67-
$formShipping->handleRequest($request);
68-
63+
$formShippingPre->handleRequest($request);
6964

7065
$this->dispatchCheckoutEvent(SyliusCheckoutEvents::ADDRESSING_INITIALIZE, $order);
7166
if (is_null($order->getShippingAddress()) || $order->getShippingAddress()->getFirstName() == 'anon.') {

src/Sylius/Bundle/WebBundle/Resources/public/js/payment_shipping.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@
44

55
$(document).ready(function() {
66
$("body").on('change, click', '#shippings input[type="radio"]', function() {
7-
var $addressing = $("#addressing");
8-
var requiredIds = $("#shippings").data('shippingsRequiresAddressing');
9-
var id = parseInt($(this).val(), 10);
10-
11-
if (requiredIds.ids.indexOf(id) !== -1) {
12-
$addressing.removeClass('js-hidden');
13-
} else {
14-
$addressing.addClass('js-hidden');
15-
}
7+
toggleAddressing($(this));
168
}).on('change', '#sylius_checkout_shipping_country_specific_country, #sylius_checkout_addressing_shippingAddress_country', function() {
179
var $form = $(this).parents('form');
1810
var $input = $form.prepend($("<input/>").attr('type', 'hidden').attr('name', 'doNotForward').val(true));
@@ -41,9 +33,20 @@ $(document).ready(function() {
4133
});
4234

4335
function checkFirstShipping() {
44-
if (!$("#shippings input[type='radio']:checked").length) {
45-
$("#shippings input[type='radio']").first().click();
36+
toggleAddressing($("#shippings input[type='radio']:checked"));
37+
}
38+
39+
function toggleAddressing($selectedShipping) {
40+
var $addressing = $("#addressing");
41+
var requiredIds = $("#shippings").data('shippingsRequiresAddressing');
42+
var id = parseInt($selectedShipping.val(), 10);
43+
44+
if (requiredIds.ids.indexOf(id) !== -1) {
45+
$addressing.removeClass('js-hidden');
46+
} else {
47+
$addressing.addClass('js-hidden');
4648
}
4749
}
50+
4851
checkFirstShipping();
4952
});

src/Sylius/Bundle/WebBundle/Resources/views/Common/_forms.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@
124124
<span class="pull-right text-muted">{{ shipping_costs[child.vars.value]|sylius_price }}</span>
125125
{% endif %}
126126
<label{% for attrname, attrvalue in child.vars.label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
127+
{% if loop.first %}
128+
{{ form_widget(child, {'attr': {'checked': 'checked'}}) }}
129+
{% else %}
127130
{{ form_widget(child) }}
128-
{{ child.vars.label|trans({}, translation_domain) }}
131+
{% endif %}
132+
{{ child.vars.label|trans({}, translation_domain) }}
129133
</label>
130134
</div>
131135
{% endfor %}

src/Sylius/Bundle/WebBundle/Resources/views/Frontend/Checkout/Step/PaymentShipping/shipping.html.twig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
{{ form_row(form.country, {'attr': {'autocomplete': 'off'}}) }}
99
{% for shipment in order.shipments %}
1010
{% set shipmentForm = form.shipments[loop.index0] %}
11-
{% if loop.first %}{{ form_errors(shipmentForm.method) }}{% endif %}
12-
11+
{{ form_errors(shipmentForm.method) }}
1312
<div class="no-cols clearfix checkbox-row">
14-
{{ form_widget(shipmentForm.method, {'attr': {'checked': 'checked'}}) }}
13+
{{ form_widget(shipmentForm.method) }}
1514
</div>
1615

1716
{% endfor %}

0 commit comments

Comments
 (0)