2017-05-23 3 views
0

J'utilise cakephp 3.4 pour mon projet. J'essaye d'ajouter le chariot de magasinage. J'ai un exemple de panier pour cakephp 2 à partir du lien suivant: https://www.startutorial.com/articles/view/build-a-shopping-cart-with-cakephp-and-jquery-part-2cakephp 3.4 panier

J'essayais de l'ajouter à mon projet. Je ne pouvais pas ajouter plus d'une donnée au panier.

Aussi, lorsque je clique sur le bouton de mise à jour de 'shopping vue panier', il montre l'erreur suivante:

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

Voici mon code:

Vue du produit

<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() ?> 

Panier Voir le produit

<?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: Je n'ai pas compris. Ce n'est pas clair. – Mustafa

+0

@ndm: merci. Je viens de l'avoir. résolu – Mustafa

Répondre

1

Dans CakePHP 3, vous devez appeler une méthode de la sorcière de classe de la table est au pluriel, donc dans cette partie de votre contrôleur:

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

vous devez utiliser:

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