1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

jQuery script - Field based on quantity input

Discussion in 'jQuery' started by kinigakos, Oct 16, 2019.

  1. #1
    I am trying to display a value based on woocommerce quantity input and i'm using the following jQuery script.

    jQuery(function($){
        var form = 'form.variations_form',       selected = 'input[name="variation_id"]',
        attrVal = 'input#attr-hidden';
    
        var price = <?php echo $product->get_price(); ?>;
        $(form).on( 'blur', 'select', function() {
            if($(selected).val() != ''){
                if(($(attrVal).val() == 'Black') && ($('[name=quantity]').val() >= 1 && $('[name=quantity]').val() <= 50)){
                    print_price = '15'
                }
                if(($(attrVal).val() == 'Black') && ($('[name=quantity]').val() >= 51 && $('[name=quantity]').val() <= 100)){ 
                    print_price = '10'
                }
            }
        });
    
        var currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
    
        $('[name=quantity]').change(function(){
            if (!(this.value < 0)) {
                var print_product_total = parseFloat(print_price * this.value);
    
                $('#print_total_price .print_price').html( currency + print_product_total.toFixed(2));
            }
            $('#print_total_price').toggle(!(this.value <= 0));
        });
    });
    Code (JavaScript):
    What this script does is that if selected attribute of a product is "Black" and quantity input is greater that 1 and less that 50 is displays a field with value "15" and if it is greter that 51 and less than 100 it displays "10".

    The problem is that if i enter quantity 1 and then changed it to 51 it still displays "15" unless i select again the attribute "Black".
     
    kinigakos, Oct 16, 2019 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I'd have to see the markup being manipulated, but this looks like the typical train wreck laundry list of how NOT to use JavaScript so typical of jQuery.

    I'm really wondering though why you're waiting for blur instead of "change".
     
    deathshadow, Oct 28, 2019 IP