I Am new in working with CartThrob and EE2. How i can assign {exp:cartthrob:cart_total}
value to a php variable.Please let me know how to do that.
Thanks in advance.
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It only takes a minute to sign up.
Sign up to join this communityI Am new in working with CartThrob and EE2. How i can assign {exp:cartthrob:cart_total}
value to a php variable.Please let me know how to do that.
Thanks in advance.
As with what @foamcow said, this is generally a bad idea, especially if you're new to PHP or ExpressionEngine in general. I'm providing you an example, but I highly recommend against going this route. You do so at your own risk. Understanding the differences between input
and output
parsing is huge, and it sounds like you are new to these concepts. Please revise your question and expand on what you are trying to accomplish, and we can probably give you a safer answer.
For the template in question, set it's PHP parsing to Output
.
https://ellislab.com/expressionengine/user-guide/templates/php.html
Then, in your template code, you can use this code to grab that value:
<?php
// grab cart_total, cast to int
$my_php_var = (int) "{exp:cartthrob:cart_total}";
// what **are** you trying to do, anyways? let's add 10
$my_php_var += 10;
echo $my_php_var
?>
Again, we highly recommend not going this route.
You would need to enable PHP parsing for the template where you are using the variable. The parsing can be done on "input" or "output" and you will need to choose a setting depending on what you are trying to do.
Be aware that enabling PHP within template code can be a security risk and there is often a "better" way to do things by means of creating a custom plugin or extension. This keeps the PHP away from your template code.