Chapter 33: Build an eCommerce Profit Margin Calculator Using OOP

Business is essentially a calculation. Based on the calculations that integrate with external and internal factors, people can stand on a giant shoulder and evaluate the current status and predict a more precise future. It’s not 100% correct, as the change is beyond the plan. However, it ensures you are indeed on the way to the goal instead of going the wrong way. The goal here is profitable whatever strategies you are implementing. And in the eCommerce space, SKU profit margin calculation is applicable as well.

In this Chapter, I will walk you through what and why OOP is for building a P&L calculator. By the end of this Python Tutorial, you can master how to write Python coding scripts to build the calculator that fits your business model.

Table of Contents: Profile Margin Calculator

What’s OOP – Object-Oriented Programming

​​Object Oriented Programming or OOP is a programming paradigm that relies on the concept of classes and objects. You can use it to structure a software program into simple, reusable pieces of code blueprints, which we usually call classes. And in a class, you can create individual instances of objects.

OOP basically consists of the parent class (can have child class), class attributes, and methods (It’s the function, but OOP calls them methods).

Take the eCommerce P&L calculator for an example. eCommerce has different types of business models or is selling different product categories. It has domestic, cross-border, dropshipping, B2B wholesale, etc. Each type of eCommerce business can be a specific instance of an object. It should need a different calculation algorithm and attribute value. It’s because the variable cost item would be slightly different. However, no matter which type of eCommerce, it should have some general attributes to impact the profit margin.

The profit margin attributes are:

  • Landed cost (Including product per unit cost, plus international shipping and tariffs if it’s a cross-border eCommerce)
  • Fulfillment cost
  • Payment processing cost
  • Refund cost
  • Cost per sale
  • Discount cost
  • The SKU price

In OOP, the concept of class is fundamental. Each class consists of attributes and methods. Then, it can have different objects using this class. Each object in the e-commerce P&L calculator can represent your business model. In other words, all objects are using the same parent class, however, the local attribute value and methods are different.

For example, domestic eCommerce P&L might not have international shipping costs. So the landed cost value would be different with cross-border eCommerce.

If you like to learn more details regarding eCommerce variable costs, please check out this article

Selling Well But Be Losing Money? 7 Variable Costs Are Impacting Your Online Store Profit Margin

Methods: Algorithm of Cross border eCommerce Profit Margin Calculator

The calculation algorithm is the critical component of the calculator. It’s because of two reasons. The first reason is you absolutely like to have the right SKU profit margin calculation for reference. Then, the 2nd reason is you like to ensure users are clear about what data and data format she or he should input on your calculator. Sometimes it can be very complex if your calculator aims to help users to find out a number with more complex factors.

For example, generally, the profit margin formula is (SKU price – selling cost) / SKU price. And you can try to break down the selling cost into landed cost, fulfillment cost, refund cost, payment processing cost, CPS, and discount cost. Depending on the object of the calculator, you can further break it down if necessary, for example, it requests users to input weight, size, and destination for the fulfillment cost. Or here I will take refund rate input for an example to show the algorithm

The object algorithm that connects with the attribute value is:

(SKU price - landed cost - fulfillment cost - (refund rate * fulfillment cost * 2) - payment processing cost - discount rate * SKU price - CPS) / SKU price

Python OOP codings of an SKU Profit Margin calculator

I take the cross-border eCommerce P&L calculator as the parent class in this Python OOP. Now the scope of attributes and calculation algorithm is ready. Below are the codings of the core engine of the calculator.

In Python OOP, the first thing is to create a parent class. In this class, there are three fundamental components. They are the _init__ attribute, attribute association and a method definition.

_init__ attribute

This is the self-default object and method of the class. We need to def the default method by adding all attributes here in the positional arguments in the __init__( ). As we are building a cross-border eCommerce P&L calculator, the attribute items are the price and variable costs related to this model.

def __init__(
self,
landedCost,
shippingCost,
paymentCost,
refundCost,
CPA,
discount,
price
):

Assign to self-object

Self-object is ready, so you can create the variables that can be associated with the attributes in the positional arguments. So you can start using the variables for all methods in this object.

self.landedCost = landedCost
self.shippingCost = shippingCost
self.paymentCost = paymentCost
self.refundCost = refundCost
self.CPA = CPA
self.discount = discount
self.price = price
Define the P&L calculator algorithm method

The OOP-object is to calculate the profit margin after inputting the SKU attribute values. So we need an algorithm embedded in this class. For this, we can create a method by using def, and the positional argument can be self.

def calculate_profitmargin(self):

