function setShippingMethods()

in src/Feature/Cart/code/Scripts/checkout.js [233:357]


function setShippingMethods() {
    ClearGlobalMessages();
    var parties = [];
    var shipping = [];
    var orderShippingPreference = checkoutDataViewModel.selectedShippingOption();
    $("#deliveryMethodSet").val(false);

    $("#ToBillingButton").button('loading');
    $("#BackToBillingButton").button('loading');

    if (orderShippingPreference === 1) {
        var partyId = checkoutDataViewModel.shippingAddress().externalId();
        parties.push({
            "Name": checkoutDataViewModel.shippingAddress().name(),
            "Address1": checkoutDataViewModel.shippingAddress().address1(),
            "Country": checkoutDataViewModel.shippingAddress().country(),
            "City": checkoutDataViewModel.shippingAddress().city(),
            "State": checkoutDataViewModel.shippingAddress().state(),
            "ZipPostalCode": checkoutDataViewModel.shippingAddress().zipPostalCode(),
            "ExternalId": partyId,
            "PartyId": partyId
        });

        shipping.push({
            "ShippingMethodID": checkoutDataViewModel.shippingMethod().id,
            "ShippingMethodName": checkoutDataViewModel.shippingMethod().description,
            "ShippingPreferenceType": orderShippingPreference,
            "PartyID": partyId
        });
    }
    else if (orderShippingPreference === 2) {
        var storeId = checkoutDataViewModel.store().externalId();
        parties.push({
            "Name": checkoutDataViewModel.store().name(),
            "Address1": checkoutDataViewModel.store().address().address1(),
            "Country": checkoutDataViewModel.store().address().country(),
            "City": checkoutDataViewModel.store().address().city(),
            "State": checkoutDataViewModel.store().address().state(),
            "ZipPostalCode": checkoutDataViewModel.store().address().zipPostalCode(),
            "ExternalId": storeId,
            "PartyId": storeId
        });

        shipping.push({
            "ShippingMethodID": checkoutDataViewModel.shipToStoreDeliveryMethod().ExternalId,
            "ShippingMethodName": checkoutDataViewModel.shipToStoreDeliveryMethod().Description,
            "ShippingPreferenceType": orderShippingPreference,
            "PartyID": storeId
        });
    }
    else if (orderShippingPreference === 4) {
        $.each(checkoutDataViewModel.cart().cartLines(), function () {
            var lineDeliveryPreference = this.selectedShippingOption();
            var lineId = this.externalCartLineId;

            if (lineDeliveryPreference === 1) {
                var partyId = this.shippingAddress().externalId();
                parties.push({
                    "Name": this.shippingAddress().name(),
                    "Address1": this.shippingAddress().address1(),
                    "Country": this.shippingAddress().country(),
                    "City": this.shippingAddress().city(),
                    "State": this.shippingAddress().state(),
                    "ZipPostalCode": this.shippingAddress().zipPostalCode(),
                    "ExternalId": partyId,
                    "PartyId": partyId
                });

                shipping.push({
                    "ShippingMethodID": this.shippingMethod().id,
                    "ShippingMethodName": this.shippingMethod().description,
                    "ShippingPreferenceType": lineDeliveryPreference,
                    "PartyID": partyId,
                    "LineIDs": [lineId]
                });
            }

            if (lineDeliveryPreference === 2) {
                var storeId = this.store().externalId();
                parties.push({
                    "Name": this.store().name(),
                    "Address1": this.store().address().address1(),
                    "Country": this.store().address().country(),
                    "City": this.store().address().city(),
                    "State": this.store().address().state(),
                    "ZipPostalCode": this.store().address().zipPostalCode(),
                    "ExternalId": storeId,
                    "PartyId": storeId
                });

                shipping.push({
                    "ShippingMethodID": checkoutDataViewModel.shipToStoreDeliveryMethod().ExternalId,
                    "ShippingMethodName": checkoutDataViewModel.shipToStoreDeliveryMethod().Description,
                    "ShippingPreferenceType": lineDeliveryPreference,
                    "PartyID": storeId,
                    "LineIDs": [lineId]
                });
            }

            if (lineDeliveryPreference === 3) {
                shipping.push({
                    "ShippingMethodID": checkoutDataViewModel.emailDeliveryMethod().ExternalId,
                    "ShippingMethodName": checkoutDataViewModel.emailDeliveryMethod().Description,
                    "ShippingPreferenceType": lineDeliveryPreference,
                    "ElectronicDeliveryEmail": this.shippingEmail(),
                    "ElectronicDeliveryEmailContent": this.shippingEmailContent(),
                    "LineIDs": [lineId]
                });
            }
        });
    }
    else if (orderShippingPreference === 3) {
        shipping.push({
            "ShippingMethodID": checkoutDataViewModel.emailDeliveryMethod().ExternalId,
            "ShippingMethodName": checkoutDataViewModel.emailDeliveryMethod().Description,
            "ShippingPreferenceType": orderShippingPreference,
            "ElectronicDeliveryEmail": checkoutDataViewModel.shippingEmail(),
            "ElectronicDeliveryEmailContent": checkoutDataViewModel.shippingEmailContent()
        });
    }

    var data = '{"OrderShippingPreferenceType": "' + orderShippingPreference + '", "ShippingMethods":' + JSON.stringify(shipping) + ', "ShippingAddresses":' + JSON.stringify(parties) + '}';
    AJAXPost(StorefrontUri("api/sitecore/checkout/SetShippingMethods"), data, setShippingMethodsResponse, $(this));
    return false;
}