Question

Following this link: Opencart: Adding Buy Now Button in Opencart Product Page

I added the following script on my page:

 $('#button-cart-buy').bind('click', function() {
                $.ajax({
                    url: 'index.php?route=checkout/cart/add',
                    type: 'post',
                    data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
                    dataType: 'json',
                    success: function(json) {
                        $('.success, .warning, .attention, information, .error').remove();

                        if (json['error']) {
                            if (json['error']['option']) {
                                for (i in json['error']['option']) {
                                    $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                                }
                            }

                            if (json['error']['profile']) {
                                $('select[name="profile_id"]').after('<span class="error">' + json['error']['profile'] + '</span>');
                            }
                        } 

                        if (json['success']) {window.location='<?php echo $this->url->link('checkout/checkout', '', 'SSL'); ?>'; 
                        }

                    }
                });
            });

However, I would like to empty the cart before adding the product in question.

Does anyone know how to do it or have any tips?

Was it helpful?

Solution

The simplest way to do this would be to edit the cart class /system/library/cart.php

Find this line (from a 1.5.5.1 install - any other version may vary slightly though unlikely to be majorly different)

public function add($product_id, $qty = 1, $option = array()) {

On a new line after it, add

$this->cart->clear();

And save. Note that this allows for a customer to add a quantity greater than 1 to a cart as long as it is the same product

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top