cakephp 3.4 购物车

cakephp 3.4 shopping cart

我正在为我的项目使用 cakephp 3.4。我正在尝试添加购物车。我从以下 link 中得到了 cakephp 2 的购物车示例: https://www.startutorial.com/articles/view/build-a-shopping-cart-with-cakephp-and-jquery-part-2

我试图将它添加到我的项目中。我无法将多个数据添加到购物车。

此外,当我点击 'shopping cart view' 的更新按钮时,它显示以下错误:

Error: Call to a member function saveProduct() on boolean File my_project_url\src\Controller\CartsController.php Line: 59

这是我的代码:

产品浏览

<div class="single-but item_add">
<?php echo $this->Form->create('Cart', ['id'=>'add-form', 'url' => ['controller'=>'carts', 'action' => 'add']]); ?>
<?php echo $this->Form->hidden('product_id',array('value'=>$fruit->id))?>
<input type="submit" value="add to cart"/>
<?= $this->Form->end() ?>

购物车视图

<?php echo  $this->Form->create('Cart',array('url'=>array('controller'=>'carts', 'action'=>'update')));?>
<div class="row">
    <div class="col-lg-12">
        <table class="table">
        <thead>
        <tr>
            <th>Product Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Total</th>
        </tr>
        </thead>
        <tbody>
        <?php $total=0;?>
        <?php foreach ($products as $product):?>
        <tr>
            <td><?php echo $product['name'];?></td>
            <td>tk. <?php echo $product['price_exact'];?>
            </td>
            <td><div class="col-xs-3">
                <?php echo $this->Form->hidden('product_id.',array('value'=>$product['Product']['id']));?>
                <?php echo $this->Form->input('count.',array('type'=>'number', 'label'=>false,
                'class'=>'form-control input-sm', 'value'=>$product['count']));?>
            </div></td>
            <td>tk. <?php echo $product['count']*$product['price_exact']; ?>
            </td>
        </tr>
            <?php $total = $total + ($product['count']*$product['price_exact']);?>
            <?php endforeach;?>

        <tr class="success">
            <td colspan=3></td>
            <td>tk. <?php echo $total;?>
            </td>
        </tr>
        </tbody>
    </table>

    <p class="text-right">
        <?php echo $this->Form->submit('Update',array('class'=>'btn btn-warning','div'=>false));?>
        <a class="btn btn-success"
           onclick="alert('Implement a payment module for buyer to make a payment.');">CheckOut</a>
    </p>

          </div>
      </div>
<?php echo $this->Form->end();?>

CartsController.php

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Core\Configure;

class CartsController extends AppController {

public function beforeFilter(Event $event)
{
    // allow all action
    $this->Auth->allow();
}

public function add() {
    $this->autoRender = false;
    if ($this->request->is('post')) {
        $this->Carts->addProduct($this->request->data['product_id']);
    }
    echo $this->Carts->getCount();
    $this->redirect(['controller'=>'fruits', 'action' => 'index']);
}

public function view() {
    $this->loadModel('Fruits');

    $carts = $this->Carts->readProduct();
    $products = array();
    if (null!=$carts) {
        foreach ($carts as $productId => $count) {
            $product = $this->Fruits->get($productId);
            $product['count'] = $count;
            $products[]=$product;
        }
    }
    $this->set(compact('products'));
}

public function update() {
    if ($this->request->is('post')) {
        if (!empty($this->request->data)) {
            $cart = array();
            foreach ($this->request->data['count'] as $index=>$count) {
                if ($count>0) {
                    $productId = $this->request->data['id'][$index];
                    $cart[$productId] = $count;
                }
            }
            $this->Cart->saveProduct($cart);
        }
    }
    $this->redirect(array('action'=>'view'));
}
}

CartsTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
use Cake\Network\Session;
use Cake\Core\Configure;

class CartsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('carts');      
}

/*
 * add a product to cart
 */
public function addProduct($productId) {
    $allProducts = $this->readProduct();
    if (null!=$allProducts) {
        if (array_key_exists($productId, $allProducts)) {
            $allProducts[$productId]++;
        } else {
            $allProducts[$productId] = 1;
        }
    } else {
        $allProducts[$productId] = 1;
        $this->saveProduct($allProducts);
    }
}

/*
 * get total count of products
 */
public function getCount() {
    $allProducts = $this->readProduct();

    if (count($allProducts)<1) {
        return 0;
    }

    $count = 0;
    foreach ($allProducts as $product) {
        $count=$count+$product;
    }

    return $count;
}

/*
 * save data to session
 */
public function saveProduct($data) {
    $session = new Session();
    return $session->write('cart',$data);
}

/*
 * read cart data from session
 */
public function readProduct() {
    $session = new Session();
    return $session->read('cart');
}
}

在 CakePHP 3 中,您必须从 Table class 中调用一个方法,因此在您的控制器的这一部分:

$this->Cart->saveProduct($cart);

你必须使用:

$this->Carts->saveProduct($cart);