我正在为Symfony2学习我的方法,同时为家庭经营的葡萄酒进口商建立一个小型电子商务网站.我慢慢地了解Symfony2概念,但在继续构建购物车捆绑包时,我不太确定什么是正确的(至少根据Sf2标准)实现这一点的方式.
我根据会话制作了简单的购物车套装.
我的问题是当我在购物车中添加产品然后它工作,直到产品ID为0到9并且产品数量自动增加但是在产品id为10之后它的数量等于产品id,而它应该是一个.并且错误的产品信息是当我们想要获取产品信息时.
我希望这不是一个太广泛的问题.我很清楚一个真正强大的购物车实施不是一件小事.
> CartController.PHP
<?PHP namespace Webmuch\CartBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Response; use Webmuch\ProductBundle\Entity\Product; /** * @Route("/cart") */ class CartController extends Controller { /** * @Route("/",name="cart") */ public function indexAction() { // get the cart from the session $session = $this->getRequest()->getSession(); // $cart = $session->set('cart',''); $cart = $session->get('cart',array()); // $cart = array_keys($cart); // print_r($cart); die; // fetch the information using query and ids in the cart if( $cart != '' ) { foreach( $cart as $id => $quantity ) { $productIds[] = $id; } if( isset( $productIds ) ) { $em = $this->getDoctrine()->getEntityManager(); $product = $em->getRepository('WebmuchProductBundle:Product')->findById( $productIds ); } else { return $this->render('WebmuchCartBundle:Cart:index.html.twig',array( 'empty' => true,)); } return $this->render('WebmuchCartBundle:Cart:index.html.twig',array( 'product' => $product,)); } else { return $this->render('WebmuchCartBundle:Cart:index.html.twig',array( 'empty' => true,)); } } /** * @Route("/add/{id}",name="cart_add") */ public function addAction($id) { // fetch the cart $em = $this->getDoctrine()->getEntityManager(); $product = $em->getRepository('WebmuchProductBundle:Product')->find($id); //print_r($product->getId()); die; $session = $this->getRequest()->getSession(); $cart = $session->get('cart',array()); // check if the $id already exists in it. if ( $product == NULL ) { $this->get('session')->setFlash('notice','This product is not available in Stores'); return $this->redirect($this->generateUrl('cart')); } else { if( isset($cart[$id]) ) { $qtyAvailable = $product->getQuantity(); if( $qtyAvailable >= $cart[$id]['quantity'] + 1 ) { $cart[$id]['quantity'] = $cart[$id]['quantity'] + 1; } else { $this->get('session')->setFlash('notice','Quantity exceeds the available stock'); return $this->redirect($this->generateUrl('cart')); } } else { // if it doesnt make it 1 $cart = $session->get('cart',array()); $cart[$id] = $id; $cart[$id]['quantity'] = 1; } $session->set('cart',$cart); return $this->redirect($this->generateUrl('cart')); } } /** * @Route("/remove/{id}",name="cart_remove") */ public function removeAction($id) { // check the cart $session = $this->getRequest()->getSession(); $cart = $session->get('cart',array()); // if it doesn't exist redirect to cart index page. end if(!$cart) { $this->redirect( $this->generateUrl('cart') ); } // check if the $id already exists in it. if( isset($cart[$id]) ) { // if it does ++ the quantity $cart[$id]['quantity'] = '0'; unset($cart[$id]); //echo $cart[$id]['quantity']; die(); } else { $this->get('session')->setFlash('notice','Go to hell'); return $this->redirect( $this->generateUrl('cart') ); } $session->set('cart',$cart); // redirect(index page) $this->get('session')->setFlash('notice','This product is Remove'); return $this->redirect( $this->generateUrl('cart') ); } }
> index.html.twig:
{% block body %} <h1>"FLAIRBAG" SHOPPING-CART</h1> <ul class="thumbnails"> {% if empty %} <h5>Your shopping cart is empty.</h5> {% endif %} {% set cart = app.session.get('cart') %} {% if product %} <ul class="thumbnails"> {% if app.session.hasFlash('notice') %} <divclass="flash-notice"> {{app.session.flash('notice') }} {{ app.session.removeFlash('notice') }} </div> {% endif %} {% for key,item in cart %} <p>ID:{{ key }}</p> <p>Quantity:{{ item }}</p> <button class="btn btn-primary"><a href="{{ path('cart_remove',{'id': key}) }}">Remove</a></button> {% for item in product %} <p>{{ item.title }}</p> <p>{{ item.preview }}</p> {% endfor %} {% endfor %} </ul> {% endif %} </ul> <a href="{{ path('products') }}">Products</a> {% endblock %}
请帮我解决一下这个.
谢谢!我感谢您的帮助.
解决方法
问题出在您的购物车阵列中.根据您的模板,您希望有一个具有此结构的数组:
cart { id => quantity }
即,数组的键是产品的ID,值是数量
但是然后在你的控制器中你做:
$cart[$id] = $id; $cart[$id]['quantity'] = 1;
这是一个非常不同的事情.你应该做:
$cart[$id] = 1;
在控制器中你使用$cart [$id] [‘quantity’]的所有其他地方使用$cart [$id]代替.例如:
$cart[$id] = $cart[$id] + 1;
编辑:
在您的控制器中执行:
$em = $this->getDoctrine()->getEntityManager(); foreach( $cart as $id => $quantity ) { $product[] = $em->getRepository('WebmuchProductBundle:Product')->findById($id) } if( !isset( $product ) ) { return $this->render('WebmuchCartBundle:Cart:index.html.twig',)); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。