简体   繁体   中英

How to add Product to shopping cart?

How to add Product from this model to shopping cart: https://github.com/bmentges/django-cart How to run method add_to_cart ? In template I would have button "add to cart". Thanks

from django.db import models
from sorl.thumbnail import ImageField

class Product(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField()
    price = models.DecimalField(max_digits=5, decimal_places=2)
    desc = models.TextField()
    image = ImageField(upload_to='images')

    class Meta:
        verbose_name = _('Product')
        verbose_name_plural = _('Products')

    def __unicode__(self):
        return self.name

What are quantity and unit_price from a basic usage of django-cart: https://github.com/bmentges/django-cart ?

def add_to_cart(request, product_id, quantity):
    product = Product.objects.get(id=product_id)
    cart = Cart(request)
    cart.add(product, product.unit_price, quantity)

What are quantity and unit_price from a basic usage of django-cart:

Quantity is the number of that product in your line item.

If you say 3, it means there are 3 of the "product" in your cart. This is typically related to an input type='text' field with an add to cart button next to it.

unit_price is the price of a single unit of your product. It is not automatically pulled from the product because it may well differ from what the product price actually is.

For example, maybe there's a 20% sale; this system allows you to have prices in the cart that differ from the product price.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM