Skip to content

Instantly share code, notes, and snippets.

@Tobi-De
Last active October 11, 2022 10:53
Show Gist options
  • Save Tobi-De/b973902cfd32bc9cf1a81424cba94bee to your computer and use it in GitHub Desktop.
Save Tobi-De/b973902cfd32bc9cf1a81424cba94bee to your computer and use it in GitHub Desktop.
This not explains the reason behind the get_price method needed by dj-shop-cart

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:

  1. User A visit your online shop
  2. They add an item to the cart, mulitple in fact, cause they quite like what your store is offering
  3. They are done with their shopping session and decide to proceed to checkout
  4. 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 priceon 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.

@Tobi-De
Copy link
Author

Tobi-De commented Oct 11, 2022

t. I am a sort of beginner of python and would like to learn more, and I found that you are a developer of python. Would like to know where I can get to know more in this area....like community or forum, Hope one day I can contribute myself to the python community.

@hswIT
If you want to learn more about python, I suggest this series of tutorials.

Most importantly, don't spend too much time on tutorials, if you want to get good at python (or anything really) you need lots of practice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment