简体   繁体   English

Symfony2正在构建购物车。 增加,减少数量

[英]Symfony2 building a shopping cart. Increasing, decreasing quantities

I have a few problems regarding the quantities when you want to add more than one of the same products to your shopping cart. 当您想在购物车中添加多个以上相同产品时,我在数量上存在一些问题。

The problem is that when I increase the quantity by one for some reason it increases it by 2. For example I add one products, so the quantity is 1. I add the same product again and the quantity becomes 3 instead of 2. 问题是当由于某种原因我将数量增加1时,数量增加2。例如,我添加一个产品,因此数量为1。再次添加相同的产品,数量变为3而不是2。

  if( isset($cart[$id]) ) {

            $qtyAvailable = $product->getStock();

                if( $qtyAvailable >= $cart[$id] = $cart[$id] + 1 ) {
                $cart[$id] = $cart[$id] + 1;; 
            } else {

                return $this->redirect($this->generateUrl('cart'));
            }

Looks liek a simple code but why does it do that? 看起来很简单,但是为什么要这样做呢?

Your if part is increasing your quantity and inside if you are again adding 1 in quantity 您的if部分增加了您的数量,而您内部又增加了1个数量

if( $qtyAvailable >= $cart[$id] = $cart[$id] + 1 ) {
                         here ^^^^^^^^  

Try this criteria 试试这个标准

if ( $qtyAvailable > $cart[ $id ]) {
    $cart[ $id ] = $cart[ $id ] + 1;;
} else {
    return $this->redirect( $this->generateUrl( 'cart' ) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM