phpsessionzend-framework2shopping-cartzend-hydrator

Why is martyshka/ShoppingCart ZF2 component not working (hidrator issue maybe)?


I'm having a lot of headache trying to implement martyshka/ShoppingCart component. All I found was the hydrator was null while adding items, but it didn't work too when I forced it (setting it on the component).

Here's my controller

<?php
namespace Publico\Controller;

use Doctrine\ORM\EntityManager;
use ShoppingCart\Controller\Plugin\ShoppingCart;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class CarrinhoController extends AbstractActionController
{
    private $carrinho;
    private $entityManager;

    protected function setCarrinho(ShoppingCart $cart)
    {
        $this->carrinho = $cart;
        return $this;
    }

    protected function getCarrinho()
    {
        if (null === $this->carrinho) {
            $this->setCarrinho(new ShoppingCart());
        }
        return $this->carrinho;
    }

    /*...*/

    public function indexAction()
    {
        try {
            $carrinho = $this->getCarrinho();
        } catch (\Exception $e) {
            die($e->getMessage());
        }

        $carrinhoItems = [
            'carrinho' => $carrinho->cart(),
            'valorTotal' => $carrinho->total_sum(),
            'qtdTotal' => $carrinho->total_items(),
        ];

        die($carrinhoItems);

        return new ViewModel([
            'carrinho' => $this->carrinho->cart(),
            'valorTotal' => $this->carrinho->total_sum(),
            'qtdTotal' => $this->carrinho->total_items(),
        ]);
    }
    /*...*/
}

Here's the component repo


Solution

  • this component provides a plugin "ShoppingCart" to use, you do not need to create object of ShoppingCart.

    if you properly install this component as module,

    just use it in your action-

    $this->ShoppingCart()