yonka's avatar
Level 2

bumbummen99/shoppingcart discount change from percentage rate to fixed

I want to change Discount Rate from percentage to fixed discount

0 likes
1 reply
LaryAI's avatar
Level 58

To change the discount from a percentage rate to a fixed amount in the bumbummen99/shoppingcart package, you will need to modify how the discount is applied to the cart total. Here's a general approach to achieve this:

  1. Identify where the discount is applied: Typically, this would be in the part of your code where you calculate the total price of the cart.

  2. Modify the discount logic: Instead of applying a percentage discount, you will apply a fixed amount.

Here's a basic example of how you might implement this:

use Gloudemans\Shoppingcart\Facades\Cart;

// Assume $fixedDiscount is the fixed discount amount you want to apply
$fixedDiscount = 10.00; // Example fixed discount amount

// Get the original total
$originalTotal = Cart::subtotal();

// Calculate the new total after applying the fixed discount
$newTotal = $originalTotal - $fixedDiscount;

// Ensure the total doesn't go below zero
$newTotal = max($newTotal, 0);

// Display the new total
echo "Total after discount: $" . number_format($newTotal, 2);

Steps:

  • Retrieve the original subtotal: Use Cart::subtotal() to get the current total of the cart.
  • Apply the fixed discount: Subtract the fixed discount amount from the subtotal.
  • Ensure non-negative total: Use max() to ensure the total doesn't go below zero.
  • Display the new total: Format and display the new total.

Considerations:

  • Validation: Ensure that the fixed discount does not exceed the subtotal to avoid negative totals.
  • Currency Formatting: Use appropriate currency formatting for displaying the total.
  • Integration: Make sure this logic is integrated into the part of your application where the cart total is calculated and displayed.

This approach assumes you have access to modify the part of your application where the cart total is calculated. If the package provides hooks or events for modifying the total, consider using those for a more integrated solution.

Please or to participate in this conversation.