return (self.price - self.landedCost - self.shippingCost - self.price * self.paymentCost - self.shippingCost * 2 * self.refundCost - self.CPA - self.discount * self.price) / self.price

As mentioned, the calculation algorithm varies depending on the actual needs. Thus, you can define more algorithm methods in this class, for example, you can set the other one that replaces the SKU price with the AOV.

Flask scripts

Flask is a friend and powerful web application builder that can integrate with your programming scripts, such as Python, HTML, CSS, PHP, etc. If you like to learn more details about how to install, set up and use it to build and test Python web applications, please check out this article

Chapter 26: Create a Shopify Bot Web Application Using Flask and Heroku

In the Flask script of the calculator application, there are two primary things you need to be taking care of. They are the Python script and the HTML script.

Python Script in the Flask Script

The Python script here needs to connect the front-end html table which is for collecting the attribute value. Meanwhile, it needs to connect with the OOP calculator algorithm by feeding the collected data from the html page. One of the most important things is the positional argument, please match well the input data variable and the positional argument, for avoiding the wrong result in the calculation

Self-object:

profitMargin1 = item(landedCost1, shippingCost1, paymentCost1, refundCost1, CPA1, discount1, price1)

Assign the calculation algorithm

result = "{:.2%}".format(round(profitMargin1.calculate_profitmargin(), 2))

You might notice, we convert the calculation into a percentage number. That’s because the outcome from calculate_profitmargin is a floating number. That would create a better user experience

Keep the input value in the result page

Users would like to double-check what number they input into the box generating the result. Thus, keeping the input value is important if you like to create a better user experience. Thus, in the return, we need to add these parameters apart from selecting the template of html page.

return render_template("result.html", price1=price1, landedCost1=landedCost1, shippingCost1=shippingCost1, paymentCost1=paymentCost1, refundCost1=refundCost1, CPA1=CPA1, discount1=discount1, result=result)

HTML template pages

Html pages are the front end where you show your calculator layout and design, and interaction blocks to the users. I’ll release another article regarding how to build and decorate a web page using html and CSS. But in this application, the key thing is you need to make sure all collected data can be related to the right positional argument. Otherwise, the calculation result might be wrong

Thus, as you can see, each input box has a related name element that is connected with the same name of the variable you use to calculate the profit margin.

Full Python Script of eCommerce Profit & Loss Calculator

If you are interested in the full script of the eCommerce Profit & Loss Calculator, please subscribe to our newsletter by adding the message “Chapter 33”. We would send you the script immediately to your mailbox.

Contact us

I hope you enjoy reading Chapter 33: Create an eCommerce Profit Margin Calculator Using OOP. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support and donate to our channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article on your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount codes
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)

FAQ:

Q1: What is Object Oriented Programming?

A: Object Oriented Programming (OOP) is a programming paradigm that organizes data and behaviors into reusable structures called objects. It focuses on the concept of classes and objects, allowing for modular and efficient code development.

Q2: What are the benefits of using Object Oriented Programming?

A: Some benefits of using Object Oriented Programming include code reusability, modularity, encapsulation, and abstraction. OOP allows for easier maintenance and updates, as well as faster development times.

Q3: How does Object Oriented Programming differ from procedural programming?

A: Procedural programming focuses on writing procedures or functions that operate on data, while Object Oriented Programming focuses on creating objects that contain both data and functions. OOP promotes code organization and reusability.

Q4: What are classes in Object Oriented Programming?

A: Classes are the blueprint or template for creating objects in Object Oriented Programming. They define the properties and behaviors that objects of that class will have.

Q5: What are objects in Object Oriented Programming?

A: Objects are instances of a class in Object Oriented Programming. They are created from the class blueprint and can have their own unique data and behaviors.

Q6: What is inheritance in Object Oriented Programming?

A: Inheritance is a feature of Object Oriented Programming that allows classes to inherit properties and behaviors from other classes. It promotes code reuse and hierarchy.

Q7: What is polymorphism in Object Oriented Programming?

A: Polymorphism is the ability of an object to take on many forms. In Object Oriented Programming, it allows objects of different classes to be treated as objects of a common superclass.

Q8: What is encapsulation in Object Oriented Programming?

A: Encapsulation is the process of hiding the internal details of an object and exposing only the necessary information. It helps in maintaining code integrity and security.

Q9: What is abstraction in Object Oriented Programming?

A: Abstraction is the process of simplifying complex systems by breaking them down into smaller, more manageable parts. It allows for easier understanding and maintenance of code.

Q10: What are some popular programming languages that support Object Oriented Programming?

A: Some popular programming languages that support Object Oriented Programming include Java, C++, Python, Ruby, and C#.