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 8790567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:41:28+00:00 2026-06-13T22:41:28+00:00

This function successfully calculates a global percentage using a single discount code, although I’m

  • 0

This function successfully calculates a global percentage using a single discount code, although I’m having problems making the code discount a specific range of items instead of globally. The basket session holds id, name, desc, price, qty etc… but everything I’ve tried thus far has failed, I realize I need to implement it into the foreach loop (which I keep breaking or updates every item one way or another) or use a different method entirely but I’m really unsure as how to proceed.

I’m not an experienced PHP Dev by the way I just enjoy learning and working basic markup. Any helpful pointers someone can provide would be very much appreciated. Thanks!

    $code = $this->request['code'];

    $discount_status = 'NONE'; // no discount available / applied
    if ( !empty($this->config['DISCOUNT_CODE']) && !empty($this->config['DISCOUNT_PERCENT']) && !empty($code) ) {
        if ( $code == $this->config['DISCOUNT_CODE'] ) {
            $discount_status = 'OK';
        }
        else {
            $discount_status = 'ERROR';
        }
    }       

    // calculate basket total
    $basket_value = 0;
    $discount_factor = 1;
    if ( $discount_status == 'OK' ) {
        // apply discount
        $discount_factor = ( 100 - $this->config['DISCOUNT_PERCENT'] ) / 100;
    }   

    if ( !empty($_SESSION['basket']) && is_array($_SESSION['basket']) ) {       
    foreach( $_SESSION['basket'] as $basketItem ) {                 
        // Apply discount to each item separately       
        $basket_value += sprintf("%.02f", $basketItem['detail_price'] * $discount_factor) * $basketItem['qty'];  
        }       
    }   

    // check delivery cost
    $delivery_area = $this->DeliveryAreaManager->getRecord($this->request['da']);
    if ( !empty($delivery_area['cost']) && ( $delivery_area['free_from'] <= 0 || $basket_value < $delivery_area['free_from'] ) ) {
        $basket_value += $delivery_area['cost'];
    }

    // return JSON response
    $data = JSONUtils::encode(
        array(
            'discount_status' => $discount_status,
            'basket_value' => sprintf("%.02f", $basket_value),
        )
    );

    return $data;
  • 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-06-13T22:41:29+00:00Added an answer on June 13, 2026 at 10:41 pm

    This:

    foreach( $_SESSION['basket'] as $basketItem ) {                 
        // Apply discount to each item separately       
        $basket_value += sprintf("%.02f", $basketItem['detail_price'] * $discount_factor) * $basketItem['qty'];      
    }
    

    Is saying to apply the discount to each item. It has no conditions. You would need to define the conditions that tell the code what to apply the discount to. This is extremely broad so we CAN’T tell you how to do this for YOU, but we CAN tell you how you MIGHT do it in a very horribly generic way…

    foreach( $_SESSION['basket'] as $basketItem ) {                 
        // Apply discount to each item separately
        if($basketItem['discount_bracket'] == 1)
        {
            $basket_value += sprintf("%.02f", $basketItem['detail_price'] * $discount_factor) * $basketItem['qty'];  
        }
        else
        {
            $basket_value += sprintf("%.02f", $basketItem['detail_price']) * $basketItem['qty'];
        }
    }
    

    Now, obviously I just made up $basketItem[‘discount_bracket’] to tell you that you can make discount tiers or some kind of discount identifier for an item via the database. In this case, I just use it to say if 1 then the item can be discounted. It’s up to you to make this do what you want it to do with what you have available. Seeing as we don’t know what you can do or what you have available, we are limited in what we can suggest.

    This is just one way to handle things. If you narrow down your question I might update with a more specific example.

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

Sidebar

Related Questions

I am using this bit of code successfully: $(window).bind('resize',function() { window.location.href = window.location.href; });
I can successfully do this: App.SomeCollection = Backbone.Collection.extend({ comparator: function( collection ){ return( collection.get(
This code isn't for anything in particular. I'm just trying to successfully get the
I have this function that successfully populates a textarea with text from a select,
I'm making this function that takes a string input, converts it to an int,
Why does this function successfully execute the database query, yet still results in Fatal
I was using zclip copy to clipboard function successfully until I started using twitter
I'm successfully using the boxplot function to generate... boxplots. Now I need to generate
I m Drawing the image on canvas with this code and it successfully draw
This function works fine: std::string get_str() { return std::get<0>( make_tuple(std::string(hi)) ); } But if

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.