Ok first of all, what is the get_price
method ?
The get_price is a method required by the dj-shop-cart package that you must implement on your django model representing the products that you are adding to the cart. The signature is as follow:
class Product(models.Model):
price = models.DecimalField(decimal_places=2, max_digits=10)
def get_price(self, item):
return self.price
The get_price
method take in a CartItem
instance and return back a python decimal value.
In the example below the get_price
nethod just returns your product price but it can do a lot
mode depending on your use case.
why the get_price
is needed ?
The main reason is for your cart to always have an updated version of the price of every product added to the cart. Let's take an example:
- User A visit your online shop
- They add an item to the cart, mulitple in fact, cause they quite like what your store is offering
- They are done with their shopping session and decide to proceed to checkout
- At the same time, you decide to offer a deal on some of your products, so their prices have changed (maybed you didn't lower the price directly, but have a special mechanis to apply deal) and it turns out that User A have in their cart one of the item that have changed price
I think you can see the issue at this point, if dj-shop-cart was getting the price in a static way, with an attribute price
on your model for example, it price will get an incorrect value because your custom logic to apply deals would be elsewhere, but with a method like get_price
you can write custom logic to make sure your cart items alwas have an up to date price. The get_price
method also get in parameter the CartItem
instance for your products, so you can make computations based on the quantity or any other metadata you could have passed to your product while adding it to the cart.
I hope this make thing clear. If you don't really what to do, you can just copy paste the get_price in the code snippet abose, just return your product price.
I will update the documentation later with this explanation and please don't mind typos I made this in a hurry.
Finally, I made it with your advise. Bingo, I add the quantity portion back to the cart.html like below and it works.
So now the last puzzle was made. Great~~~ Appreciate for the help and advising.