2017-05-23 81 views
0

我爲我的項目使用了cakephp 3.4。我正在嘗試添加購物車。我從以下鏈接獲得了cakephp 2的購物車示例: https://www.startutorial.com/articles/view/build-a-shopping-cart-with-cakephp-and-jquery-part-2cakephp 3.4購物車

我試圖將它添加到我的項目中。我無法將多個數據添加到購物車。

而且,當我點擊從 '購物車查看' 更新按鈕,它顯示了以下錯誤:

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'); 
} 
} 
+1

'Cart!= Carts' – ndm

+0

@ndm:我不明白。這不清楚。 – Mustafa

+0

@ndm:謝謝。我剛剛得到它。解決了 – Mustafa

回答

1

在CakePHP 3,你必須調用從表類巫婆的方法是複數形式,所以在這部分內容控制器:

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

您必須使用:

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