php – 购物车捆绑包含Symfony2

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 购物车捆绑包含Symfony2脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为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',));
    }

脚本宝典总结

以上是脚本宝典为你收集整理的php – 购物车捆绑包含Symfony2全部内容,希望文章能够帮你解决php – 购物车捆绑包含Symfony2所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。