0

我在尝试实现 martyshka/ShoppingCart 组件时非常头疼。我发现的只是水合器在添加项目时为空,但是当我强制它时它也不起作用(将它设置在组件上)。

这是我的控制器

<?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(),
        ]);
    }
    /*...*/
}

这是组件回购

4

1 回答 1

1

该组件提供了一个插件“ShoppingCart”来使用,你不需要创建ShoppingCart的对象。

如果您将此组件正确安装为模块,

只需在您的行动中使用它-

$this->ShoppingCart()
于 2017-08-08T21:32:19.603 回答