Symfony2 制作定价混乱的功能

Symfony2 Making functions for pricing confusion

我现在需要根据我想购买的相同产品的数量动态更改购物车中的价格。我停留在这部分是因为我认为我让整个购物车逻辑有点愚蠢,现在很难获得价格。或者也许我错了?

所以购物车背后的整个想法是:

我有一个产品实体,其中包含所有产品信息和功能,例如 getPrice、getDiscount 等。当我将产品添加到购物车时,我创建了一个新数组 $cart,但我只将 productsId 保存在那里,并且 productsId 有一个值(产品数量),仅此而已。这就是我的意思:

 if($session->has('cart') && count($session->get('cart')) > 0 ) {

             $em = $this->getDoctrine()->getEntityManager();
                foreach( $cart as $id => $quantity ) {
                          $productIds[] = $id;      
                } 
            if( isset( $productIds ) )
                {
                    $em = $this->getDoctrine()->getEntityManager();
                    $product = $em->getRepository('MpShopBundle:Product')->findById( $productIds );
                } else {
                    return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
                        'empty' => true,
                    ));
                }

               return $this->render('MpShopBundle:Frontend:product_summary.html.twig',     array(
            'product' => $product,
                    ));
                } else {
                    return $this->render('MpShopBundle:Frontend:product_summary.html.twig',     array(
                        'empty' => true,
                    ));
                }
            }

现在,当我想根据 productsId 显示购物车中的商品时,我只需这样做:

{% if product is defined %}

         {% if cart is defined %}

              {% for info in product %}

                <tr>

                  <td> <img width="60" src="{{ asset('bundles/mpFrontend/assets/products/6.jpg') }}" alt=""/></td>

                  <td>{{ info.model }}</td>
                  <td>

                  {% for key, item in cart %}

                {% if key == info.id %}

                    <div class="input-append">

                    <input class="span1" style="max-width:34px" placeholder="{{ key }}" value="{{ item }}" id="appendedInputButtons" size="16" type="text" data-id="{{ key }}"/>
                    <a href="javascript:void(0)" class="remove btn"><i class="icon-minus"></i></a>
                    <a href="javascript:void(0)" class="add btn"><i class="icon-plus"></i></a>

                    {% endif %}

                    {% endfor %}

                    {% for key in cart|keys %}

                    {% if key == info.id %}

                    <button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white"></i></button>

                        {% endif %}
                    {% endfor %}
                    </div>

                  </td>

                  <td>{{ info.price }}</td>
                  <td>{{ info.discount }}</td>
                  <td>{{ info.value }}</td>


                  <td>{{ info.getFinal|number_format(2, '.', ',') }}</td>


                </tr>


 {% endfor %}


                <tr>
                  <td colspan="6" align="right">Total Price:    </td>
                  <td> ${{getTotalPrice(product)|number_format(2, '.', ',')}}</td>
                </tr>

                 <tr>
                  <td colspan="6" align="right">Total Discount: </td>
                  <td> ${{getTotalDiscount(product)|number_format(2, '.', ',')}}</td>
                </tr>
                 <tr>
                  <td colspan="6" align="right">Total Tax:  </td>
                  <td> ${{getTotalTax(product)|number_format(2, '.', ',')}}</td>
                </tr>
                 <tr>
                  <td colspan="6" align="right"><strong>TOTAL</strong>  </td>
                  <td class="label label-important"> <strong> ${{getTotalCart(product)|number_format(2, '.', ',')}} </strong></td>
                </tr>

                </tbody>
            </table>

{% endif %}
{% endif %} 

如您所见,有很多问题也让我很恼火。但是问题是当我想显示价格时:

我可以很容易地显示单个价格,使用 info.price 等等..但是这只显示有 1 个相同类型产品时的价格。我也可以通过这样的树枝扩展获得所有产品的全价:

public function getTotalCart($items)
    {
    $total = 0;
    foreach($items as $item){

        $total += $item->getFinal();
    }
    return $total;
    }

但是就像我说的,它只在产品数量为 1 时有效。 所以我的问题是:如何创建一个函数,从产品实体获取价格,从 $cart 数组获取数量,然后将它们相加?这可能吗?或者也许有一种方法可以将所有产品信息保存在 $cart 中然后访问它?我不知道..

我知道我可以做到 {{ info.price * item }} 但我希望将价格保存在某处而不只是显示它。

这是我到现在为止想到的:

public function getTotalCart($items, $cart)
    {
    $qtyTotal = 0;
    foreach($cart as $id=>$quantity) {      
        $qtyTotal += $quantity * getItemPrice($id, $items);
    }
    return $qtyTotal;
    }

    public function getItemPrice($id, $items){

        return $this->getFinal();
    }

我得到错误:Attempted to call function "getItemPrice" from namespace "Mp\ShopBundle\twig".我可能没有写这个函数吧?

如果您想根据产品的数量获得每个产品的价格,则 $total 变量在这里没有用。你可能应该做这样的事情:

foreach($cart as $id=>$quantity) {      
        $qtyTotal += $quantity * getItemPrice($id, $items);
    }

其中 getItemPrice 是返回带有 $id 的商品价格的函数。