how to add multiple products to the basket?

208 views
Skip to first unread message

pythan

unread,
Apr 1, 2019, 9:04:01 AM4/1/19
to django-oscar
How can I change the view so I can add a product and choose the quantity it?
Like updating the quantity in the basket.

Is there a simple solution?

Thank you very much for help!

SammyJ

unread,
Apr 2, 2019, 1:17:55 AM4/2/19
to django-oscar
Hi I guess you want the user to select how many products they want instead of just the 'add to cart' button.

Then for this in the product details page under templates/catalogue/detail.html I wrote a javascript code, this isn't the very clean way I guess but I didn't know how else to do it
So here is the code:

{% block extrascripts %}
{{ block.super }}
<script>
var qty=document.getElementById("id_quantity");
qty.setAttribute("type","number");
qty.setAttribute("min","1");
qty.setAttribute("value","1");
</script>
{% endblock %}

pythan

unread,
Apr 6, 2019, 2:57:17 PM4/6/19
to django-oscar
Thank you SammyJ for your reply. I added in my template (add_to_basket_from_compact.html) a select option:

<select name="basket_quantity">
            <option value='1'>1</option>
            <option value='2'>2</option>
            <option value='3'>3</option>
            <option value='4'>4</option>
            <option value='5'>5</option>
</select>

Then I updated the basket/views.py:

def post(self, request, *args, **kwargs):
        self.quant = int(self.request.POST.get('basket_quantity'))
        self.product = shortcuts.get_object_or_404(
            self.product_model, pk=kwargs['pk'])
        return super().post(request, *args, **kwargs)

def form_valid(self, form):       
        self.request.basket.add_product(
            form.product, self.quant,
            form.cleaned_options())
...


I hope I can help someone with that!

Stéphane Rodriguez

unread,
Nov 2, 2020, 4:24:07 PM11/2/20
to django-oscar
FYI,

I was looking to the same functionality and I found the following.

You just have to replace the single in multi:   {% basket_form request product 'multi' as basket_form %}

Create a templates folder like: templates/oscar/catalogue/partials/add_to_basket_form_compact.html

{% load basket_tags %}
{% load i18n %}
{% load purchase_info_tags %}

{% purchase_info_for_product request product as session %}

{% if session.availability.is_available_to_buy %}
    {% basket_form request product 'multi' as basket_form %}
    <form action="{% url 'basket:add' pk=product.pk %}" method="post">
        {% csrf_token %}
        {{ basket_form.as_p }}
        <button type="submit" class="btn btn-primary btn-block" data-loading-text="{% trans 'Adding...' %}">{% trans "Add to basket" %}</button>
    </form>
{% else %}
    <span class="btn btn-secondary btn-block disabled">{% trans "Add to basket" %}</span>
{% endif %}
Reply all
Reply to author
Forward
0 new messages