更新购物车 - 更改商品数量时出错。Cakephp 3.8

问题描述 投票:0回答:1

朋友们,我正在尝试更新我的购物车,基于以下几点: 1. 关于本文.

但是,我不能在会话中单独更新 "数量 "值。该条款适用于数据库中注册的产品。我使用的是session。我相信cart.ctp中有一些细节在这个过渡中是不正确的! 如果有谁能看出是怎么回事,我将感激不尽!

cart.ctp

我的cart.ctp的代码如下。

  <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',$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>

cart.php

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

php session cakephp cart
1个回答
0
投票

你的编辑解决了一个主要问题,那就是你在启动多个表单的内 td 元素,但直到很晚才结束。这将产生无效的HTML。请注意,您仍然有您原来的 $this->Form->end() 表结束后的调用,应该被删除。

你在这里的主要问题是你的表单没有任何信息说明他们应该更新哪一行。你需要包含一些关于哪个购物车项目的参考信息,可能包括 $cart->product->id 中,就像您在URL中加入了 $index 在你的删除函数中,或者作为表单中的一个隐藏输入。

第二个问题是,你似乎认为发布的数据会有一些结构,但它不是。你把你的控件简单地命名为 "数量"。这就是数据中的所有内容。您将有 $this->request->data['quantity'],而且它只是一个单一的整数值,而不是一个数组你可以 foreach 过。因此出现了这个特殊的错误。

第三,你正在尝试访问 $this->request->data['Carrinho']['produto_id']而这个信息根本不存在。似乎你想从这里获取产品ID,但(根据上面的第一个问题)你从来没有将其添加到表单中。

所以,这样的东西应该可以解决所有问题。

<?php echo $this->Form->create('Requests',array('id'=>'add-form',
    'url'=>array('controller'=>'requests','action'=>'update',$cart->product->id)));?>

然后..:

public function update($productId) {
    // ...
    $carrinho = $session->read('carrinho');
    $quantidade = $this->request->data('quantity');
    if ($quantidade > 0) {
        $carrinho[$productId] = $quantidade;
    }
    // ...
© www.soinside.com 2019 - 2024. All rights reserved.