问题
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.
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);
}
}
回答1:
Many things :
- $this->getProducts() won't work in the order controler. Use get it with the context instead
- getProducts() method doesn't return product object, but a collection of product array. You can't get informations with -> use [] instead
There is your correct function :
public function emptybag()
{
$products = $this->context->cart->getProducts();
foreach ($products as $product) {
$this->context->cart->deleteProduct($product["id_product"]);
}
}
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>
And this is it!
回答2:
To have a clean url in your navigation you can add this line after your condition "emptybag"
Tools::redirect($this->context->link->getPageLink('order', true, NULL));
to redirect page on order.
回答3:
$this->context->cart->delete();
Simple!
来源:https://stackoverflow.com/questions/17101662/how-to-clear-all-produts-from-cart-in-prestashop