B
B
blazer052017-08-22 17:50:55
Django
blazer05, 2017-08-22 17:50:55

How to get a discount in a function?

There is a discount method in the product model

def get_sale(self):
        '''Расчитать стоимость со скидкой'''
        price = int(self.price * (100 - self.discount) / 100)
        return price

Everything works, but the real price without a discount is added to the cart. You need to do if there is a discount, then it is added to the basket at a discount, if not, then the real one.
I understand that you need to write a check in the function, but I don’t understand how?
# Страница товара
def ProductDetail(request, id, slug):
    top_five_products = Product.objects.all().exclude(id=id).order_by()[:10]
    alboms = Albom.objects.filter(product=id)
    product = get_object_or_404(Product, id=id, slug=slug, available=True)
    cart_product_form = CartAddProductForm()
    return  render(request, 'shop/product/detail.html', {'product': product,
                                                         'cart_product_form': cart_product_form,
                                                         'alboms': alboms,
                                                         'top_five_products': top_five_products})

Now the product is added to the cart like this
<form action="{% url "cart:CartAdd" product.id %}" method="post" class="add">
                    {% csrf_token %}
                    <p>{{ cart_product_form }}</p>
                 </div>
                 <div class="single-item-more-action">
                    <input type="image" src="{% static 'img/item-action-btn.png' %}">
                 </div>
                 </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-08-22
@kentuck1213

Everything is ready, why reinvent the wheel again?
https://github.com/bmentges/django-cart

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question