Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2为定价混乱提供功能_Php_Symfony_Twig - Fatal编程技术网

Php Symfony2为定价混乱提供功能

Php Symfony2为定价混乱提供功能,php,symfony,twig,Php,Symfony,Twig,我现在需要根据我想要购买的相同产品的数量动态更改购物车中的价格。我被困在这一部分,因为我觉得我把整个购物车的逻辑搞得有点愚蠢,现在真的很难得到价格。也许我错了 因此,购物车背后的整个理念是: 我有一个产品实体,其中包含所有产品信息和功能,如getPrice、getDiscount等。。当我向购物车添加产品时,我创建了一个新数组$cart,但我只在那里保存productsId,productsId有一个值products quantity,就是这个值。这就是我的意思: if($session-&

我现在需要根据我想要购买的相同产品的数量动态更改购物车中的价格。我被困在这一部分,因为我觉得我把整个购物车的逻辑搞得有点愚蠢,现在真的很难得到价格。也许我错了

因此,购物车背后的整个理念是:

我有一个产品实体,其中包含所有产品信息和功能,如getPrice、getDiscount等。。当我向购物车添加产品时,我创建了一个新数组$cart,但我只在那里保存productsId,productsId有一个值products quantity,就是这个值。这就是我的意思:

 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 %} 
但正如我所说,它只在产品数量为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();
    }

我发现错误:试图从命名空间Mp\ShopBundle\twig调用函数getItemPrice。我可能没有正确编写此函数?

如果您希望根据产品的数量确定产品的每个价格,那么$total变量在这里没有用处。您可能应该这样做:

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

其中,getItemPrice是一个返回$id商品价格的函数。

如果在函数中返回$this->getFinal,则始终返回相同的价格。也许类似的东西会起作用,尽管我不确定这是性能的最佳方式:公共函数getItemPrice$id,$items{foreach$items as$item{if$item->getId=$id return$item->getFinal;}}嘿,我仍然收到以下错误:未捕获的PHP异常Symfony\Component\Debug\Exception\UndefinedFunctionException:试图从命名空间Mp\ShopBundle\twig调用函数getItemPrice。在C:\wamp\www\Digidis\tree\src\Mp\ShopBundle\Twig\AppExtension.php第40行,函数是否属于对象?如果是这样,试着用$this->getItemPrice调用它。否则,可能会将getItemPrice声明放在其他函数之前。我现在看不到更多,因为我不知道函数是在什么上下文中编写的
foreach($cart as $id=>$quantity) {      
        $qtyTotal += $quantity * getItemPrice($id, $items);
    }