Shopp price string to number, removing currency sign

Performing calculations on prices, subtotal and so on is kind of a problem with earlier versions of Shopp.
You can use a command like

$myPrice = shopp('product', 'price', 'return=true');

or its equivalent

$myPrice = shopp('product.get-price');

to retrieve the price of a product and assign it to a variable. But when you start to add to $myPrice or multiply $myPrice with whatever number, you will find the outcome to be zero. Taking a closer look at $myPrice will reveal the problem, it is not a number but a string. The value will be something like “$12.50” or in other cases if variations have been set for the product something like “$10.00 — $25.00”.

You will have to jump some hoops to turn it into a number.

Your life is easier in Shopp version 1.2+ if you are trying this in your cart.php file.
There is new option. Now you can use a command like this

$myPrice = shopp('cart', 'subtotal', 'number=on&return=true');

or its equivalent

$myPrice = shopp('cart.get-subtotal', 'number=on');

Now that $myPrice is a number, you can perform calculations on it.

By default shopp(‘object’, ‘property’, ‘options’)-tags will display their generated output on screen. By adding the ‘return=true’ option, or using ‘.get-‘, you can assign the output to a variable.

link-to-php-article Image for Shopp WordPress bestfriends article
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *