cakephp 3.4购物车

时间:2017-05-23 19:30:27

标签: cakephp-3.0 shopping-cart

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

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

此外,当我点击购物车视图'中的更新按钮时,会显示以下错误:

  

错误:在布尔值上调用成员函数saveProduct()   文件my_project_url \ src \ Controller \ CartsController.php   行: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');
}
}

1 个答案:

答案 0 :(得分:1)

在CakePHP 3中,您必须从Table类中调用一个以复数形式存在的方法,因此在控制器的这一部分中:

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

你必须使用:

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