Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6556727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:58:54+00:00 2026-05-25T12:58:54+00:00

I’m working out a method to calculate a total for a shopping cart written

  • 0

I’m working out a method to calculate a total for a shopping cart written in PHP and would like some feedback on a good design pattern for handling the different conditions. I am trying to offer the admins multiple strategies for calculating discounts. Admins will be given the choice to apply discounts before or after taxes are applied, as well as apply the discount to the shipping or not. This gives an overview of my task.

The Variables

I have the following variables for this task with the possible values:

$tax_option : ‘before’, ‘after’

$shipping_option : ‘yes’, ‘no’

In addition to these two variables, the formula for calculating the total value will change depending on the relationship between $subtotal (the amount of the items in the cart) and the $reduction (the total amount to be discounted).

Generally speaking, my logic is that I test for each of the 4 combinations of the $tax_option and $shipping_option variables. I also need to change the formula for situations where the $subtotal is less than or equal to the $reduction. In all, I have 8 different conditions.

The Possibilities

I figure that I really have 3 different options here: if statements, a switch statement, or a strategy pattern. I’ll show the structure of the if statement and the strategy pattern, but will exclude the switch possibility because that just does not seem right in this context.

if Statements

Here is the general pattern of the if statement that I am considering. Note that I know I could restructure this slightly, but for whatever reason this is more readable for me.

if($tax_option == 'after' && $shipping_option == 'yes')
{
    if($subtotal <= $reduction)
    {

    }
    else
    {

    }
}
elseif($tax_option == 'before' && $shipping_option == 'yes')
{
    if($subtotal <= $reduction)
    {

    }
    else
    {

    }
}
elseif($tax_option == 'before' && $shipping_option == 'no')
{
    if($subtotal <= $reduction)
    {

    }
    else
    {

    }
}
elseif($tax_option == 'after' && $shipping_option == 'no')
{
    if($subtotal <= $reduction)
    {

    }
    else
    {

    }
}
else
    $new_total = $total;

Strategy Pattern

After looking around online for a solution to this problem, I learned about the Strategy Pattern, which looks awesome. I started work on it here and wouldn’t mind some feedback on it. So far it looks like the following code, with obvious deletion of some of the routines.

class DPPCalculateTotal
{
    protected $formulas = array();

    public function DPPCalculateTotal($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {
        foreach($this->formulas as $formula)
        {
            if($formula->test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction))
            {
                return $formula->calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction);
            }
        }
    }

    function add_formula(DPPFormula $formula)
    {
        $this->formulas = $formula;
    }
}

interface DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction);

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction);
}

class AfterTaxesYesShippingGreaterSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class AfterTaxesYesShippingLesserSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class AfterTaxesNoShippingGreaterSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class AfterTaxesNoShippingLesserSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class BeforeTaxesYesShippingGreaterSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class BeforeTaxesYesShippingLesserSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class BeforeTaxesNoShippingGreaterSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

class BeforeTaxesNoShippingLesserSubotal implements DPPFormula
{
    public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }

    public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
    {

    }
}

The Questions

1) In your opinion, what would be the best way to proceed here?

2) What are the advantages of the Strategy Pattern in this instance?

3) As this is the first time I’m trying out the Strategy Pattern, does it look like I’m heading in the right direction?

Thank you so much for your input! I’m having fun learning this pattern and will appreciate any feedback I can get!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:58:54+00:00Added an answer on May 25, 2026 at 12:58 pm

    I think you’re heading the right way. With the strategy pattern it’s really easy to add new strategies later on as the application gets bigger or the requirements change (which in my experience is the case 9 times out of 10).

    What I would suggest is making one more class called something like Order to encapsulate all your order details in there and pass the objects around instead. Your tests and calculation methods would be a bit tidier

    interface DPPFormula
    {
        public function test(OrderInteface $order);
    
        public function calculate_total(OrderInterface $order);
    }
    
    interface OrderInterface
    {
        function setTotal($total);
        function getTotal();
    }
    

    You could then do something similar to

    $order->setTotal($calculator->DPPCalculateTotal());
    

    Depending on your complexity you may or may not want to use an interface for the Order. I strongly suggest you do use one as this further increases the abstraction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.