Smarty Variables in {php}

In version 1.3.5, I used the following to pull Smarty variables into some PHP code in a template:

{php}
global $smarty;
$skins = $smarty->_tpl_vars['skin_dir'];
{/php}
In version 2.0, this isn’t working. Anyone know how I can use a Smarty variable inside {php}{/php} tags?

Turns out I can do everything I needed to do with Smarty alone. However, the following code is returning the {else} result, and I know the file exists. Any ideas?

{if file_exists("/store/skins/basic/customer/images/head-profile-details.jpg")}
1
{else}
0
{/if}

Dear silly Borat, you are going off your html or www folder, the proper way is to go from the root folder, it is not finding the file since the path is off. (you are confusing how browsers and php see paths)



do:



echo dirname(file); (or similar)



or look in phpinfo() for the actual path…



it will be like /var/www/mysite/htmldocs/store/skins/basic/customer/images/head-profile-details.jpg



Once you will find the FULL path to the file, use that. You can also use $_SERVER values…

To help you with your original question, do this:



{php}

echo ‘

’;

print_r(get_defined_vars());

{/php}



It will print a list of all smarty variables in the array and what values hold them. The list will be extensive but complete. Thus, then you can easily find your value and know how to access it.

I saw the list of all variables, and the one I need to access is: [this] => [_tpl_vars] => [cart] => [subtotal]



So, how do I get it’s value into my {php} stuff?

My untested but educated guess would be

{php}

echo $this->_tpl_vars[‘cart’][‘subtotal’];

{/php}



Checkout: [url]http://forum.cs-cart.com/showthread.php?t=17286[/url]

Do you have any idea where the shopping cart subtotal would be stored? When I access get_defined_vars() on a product page, the [cart] array is not present - even when I have items in my cart. My cart subtotal is $89.98, but that number is not in the get_defined_vars() text anywhere. Strange.

That is weird. But the subtotal is in the [cart][subtotal]



[cart] => Array

…(

…[products] => Array

…(

…[3144010713] => Array

…(

…[product_id] => 29924

…[amount] => 1

…[product_options] => Array

…(

…[1502] => 12455

…[747] => 3185

…[795] => 3679

…)



…[price] => 259.99

…[stored_price] => N

…[extra] => Array

…(

…[product_options] => Array

…(

…[1502] => 12455

…[747] => 3185

…[795] => 3679

…)



…[return_period] => 10

…)



…[stored_discount] => N

…[return_period] => 10

…[modifiers_price] => 0

…[is_edp] =>

…[edp_shipping] =>

…[base_price] => 259.99

…[display_price] => 259.99

…)



…[3981506623] => Array

…(

…[product_id] => 29784

…[amount] => 1

…[product_options] => Array

…(

…[1048] => 7083

…[1049] => 7089

…[1050] => 7093

…[1051] => 7097

…)



…[price] => 59.99

…[stored_price] => N

…[extra] => Array

…(

…[product_options] => Array

…(

…[1048] => 7083

…[1049] => 7089

…[1050] => 7093

…[1051] => 7097

…)



…[return_period] => 10

…)



…[stored_discount] => N

…[return_period] => 10

…[modifiers_price] => 0

…[is_edp] =>

…[edp_shipping] =>

…[base_price] => 59.99

…[display_price] => 59.99

…)



…)



…[recalculate] =>

…[user_data] => Array

…(

…)



…[tax_subtotal] => 0

…[discount] => 0

…[total] => 319.98

…[amount] => 2

…[pure_subtotal] => 319.98

…[subtotal] => 319.98

…[use_discount] =>

…[shipping_required] => 1

…[shipping_failed] => 1

…[stored_taxes] => N

…[shipping_cost] => 0

…[display_shipping_cost] => 0

…[coupons] => Array

…(

…)



…[free_shipping] => Array

…(

…)



…[options_style] => F

…[no_promotions] => 1

…[promotions] => Array

…(

…)



…[subtotal_discount] => 0

…[shipping] => Array

…(

…)



…[taxes] => Array

…(

…)



…[display_subtotal] => 319.98

…[payment_surcharge] => 0

…)

For me, [cart][subtotal] shows up on the shopping cart page, but I don’t get it on the home page, category pages, or product pages. Do you have the variable there?

Ok, you owe me. :slight_smile: I spent like 30 mins looking for a solution.


  1. you are right, on categories the [cart] is not available.
  2. however, you still see in the header the total for your cart
  3. found that you can get that number with: {$smarty.session.cart.display_subtotal}

BUT… can you get the value of {$smarty.session.cart.display_subtotal} to work inside {php}{/php}? I tried, but I got an error message.

My understanding:



{assign var=“subtotal” value=$smarty.session.cart.display_subtotal}



{php}

echo $this->_tpl_vars[‘subtotal’];

{/php}

That works!



Pure genius.Thank you!

print_r($_SESSION[‘cart’]) will show you what you need.

May be a bit late, but good old {$var |@print_r} is the prettiest solution I guess.

May be a bit late, but good old {$var |@print_r} is the prettiest solution I guess.

More readable way with CS-Cart function

{$var|fn_print_r}

And if you don't want it blasting over your screen, you can put it in comment like:


Then just use the browser inspector to see the results. You don't destroy ajax response or other things this way.