Changing order quantity - Cakephp 3.8

Asked

Viewed 22 times

-1

Friends, I’m trying to update the quantity of products in an order cart.

My index.ctp is like this:

<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>

As you can see, I change the amount on a form. My controller method is 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 this, I’m taking the product id to see if I can change the quantity, but I don’t know if it would be the right information. I already did some tests with the index of the array but could not. Someone can give me a suggestion.

The array is like this: inserir a descrição da imagem aqui

2 answers

0

In the case of the index you mentioned I think is correct:

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

PS.: I made up my mind I didn’t test the code.

0

I got it this way:

public Function update($index = null) { $Session = $this->request->Session(); $cart = $Session->read('cart'); $quantity = $this->request->date('quantity'); if ($quantity > 0) { $carrinho[$index]->quantidade = $quantidade; $Session->write('cart', $cart); Return $this->redirect(['action' => 'index']); } }

Thanks for the return!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.