如何清除PrestaShop中购物车中的所有产品
我正在使用PrestaShop版本1.5.4.1
I am using PrestaShop version 1.5.4.1
当前,我的购物车中每种产品都有单独的删除按钮。
Currently, my cart has separate delete buttons for each product.
如何在一次操作中删除所有产品?我只需要一键清空购物车。
How can I remove all the products in one action? I just need to empty the cart in one click.
我在 ordercontroller
中使用了此代码,并调用了该函数来自主题/默认值/shoopin-cart.tpl
I have used this code in ordercontroller
and call the function from themes/defaulte/shoopin-cart.tpl
public function emptybag()
{
$products = $this->getProducts();
foreach ($products as $product) {
$this->deleteProduct($product->id);
}
}
很多事情:
- $ this-> getProducts()在订单控制程序中不起作用。取而代之的是在上下文中使用获取它
- getProducts()方法不会返回产品对象,而是一个产品数组的集合。您无法使用->使用[]来获取信息
有正确的功能:
public function emptybag()
{
$products = $this->context->cart->getProducts();
foreach ($products as $product) {
$this->context->cart->deleteProduct($product["id_product"]);
}
}
要使其更简单,请将函数添加到覆盖的前端控制器的文件,这样您就可以从前端的任何地方调用它。然后覆盖init函数,并将这些行添加到函数的结尾(而不是之前,因为我们需要初始化cart属性):
To make it easier, add your function to your overrided file of the front controler, like that you will be able to call it from everywhere in the front. Then override the init function and add these line to the end of the function (not before because we need the cart attribute to be initialised) :
if (isset($_GET['emptybag'])){
$this->emptybag();
}
然后,在您想要的模板上添加链接:
Then, add a link to your template where you want :
<a href="{$link->getPageLink('order', true, NULL, 'emptybag=1')}" class="button_large" title="{l s='Clear cart'}">{l s='Clear cart'}</a>
就是这样!