Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Using Vue to validate coupon(s)

Hi all,

I have some code (as follows) which is used to validate and return the correct amount using Vue, but after a few updates to the system this has stopped working. Here's what I have using version v0.11.10

// Vue Instance
new Vue({
    el: '#coupons',

    data: {
        coupon: {
            code: '',
            description: '',
            discount: 0
        },
        valid: false,
        finalTotalAmount: 0,
        finalTotal: 0
    },

    ready: function() {
        this.getTotal();
    },

    methods: {
        getTotal: function() {
            this.$http.get('/cartTotal', function(total) {
                var delivery = 399;
                var currentUrl = window.location.pathname;

                if(total < 7500 && currentUrl != '/collection') {
                    this.finalTotalAmount = total + delivery;
                }
                else {
                    this.finalTotalAmount = total;
                }
            });
            console.log(this.finalTotalAmount);
        },
        validate: function() {
            this.$http.get('/validateCoupon/' + this.coupon.code)
                .then(function(coupon) {
                    this.coupon = coupon;
                    this.valid = true;
                    this.coupon.description = 'Great! ' + this.coupon.amount + '% has been applied.';

                    this.calculateDiscountedFinalAmount();
                })
                .error(function() {
                    this.coupon.code = '';
                    this.coupon.description = 'Sorry, that coupon does not exist.';
                });
        },
        calculateDiscountedFinalAmount: function () {
            discountPercentage = this.coupon.amount / 100;
            discountAmount = this.finalTotalAmount * discountPercentage;
            discountedTotal = this.finalTotalAmount - discountAmount;
            return this.finalTotalAmount = Math.round(discountedTotal);
        }
    }
});

// routes 
// Get cart totals
Route::any('cartTotal', 'FrontController@getCartTotal');
Route::any('finalTotal/{total}', function(){
   $total = Request::segment(2);
    \Session::put('finalTotal', $total);
});
Route::any('validateCoupon/{code}', function($code){
    return \App\Coupons::whereCode($code)->firstOrFail();
});
// Controller methods for the routes
public function getCartTotal()
    {
        $total = Cart::total() * 100;
        return response()->json($total);
    }

// html form wrapped in a div with an id of coupons
<input type="text"
                                   name="coupon"
                                   id="coupon"
                                   class="form-control"
                                   v-model="coupon.code"
                                   v-on="blur: validate"
                                   v-attr="disabled: valid"
                            >
                            <p class="label label-primary" v-show="coupon.description" v-text="coupon.description"></p>

I then have two hidden fields to get these values, so I can then pass the corrected price should a valid coupon be entered into my controller to pass to Stripe:

<input type="hidden" name="finalTotalAmount" value="@{{ finalTotalAmount / 100 }}">
<input type="hidden" name="totalNoDelivery" value="@{{ finalTotalAmount  }}">

When I've tried this it seems to be displaying all the HTML and showing finalTotalAmount as either an empty string or NaN

0 likes
10 replies
theUnforgiven's avatar

Also shows up on the screen as

Total   {{ finalTotalAmount / 100 | currency £ }}

Rather than been rendered correctly. So obviously not correct!

rk's avatar

In first code sample, inside methods: calculateDiscountedFinalAmount:

this looks like a typo return this.finalTotalAmount = Math.round(discountedTotal); it should be - instead of =.

theUnforgiven's avatar

@rk That makes no difference it's still showing on the page as {{ finalTotalAmount / 100 | currency £ }}so something isn't rendering properly either, I don't see anything in `console.lo and everything is imported that should should & scripts loaded also, so not sure!

rk's avatar

@theUnforgiven I can't see Total {{ finalTotalAmount / 100 | currency £ }}in the code sample.

Can you reproduce it in a jsFiddle? that will simplify a lot of things.

theUnforgiven's avatar

The Total {{ finalTotalAmount / 100 | currency £ }}is in another part of the html file.

michaeldyrynda's avatar

Are you using vue dev tools in chrome? Does that shed any light?

theUnforgiven's avatar

Cos its not v1 devtools doesnt work so kinda in the dark @deringer so thinking may just re-write in newest version of Vue.

theUnforgiven's avatar

I've managed to upgrade to Vue v1.0.26 but the :blur function doesn't seem to work.

Basically, I need the input to validate (do my method called validate) once the use moves away from the input.

Any suggestions @deringer @rk

jimmck's avatar
<!-- field template -->
<script type="text/x-template" id="dynoinput-template">
    <div id="compinput">
        <div v-for="(item, $index) in list" v-bind:value="item">
            <input @blur="onKeyUp($index)" name="item.name"
                v-model="list[$index].value">
        </div>
    </div>
</script>

Its a list of input fields for testing. I am porting Vue 2.0 right now. Don't remember if @blur worked in 1.26.

1 like

Please or to participate in this conversation.