A
A
angelzzz2018-11-24 16:34:23
opencart
angelzzz, 2018-11-24 16:34:23

How to make sure that there is always one product in the cart by default in Opencart?

A site with one product, redirection to the cart is now implemented when you click on the buy button. Like this:

function addToCartQuick(product_id, quantity) {
  $('.js-btn-buy').html('Переадресация в корзину...');
    quantity = typeof (quantity) != 'undefined' ? quantity : 1;
    $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' + product_id + '&quantity=' + quantity,
            dataType: 'json',
            success: function(json) {
                    $('.success, .warning, .attention, .information, .error').remove();
                    window.location.href = 'index.php?route=checkout/simplecheckout';
            }
    });
}

and, accordingly, the call to this function on the "Buy" button.
onclick="addToCartQuick('50');"
The problem is that sometimes it freezes and the user constantly has the text Redirecting to the basket ... I decided to make it easier - this product is always in the basket by default and when you click on the "Buy" button, the basket opens. So it seems to me that unnecessary steps can be avoided. How do I get an item already in the cart? Hardcode it while leaving the ability to change the number of products and the price when using coupons or shipping

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zoozag, 2018-11-26
@angelzzz

Make a check for an empty basket somewhere, for example, in the controller of the basket itself (it already exists there, you need to rewrite the logic a little).
If the cart is empty, add your product to it:
$this->cart->add($product_id, $quantity);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question