开发者

What's the best way to change specific values in a cookie?

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开发者_如何学Python elegant solution to this, I would really love to hear it. This feels clunky, but it works.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