-2

How can I remove an item from the customer's basket in django oscar?

The following line just acts as an <a href="#"></a> link:

<a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="remove" class="inline">{% trans "Remove" %}</a>
ruddra
  • 50,746
  • 7
  • 78
  • 101
ironicaldiction
  • 1,200
  • 4
  • 12
  • 27
  • 1
    Just like you'd do with any Django model instance, using the line's `delete` method: `basket.all_lines().filter(..).delete()` – Phillip Nov 03 '14 at 08:47
  • Your question is a bit vague. Oscar uses some javascript to remove an item from the basket:$('a[data-behaviours~="delete"]').click(function(){o.notifications.checkAndSubmit($(this),'delete');});},checkAndSubmit:function($ele,btn_val){$ele.closest('tr').find('input').attr('checked','checked');$ele.closest('form').find('button[value="'+btn_val+'"]').click();return false;}}; – het.oosten Nov 03 '14 at 10:32

1 Answers1

2

In Python, if you know the line of which item to delete:

request.basket.items[line].delete()
request.basket.save()

In JavaScript, look at https://github.com/django-oscar/django-oscar/blob/master/oscar/static/oscar/js/oscar/ui.js#L177-180 for the lines of code that trigger the item removal:

$('#content_inner').on('click', '#basket_formset a[data-behaviours~="remove"]', function(event) {
    o.basket.checkAndSubmit($(this), 'form', 'DELETE');
    event.preventDefault();
});

Some more possibly helpful reading: email thread about python deletion and email thread about js deletion.

Rodney Folz
  • 6,709
  • 2
  • 29
  • 38