phpzend-framework2shopping-cartzend-session

Zend Framework SpeckCart not working correctly


I am using SpeckCart shopping cart module in my ZF2 project. I have setup the module and it adds item to the cart successfully, but it overwrites the same item on every call instead of adding new items. Please guide me where I am doing anything wrong, here is the code:

    public function addAction(){
    $prod_id = $this->params()->fromPost('prod_id');
    $product = $this->getProductTable()->getProduct($prod_id);

    $item = new CartItem();
    $item->setDescription($product->name);
    $item->setCartItemId($prod_id);
    $item->setPrice($product->price);

    $cs = $this->getServiceLocator()->get('SpeckCart\Service\CartService');

    $cart = $cs->getSessionCart(true); 

// I think the above line is creating a new cart every time and this is where the problem lies. I am unable to know how to make it use the existing cart created in previous call.

    $cs->addItemToCart($item, $cart);

I am a newbie regarding ZF, please let me know how should I use this cart module properly. The above code is from an action of my application that is called every time the 'add to cart' button is pressed.


Solution

  • Just came across this after trying the speckcart module out for the first time. I think your problem here is that you are trying to set the CartItemId. The database code (provided within the module) makes the cart item Id an auto-incremented primary key field, which therefore should not be set as the database should create it automatically.

    Also, you must supply a quantity as the quantity's database field is set to not accept null values.

    Other than that, everything should work.

    Hope this helps.