使用Ionic计算总价

时间:2018-06-11 06:22:31

标签: angularjs ionic-framework ionic-native

我开发了电子商务应用程序。我需要计算总金额。我的方法不起作用。当我添加一些数量相同的项目而没有特别说明。它显示正确。但是相同数量,带有特殊注释的相同项目则会添加到购物车中。它的计算正确。然后我之前添加了没有特殊注释项目的价格倍增。我该怎么解决呢。

    getTotalPrice({ quantity, price }: any) {

      const stringifiedPrice = price.toString().replace("RS. ","");
      const index = this._getFirstNum(stringifiedPrice);
      const numericPrice = stringifiedPrice.slice( index );
      const forLog = `${stringifiedPrice.slice(0, index)}${numericPrice * quantity}`;
      // const _getFirstNum = +stringifiedPrice.slice(0, quantity)
      console.log('Quantity => ', quantity);
      console.log('Price => ', numericPrice);
      console.log('FORLOG => ', forLog);
      console.log('stringifiedPrice => ', stringifiedPrice);
      return forLog;
    }

    private _getFirstNum(price: string): number {
      let index = 0;

      for (const char of price) {

        if (!Object.is(+char, NaN)) {
          return index;
        }
        index++;
      }
    }

  async addToCart(Order: IMenuItem) {

    console.log('Order => ', Order);

    try {
      const CartEntity: Cart = await this.CartEntity; 

      if (CartEntity.TOTAL_ORDERS_IN_CART + Order.quantity > MAX_CART_ITEMS) {
        throw new Error('Too many items in cart, first buy this!');
      }
      // const EntireDataSet = CartEntity.CART;

      let EntityInCart = CartEntity.CART.find(({ _id }: IMenuItem) => _id === Order._id);

      if (!EntityInCart) {

        CartEntity.CART.push(Order);

      } else {

        const parsedId = Order._id.toString().split('_')[0];

          if ( (!!!EntityInCart.userNotes && !!!Order.userNotes) || EntityInCart.userNotes === Order.userNotes ) {
            EntityInCart.quantity += Order.quantity;
          }else {
            Order._id += ('_' + Date.now());
            CartEntity.CART.push(Order);
          }
      }

      CartEntity.CART.forEach((iMenuItem: IMenuItem) => {
        iMenuItem.price = this.getTotalPrice(iMenuItem);
      });

      console.log('CartEntity.CART => ', CartEntity.CART);

      this._completer({
        actionFlag: CART_ACTION_FLAGS.ADD,
        message: `${Order.quantity} ${Order.name.toUpperCase()} was added to cart`,
        soundPath: 'addToCart.mp3',
        cartDbEntity: CartEntity,
        itemCount: Order.quantity
      });

    } catch (err) {
      // Handling all errors here :)
      this._toastMessageHandler(`${(err && err.message) || 'Error occured while trying to add to cart'}`);
    }

  }
当我将一些商品添加到购物车时,

一件商品是150件。我弄错了。

喜欢这个

enter image description here

0 个答案:

没有答案