Answer the question
In order to leave comments, you need to log in
How to create a new cart?
Good day.
Background:
There was a task: to make 5 stores with one back office, but there with different prices, etc. After many discussions and trials, we decided to use one database and connect it django.contrib.sites
to separate content across sites. For stores, we chose Django-Oscar.
Oscar has the following structure: there is an application Basket
. It has two main classes: Basket
and Line
. Both classes changed to be used with django.contrib.sites
(added fields). Everything worked, that is, different sites have different baskets. But when I put the same products in the basket on different sites, and buy on one of them, then on the second the basket becomes empty.
I got into the Dzhang admin panel, and found out that when you add a product to the cart, aLine
and Basket
and Line
is associated with Basket
. But, when the same product is added to the cart on the second site, it will only be created Line
and it will be linked from Basket
the other site. In other words - one basket for two sites.
How can I change it? How to make sure that the cart is created only on the site where the product was added to it?
Thanks in advance for your help.
Here are examples of models:
class Basket(AbstractBasket):
site = models.ForeignKey(Site, blank=True, null=True, on_delete=models.CASCADE)
objects = CurrentSiteManager()
def __init__(self, *args, **kwargs):
super(AbstractBasket, self).__init__(*args, **kwargs)
self._lines = None
self.offer_applications = results.OfferApplications()
self.site = Site.objects.get_current()
class Line(AbstractLine):
site = models.ForeignKey(Site, blank=True, null=True, on_delete=models.CASCADE)
objects = CurrentSiteManager()
def __init__(self, *args, **kwargs):
super(AbstractLine, self).__init__(*args, **kwargs)
# Instance variables used to persist discount information
self._discount_excl_tax = D('0.00')
self._discount_incl_tax = D('0.00')
self._affected_quantity = 0
self.site = Site.objects.get_current()
class OpenBasketManager(models.Manager):
"""For searching/creating OPEN baskets only."""
status_filter = "Open"
def get_queryset(self):
return super(OpenBasketManager, self).get_queryset().filter(
status=self.status_filter, site__id = settings.SITE_ID)
def get_or_create(self, **kwargs):
return self.get_queryset().get_or_create(
status=self.status_filter, **kwargs)
site__id = settings.SITE_ID
in . And everything worked. But, when I added a product to an empty cart and then on the second site I add the same product to the cart, an error occurs:IntegrityError at /en-gb/basket/add/15/
duplicate key value violates unique constraint "basket_line_basket_id_7d0d707a7fd92c45_uniq"
DETAIL: Key (basket_id, line_reference)=(13, 15_11) already exists.
Open - currently active
, but Merged - superceded by another basket
. I understand that when I added the product a second time, Django-Oscar considered that I had a basket and merged the two of them. Answer the question
In order to leave comments, you need to log in
Here you can see that when adding a product, the cart is taken from request.
Here you can see that when placing an order, the basket is again taken from request.
Here the basket is added to request.
Conclusion: it is in the middleware that you need to implement the logic for choosing the current basket.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question