问题描述
当我按下添加到购物车按钮时,如下所示。
<?= $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 (将#修改为@)