我在为一家家族经营的葡萄酒进口商建立一个小型电子商务网站时,正在学习使用 Symfony2 的方法。慢慢地,我开始了解 Symfony2 的概念,但是在继续构建购物车捆绑包时,我不太确定实现它的正确方法(至少根据 Sf2 标准)是什么。
我根据会话制作了简单的购物车捆绑包。
我的问题是,当我在购物车中添加产品时,它会一直工作,直到产品 ID 为 0 到 9 并且产品数量会自动增加,但是在产品 ID 为 10 之后,它的数量等于产品 ID,而它应该是一个。而且产品信息错误是当我们想要获取产品信息时。
我希望这不是一个太宽泛的问题。我很清楚这样一个事实,一个真正强大的购物车实施不是一件容易的事。
我的代码在这里:
<?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') );
}
}
{% 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 %}
请帮我解决一下这个。
谢谢!我感谢您的帮助。