0

I have a problem, I made a switch case in my ajax.js file but even if the values fit a case, this is always the default case which is return,

someone has an idea why?

There my code :

app/code/Vendor/CustomerZipCode/view/frontend/web/js/ajax.js

define([
    'jquery',
    'underscore',
    'mage/template',
    'mage/url'
], function (
    $,
    _,
    template,
    url
) {
    'use strict';
    $(document).ready(function() {
        var ajaxurl = url.build('customerzipcode/zipcode');
        $('#zip_btn').val("not clicked");
        $('#zip_btn').on('click', function () {
            let Zip = $('#zip_txt').val();
            $('#zip_btn').val("clicked");
            $.ajax({
                showLoader: true,
                url: ajaxurl,
                data: {zipcode : Zip},
                type: "POST",
                dataType: 'json'
            }).done(function (data) {
                $('#test').removeClass('hideme');
                let displayzip ="You enter "+data['ZipCode']+" as zipcode <br/>";
                let fullchoice="You can choice between delivery or store pickup (for order over 200$)";
                let partialchoice="You can choice between delivery or store pickup, both for order over 200$";
                let nochoice="You are eligible only for store pickup for order over 200$";
                let html;
                console.log(data['ZipCode']);
                switch(data['ZipCode']){
                    case 11222 || '11222':
                        html=fullchoice
                        break;
                    case 11211 || '11211':
                        html=fullchoice
                        break;
                    case 11249 || '11249':
                        html=fullchoice
                        break;
                    default:
                        html=nochoice
                }
                $('#test p').html(displayzip+html);
            });
        });

    });
});
3

As Sumit has fixed but not explained, you are writing the switch statement incorrectly. Rather than use || you should write a new case. Try replacing your switch with this:

switch(data['ZipCode']){
    case '11222':
    case '11249':
    case '11211':
        html=fullchoice
        break;
    default: 
       html=nochoice
}

This is 'vanilla' JS and Magento have not changed how it works.

1

There is issue with the Switch statement in your code. Update your js with below code and give a try.

define([
    'jquery',
    'underscore',
    'mage/template',
    'mage/url'
], function (
    $,
    _,
    template,
    url
) {
    'use strict';
    $(document).ready(function() {
        var ajaxurl = url.build('customerzipcode/zipcode');
        $('#zip_btn').val("not clicked");
        $('#zip_btn').on('click', function () {
            let Zip = $('#zip_txt').val();
            $('#zip_btn').val("clicked");
            $.ajax({
                showLoader: true,
                url: ajaxurl,
                data: {zipcode : Zip},
                type: "POST",
                dataType: 'json'
            }).done(function (data) {
                $('#test').removeClass('hideme');
                let displayzip ="You enter "+data['ZipCode']+" as zipcode <br/>";
                let fullchoice="You can choice between delivery or store pickup (for order over 200$)";
                let partialchoice="You can choice between delivery or store pickup, both for order over 200$";
                let nochoice="You are eligible only for store pickup for order over 200$";
                let html;
                console.log(data['ZipCode']);
                switch(data['ZipCode']){
                    case '11222': 
                    case '11211': 
                    case '11249': 
                        html=fullchoice 
                        break; 
                    default: 
                        html=nochoice
                }
                $('#test p').html(displayzip+html);
            });
        });

    });
});

Hope it helps!!!

  • 2
    There is no need to repeat the contents of each case, you can write that as: case '11222':case '11211':case '11249':html=fullchoice; break;default:html=nochoice. I can't add new lines in a comment but hopefully you get the idea. – Ben Crook Aug 30 '19 at 10:49
  • 2
    Yes Ben, Great thanks. You mean I can add the code like case '11222': case '11211': case '11249': html=fullchoice break; default: html=nochoice for above example. – Sumit Aug 30 '19 at 10:52
  • it will work also in Magento ? as magento is kind of ... special ... ? – SylwekFr Aug 30 '19 at 10:55
  • It can work with Magento as well. Also, I've optimized the code as per Ben suggests. – Sumit Aug 30 '19 at 10:55
  • 1
    The switch statement is pure JS, Magento have not changed this at all @SylvainMichel – Ben Crook Aug 30 '19 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.