Update shopping cart - cakephp 3.8

Friends, I am trying to update my shopping cart based on this article.

However, I cannot update the “quantity” value separately in the session. The article works with the products registered in the database. I use session. I believe that there is some detail in cart.ctp that is not correct in this transition! I appreciate if anyone can see what’s going on!

index.ctp

The code for my index.ctp is as follows:

<html>
  <body>
    <main class="mt-1 pt-1">
      <div class="container wow fadeIn">
        <div class="row">
          <div class="col-md-8 mb-4">
            <div class="card">

              <?php foreach($this->Session->read('carrinho') as $index=>$carrinho): ?>

              <div class="container">
                <div class="row">
                  <div class="col-md-10">
                    <table class="table table-striped">
                      <thead>
                        <tr>
                          <th scope="col">product</th>
                          <th scope="col">price</th>
                          <th scope="col">Qte</th>
                          <th scope="col">subtotal</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <th scope="row">
                            <?= $carrinho->has('produto') ? $this->Html->link($carrinho->produto->nome_produto, ['controller' => 'Produtos', 'action' => '/', $carrinho->produto->id]) : '' ?>
                          </th>
                          <td>
                            <strong>R$ <?php echo number_format($carrinho->produto->preco, 2, ',', '') ?></strong>
                          </td>
                          <td>
                            <?php echo $this->Form->create('Pedidos',array('id'=>'add-form', 'url'=>array('controller'=>'pedidos','action'=>'update', '$produto_id' => $carrinho->produto->id)));?>

                            <?php 
                            echo $this->Form->control('quantidade',
                            array('type'=>'number', 'label'=>false,'min'=> 1,'size' => 2, 
                            'maxlenght' => 2, 'class'=>'form-control','required' => 'true', 'value'=>$carrinho->quantidade));?> un.

                            <?php echo $this->Form->submit('update',array('class'=>'btn-md text-white waves-effect m-1', 'style' => 'background-color:#1ab394'));?>
                            <?php echo $this->Form->end();?>

                          </td>
                          <td>
                            <strong>
                              R$ <?php 
                              $sub = ($carrinho->produto->preco * $carrinho->quantidade);
                              echo number_format($sub, 2, ',', '');
                              ?>
                            </strong>
                            <?php $total = array(number_format($sub, 2, ',', ''));?>   
                          </td>
                        </tr>
                      </tbody>
                    </table>
                    <hr width="40%">
                  </div>
                </div>
              </div> 
              <div class="row">
               <div class="col-md-8">
               </div>
               <div class="col-md-2 mt-3">
                <?= $this->Html->link(__('Update'), ['action' => 'update']); ?>
              </div>
              <div class="col-md-2 mt-3">
                <?= $this->Html->link(__('Delete'), ['action' => 'delete', $index]); ?>
              </div>
            </div>
            <?php endforeach; ?>
            <br>
          </div>

        </div>
        <div class="col-md-4 mb-4">
          <form class="card p-2">
            <div class="input-group">
              <?= $this->Html->link(__('Checkout'), ['action' => 'checkout']); ?>
            </div>
            <?php echo $this->Form->end(); ?>
          </div>
        </div>
      </div>
    </main>
  </body>
  </html>

How can you change the value of the form number. My method on the controller looks like this:

public function update($produto_id = null) {
    $session = $this->request->session();
    $carrinho = $session->read('carrinho');
    $quantidade = $this->request->data('quantidade');
    if ($quantidade > 0) {
        $carrinho[$produto_id] = $quantidade;
        $session->write('carrinho', $carrinho);
        return $this->redirect(['action' => 'index']);
        }
    }

My question is as follows, I’m taking the product id to see if I can change the quantity, but I don’t know if it would be the correct information. To delete I use the array index, would it be the same way to change the quantity ?. Can someone give me a suggestion. I appreciate any comments.

If the session is using some zero-based index, then that’s what you need to pass to your function via the URL in order to update it.

I can delete the index normally that way.

    public function delete($index = null){
    $carrinho = $this->request->session();
    $carrinho->delete("carrinho.$index");

When I try to pass the index to the update () method, nothing happens.

index.ctp:
<?php echo $this->Form->create('Pedidos',array('id'=>'add-form','url'=>array('controller'=>'pedidos','action'=>'update', $index)));?>

controller:

public function update($index = null) {
    $session = $this->request->session();
    $carrinho = $session->read('carrinho');
    $quantidade = $this->request->data('quantidade');
    if ($quantidade > 0) {
        $carrinho[$index] = $quantidade;
        return $this->redirect(['action' => 'index']);
        }
    }

I tried to add session-> write ('cart', cart) but it also gives an error.

“Nothing happens” is not a good problem description.

First off, is the URL in your generated HTML correct? It should look like /pedidos/update/3, of course depending on the exact index. If it doesn’t, then the problem is in your template; if it does, it’s in the controller. This is how debugging works, you try to break the potential problem down into smaller, easily verifiable pieces.

P.S. Controller names in URL arrays should be capitalized, like array('controller' => 'Pedidos', .... Cake will take care of fixing the case when it generates the URL. But you need to be consistent in capitalization in order for more complex routing to work reliably for you. May not be a problem today, but it will be someday, and having your code and habits right from the start will save you much pain later on.

The URL is correct. I can display /pedidos/update/1, for example.
And the controller name is capitalized now. Thanks for clarifying this!

I also tried:
.
.
if ($quantidade > 0) {
$carrinho->write(“carrinho.$index”, $quantidade);
return $this->redirect([‘action’ => ‘index’]);
}
.
.
Error: Call to a member function write() on array

Two products with quantity ($quantidade) equal to 1 in the cart.
screen 01

To be blunt, you seem to be struggling with understanding what variables “are”, and just trying things in the hopes you’ll hit on something that works. I’m not sure what to suggest in terms of resolving this fundamental issue, apart from maybe doing more reading about programming in general and PHP in particular. If you’re trying to learn all of this from Cake tutorials and documentation, then you’re going to struggle with this for a long time; there’s a certain level of background knowledge that’s sort of assumed on the part of the reader before they start trying to develop an e-commerce site.

That said, $carrinho is very clearly an array. You can tell this from the screen shot you’ve provided, and from the error message. It’s an array with zero-based indices, containing entities. Arrays don’t have any write function, or any other function for that matter. They are just data.

You have an $index variable, and you want to update the quantidade in the entity referenced by that index. The entity in question is at $carrinho[$index], so updating that would be $carrinho[$index]->quantidate = $quantidade;. That’s now updated in local memory.

To write it back to the session, it would be $session->write('carrinho', $carrinho);. In another bit of your code, you mentioned $session->write('cart', cart), which (apart from the translation to English) will give an error because you’ve used just cart instead of $cart.

You may also be confusing yourself because in your delete function, you’ve used $carrinho to refer to the session object instead of the contents of the session. That’s absolutely fine to do, you can call your variables whatever you want, but in your update function $carrinho refers to the session contents, not the session object.

thanks for the feedback and for the tips. I will return my learning in the basics of it! Regarding the code, it worked!