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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:34:39+00:00 2026-05-22T01:34:39+00:00

I’m using SimpleCart JS, and I’ve been writing a coupon-code extension for it. Since

  • 0

I’m using SimpleCart JS, and I’ve been writing a coupon-code extension for it. Since SimpleCart uses a combination of the DOM and cookies to calculate price (I’ve got the DOM portion of this problem under control), I have to change a specific set of values in the cookie in order to actually alter the price of items that are in the cart before the coupon is entered.

Here’s my question: How can I change the values of the price= variables in this cookie string?

Here’s the cookie SimpleCart creates:

id=c4||quantity=1||name=XS%20Shirt%20%28Green%29||price=10++id=c5||quantity=1||name=XS%20Shirt%20%28Royal%20Blue%29||price=10++id=c6||quantity=1||name=%20Book||price=45.50

Currently, I’m sending this to a PHP script using POST and then exploding it into an array, but I can’t figure out a way to isolate the prices so that I can modify them with a discount. The ++ that separates the prices from the id are throwing me off. I’m sure there’s a way that Regex can do this, but all of my attempts at it have failed.

Here’s the PHP script that I’m currently using to process the string:

$currentCookie = $_POST['currentCookie'];

$amount = $_POST['couponAmount'];   

$orderArray = explode('||',$currentCookie); 

print_r($orderArray);

Given the above string, this script returns this:

Array ( [0] => id=c4
[1] => quantity=1
[2] => name=XS%20Shirt%20%28Green%29
[3] => price=10++id=c5
[4] => quantity=1
[5] => name=XS%20Shirt%20%28Royal%20Blue%29
[6] => price=10++id=c6
[7] => quantity=1
[8] => name=%20Book
[9] => price=45.50 ) 

I need to modify the price values based on the $amount variable. The $_POST string looks like this:

?currentCookie=id=c4||quantity=1||name=XS%20Shirt%20%28Green%29||price=10++id=c5||quantity=1||name=XS%20Shirt%20%28Royal%20Blue%29||price=10++id=c6||quantity=1||name=%20Book||price=45.50&amount=10

The goal is to reduce all the prices by 10%, when $amount = 10. The math part looks like this:

$newPrice = $currentPrice - ($amount / 100 * (currentPrice));

Thanks to the answerers. Here’s what I wound up doing:

    $currentCookie = $_POST['currentCookie'];

function applyDiscount($price){

    $couponAmount = $_POST['couponAmount'];

    $newPrice = $price - ($couponAmount / 100 * ($price));

    return round($newPrice, 2);

}

$orderArray = explode('||',$currentCookie);

$output = array();

$pricePattern = '/(price=)([^\+]*)/';

foreach($orderArray as $item){

$currentPrice = preg_match($pricePattern, $item, $matches);

    $newPrice = preg_replace($pricePattern, applyDiscount($matches[2]), $item);

    if ($matches){
    array_push($output, 'price='.$newPrice);
    } else {
        array_push($output, $item);
    }

}

$output = implode('||', $output);

echo $output;

This takes the incoming string, splits it into an array, searches for and modifies the price (thanks to @stema), and then replaces the price variable.

Everything gets sewn back together and sent back to the page, where jQuery replaces the old cookie with the new one.

If anyone has a better or more elegant solution to this, I would really love to hear it. This feels clunky, but it works.

  • 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-22T01:34:39+00:00Added an answer on May 22, 2026 at 1:34 am

    You can find the price herewith

    (price=)([^\+]*)
    

    See it online here on Regexr

    I put the price= in group 1 and the value in group 2. The trick here is to match anything that is not a +. This will work when every price is really limited by a + or by the end of the string.

    You can then replace with $1NEWPRICE.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

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.