Взяла на GitHub приложение с купонами, которое можно использовать у себя в проекте, по ссылке https://github.com/byteweaver/django-coupons. Но в файле ReadMe.txt, кажется, указана не вся информация по подключению к проекту данного приложения, так как после того, как я сделала все описанное в файле, купоны не заработали. Ведь не описано само подключение к корзине и ее продуктам. Есть ли у кого-то информация по подключению данного приложения? Может кто-то уже с подобным сталкивался. Заранее благодарна за помощь.
Прилагаю ниже свой код для корзины:
models.py
class CartItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name = 'Товар')
quantity = models.PositiveIntegerField(default=1, verbose_name = 'Кількість')
item_total = models.DecimalField(max_digits=9, decimal_places=2, default=0.00, verbose_name = 'Сума корзини')
def __str__(self):
return "Cart item for product {0}".format(self.product.title)
class Meta():
verbose_name_plural = 'Вміст корзини'
verbose_name = 'Вміст корзини'
class Cart(models.Model):
items = models.ManyToManyField(CartItem, blank=True, verbose_name = 'Складова')
cart_total = models.DecimalField(max_digits=9, decimal_places=2, default=0.00, verbose_name = 'Сума корзини')
def __str__(self):
return str(self.id)
def add_to_cart(self, product_slug):
cart = self
product = Product.objects.get(slug=product_slug)
current_price = Cart.getActualPrice(product)
is_present = False
for item in cart.items.all():
if item.product.slug == product_slug:
is_present = True
#cart_items = [item.product for item in cart.items.all()]
if not is_present:
#if new_item not in cart.items.all():
new_item, _ = CartItem.objects.get_or_create(product=product, item_total=current_price)
cart.items.add(new_item)
cart.save()
def remove_from_cart(self, product_slug):
cart = self
product = Product.objects.get(slug=product_slug)
for cart_item in cart.items.all():
if cart_item.product == product:
cart.items.remove(cart_item)
cart.save()
def getActualPrice(product):
if product.promoteprice != 0.00:
return product.promoteprice
else:
return product.price
def change_quantity(self, quantity, item_id):
cart = self
cart_item = CartItem.objects.get(id=int(item_id))
current_price = Cart.getActualPrice(cart_item.product)
cart_item.quantity = int(quantity)
cart_item.item_total = int(quantity) * Decimal(current_price)
cart_item.save()
new_cart_total = 0.00
for item in cart.items.all():
new_cart_total += float(item.item_total)
cart.cart_total = new_cart_total
cart.save()
class Meta():
verbose_name_plural = 'Корзини'
verbose_name = 'Корзина'