繁体   English   中英

更新购物车 - 更改产品数量时出错.. Cakephp 3.8

[英]Update shopping cart - error when changing quantity of products.. Cakephp 3.8

朋友们,我正在尝试根据这篇文章更新我的购物车。

但是,我无法单独更新 session 中的“数量”值。 该文章适用于在数据库中注册的产品。 我使用 session。 我相信 cart.ctp 中有一些细节在这个过渡中是不正确的! 如果有人能看到发生了什么,我将不胜感激!

购物车.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>

购物车.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']);
        }
    }

您的编辑解决了一个主要问题,即您在td元素中启动了多个 forms ,但直到很久以后才结束它们。 这将产生无效的 HTML。 请注意,在表格结束后您仍然有原来的$this->Form->end()调用,应该将其删除。

您在这里遇到的主要问题是您的 forms 关于它们应该更新的行的信息为零。 您需要包含对哪个购物车项目的一些引用,大概在$cart->product->id ,就像您在删除 function 中包含$index一样,或者作为表单中的隐藏输入。

这里的第二个问题是您似乎认为发布的数据将具有某种结构,而事实并非如此。 您已将控件命名为“数量”。 这就是数据中的全部内容。 您将拥有$this->request->data['quantity'] ,它只是一个 integer 值,而不是您可以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;
    }
    // ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM