如何解决Cakephp-单击“添加到购物车”按钮后,不会重定向到购物车项目页面“购物车/索引”
您可以在视图中看到添加单个产品的代码。
当我按下添加到购物车按钮时,如下所示。
<?= $this->Form->submit('add to cart',['class'=>'btn_3']); ?>
它实际上将数据保存在phpmyadmin的carts表中,但没有重定向到我必须结帐的cart物品页面。
<div class="single_product_text text-center">
<h3><?= h($product->name) ?></h3>
<p><?= h($product->description) ?></p>
<?= $this->Form->create(null,['id'=>'cartForm','url'=>['controller' => 'Products','action' => 'addItem',$product->id]]); ?>
<div class="card_area">
<div class="product_count_area">
<p>Quantity</p>
<div class="product_count d-inline-block">
<span class="product_count_item inumber-decrement"> <i class="ti-minus"></i></span>
<?= $this->Form->hidden('user_id',['value'=>$this->request->getSession()->read('Auth.User.id')]); ?>
<?= $this->Form->hidden('product_id',['value'=>$product->id]); ?>
<?= $this->Form->hidden('price',['value'=>$product->sell_price]); ?>
<?= $this->Form->input('quantity',['class'=>'product_count_item input-number','value'=>1,'min'=>0,'max'=>6,'label' => false,]); ?>
<span class="product_count_item number-increment"> <i class="ti-plus"></i></span>
</div>
<p>$<?= h($product->sell_price) ?></p>
</div>
<div class="add_to_cart">
<?= $this->Form->submit('add to cart',['class'=>'btn_3']); ?>
</div>
</div>
<?= $this->Form->end(); ?>
</div>
</div>
您可以在Products控制器中看到additem函数,在该控制器中我试图将其重定向到Carts Index页面。目前它只是重定向到同一页面。 @GregSchmidt,但它将数据保存在数据库表购物车中。
public function addItem($id = null)
{
$cartModel = $this->loadModel('Carts');
$cart = $cartModel->find()
->where(['Carts.product_id' => $id,'Carts.user_id' => $this->request->getData('user_id')])
->contain(['Products']);
if($cart->isEmpty())
{
$cart = $cartModel->newEmptyEntity();
if ($this->request->is('post'))
{
$cart = $cartModel->patchEntity($cart,$this->request->getData());
if ($cartModel->save($cart))
{
return $this->redirect(['controller'=>'Products','action' => 'view',$id]);
}
$this->Flash->error(__('The cart could not be saved. Please,try again.'));
}//end of post if
}//end of isEmpty if
else
{
if ($this->request->is(['patch','post','put']))
{
foreach($cart as $cart1)
{
$cart1->quantity += $this->request->getData('quantity');
if ($cartModel->save($cart1))
{
// $this->Flash->set(__('The cart has been saved.'),['element'=>'success']);
return $this->redirect(['controller'=>'Carts','action' => 'index']);
}
$this->Flash->set(__('The cart could not be saved. Please,try again.'),['element'=>'error']);
}//end of foreach
}//end of if
}//end of else
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。