0

I realize this may have been answered in one way or another and I will continue trying to find a solution in the mean time. I was just hoping to get some ideas on the BEST way to handle this one.

I have a shopping cart. My site links many stores together. My site is based on weight not quantity. My cart has a max capacity of 28 grams.

If a user has an item in there cart, and they decide to increase the weight. The old item is replaced by the new item.

DURING this process the OLD item weight is added into the new items weight then added to the carts current weight. I need to EXCLUDE the old Items weight from this process, only comparing the new item and the other items in the cart.

I found a function that will call the duplicate item and get the weight results.

I subtract the amount from the total and everything works great!

WRONG! the function I found that gets the duplicate items weight, will return false if there is no duplicate item. Now that function is located inside another function that is based on returning true or false. True means the item is "added", false means it is "not added". So When my cart is empty or there is no duplicate, the whole function ends up false prematurely.

I am only posting this because I noticed there is A LOT of ways to accomplish this and a lot of these solutions are 5 years old.

Now here is where the magic happens

<script>

simpleCart.bind('beforeAdd', function (item) {


    var existItem = simpleCart.has(item).get('weight');

    var multi_item = simpleCart.quantity();
    var cartweight = simpleCart.weight();
    var shopid = simpleCart.sid();
    var newweight = $( "div.item_weight" ).text();
    var itemid = $( "div.item_id" ).text();
    var totalweight = parseInt(cartweight) + parseInt(newweight) - parseInt(existItem);


if(shopid != null && shopid != <?=$shopid?> ){
    $( "#pop_fail_store" ).popup( "open" )
    return false;

        }else if (parseInt(totalweight)>=29){
            $( "#pop_fail_weight" ).popup( "open" )
            return false;

                }else if (simpleCart.has(item)){

                        $( "#pop_update" ).popup( "open" )
                            return true;


                                }else { 
                                    return true;
                                    }
        }); 

//SUCCESS       
simpleCart.bind( "afterAdd" , function( item ){
         $( "#pop_success" ).popup( "open" )
}); 

</script>

THIS is returning false when there is no items, and killing the function early.

var existItem = simpleCart.has(item).get('weight');

These are the functions that represent HAS EQUALS and GET

            has: function (item) {
                var match = false;

                simpleCart.each(function (testItem) {
                    if (testItem.equals(item)) {
                        match = testItem;
                    }
                });
                return match;
            }





me.equals = function (item) {
                    for( var label in _data ){
                        if (Object.prototype.hasOwnProperty.call(_data, label)) {
                            if (label !== 'quantity' && label !== 'id' && label !== 'price' && label !== 'weight') {
                                if (item.get(label) !== _data[label]) {
                                    return false;
                                }
                            }
                        }
                    }
                    return true;
                };








            me.get = function (name, skipPrototypes) {

                var usePrototypes = !skipPrototypes;

                if (isUndefined(name)) {
                    return name;
                }

                // return the value in order of the data object and then the prototype
                return isFunction(_data[name])  ? _data[name].call(me) :
                        !isUndefined(_data[name]) ? _data[name] :

                        isFunction(me[name]) && usePrototypes       ? me[name].call(me) :
                        !isUndefined(me[name]) && usePrototypes ? me[name] :
                        _data[name];
            };

I cannot change the get, has, or equals functions without messing up other functions. I just need the value that it returns.

My first plan is to move it out of the "before add" function and save it as a global variable inside of a ready function, then call it once I start the "before add" function? Is this the best way?

or

ajax...

or

make a new function?

or can I add another if statement?

The power of choice is not always the best. I'm sure there is a easy answer starring me in the face.

Maybe someone can tell me the most efficient 2017 way to do this.

a page from my site Click Here

Thanks in advance!

  • Isn't it just a matter of splitting it up? i.e. `var hasItem = simpleCart.has(item); var existItem = hasItem === false ? 0 : hasItem.get('weight');` – Alexander Jun 23 '17 at 07:04
  • I will give it a shot. –  Jun 23 '17 at 07:07
  • You should have answered... that works like a charm. Thanks, I knew it was simple, just couldn't see it. That was the last piece to my shopping cart puzzle. –  Jun 23 '17 at 07:45

1 Answers1

0

By looking at the code of the SimpleCart.has() and SimpleCart.get() functions, you just need to check the output of has() before using it.

var hasItem = simpleCart.has(item);
var existItem = hasItem === false ? 0 : hasItem.get('weight');
Alexander
  • 23,432
  • 11
  • 63
  • 73