PDA

View Full Version : Passing variables into php code


vhound
17-12-09, 10:32
Hello, the shopwindow software is great. However, I'm having a torrid time passing a variable which includes an array into a block of php code.

I've tried use this to get the variable;

<?php
$x = $this->get_template_vars('value');
?>

This works fine for a basic value but if the variable holds an array of data it simply won't capture it. Simply put I need this $oProduct->iMerchantId but how do I get this into a php block?

Andy
17-12-09, 11:11
Hi vhound

Welcome to the forums

How exactly are you using the software?

If you're using the client, then this is a Smarty issue, so you would add a variable like so:

Content here
{$oProduct->iMerchantId}
Content here
If you are using bespoke code plugged into the api, then there is a good chance that $oProduct->iMerchantId hasn't actually been set.

Whichever you're using, this eg:


$x = '123';
$y = array('1','2','3');

echo $x.'<br />'.$y;


Would print out:

123
Array


to print out an array for you to see, use:

print('<pre>');
print_r($y);


Let me know where you are and what abilities you already have and I'll probably be able to help abit more.

Andy

vhound
17-12-09, 11:28
The values have been set as I have a printout of the variables used. I am a total noob in Smarty but have some experience in PHP. I am using shopwindow in conjunction with the affiliate window api. I'm trying to include voucher codes onto a product page. I'm trying to use the variable $oProduct->iMerchantId pass it into my block of php code and then print on the page under the product info a list of the relevant vouchers available to that merchant.

I am so close but getting the merchant id is causing me a major headache. Thanks for your help so far.

GeorgeGaz
17-12-09, 12:28
Hello vhound,

The below thread might be of some use. You could see how this user is pulling voucher codes from the API and possibly adjust to your code accordingly:

Discount Codes for a single merchant (http://www.shopwindowforum.com/showthread.php?t=1331&highlight=voucher+codes).

Hope that helps.

Cheers

vhound
17-12-09, 12:34
Thanks George but strangely enough that particular code is what I am using. However, the current code need the array to be manually entered. The store id needs to be defined in the php code. What I want to do is get the array value for the merchant from the $oProduct->iMerchnatId variable so that the code is dynamic.

Again thanks for the suggestion. The code works fine btw just need the variable value to complete it.

vhound
17-12-09, 13:07
Using this;

<?php
$x = $this->get_template_vars($oProduct);
?>

i get the response: 'array' how can I get the iMerchantId value out of the array in PHP?

I have tried:

<?php
echo $x['iMerchantId'];
?>

but this doesn't work. Is this the wrong way to go about this? Another thought of mine would be to assign the array value to a variable in smarty and then just call that new variable in php. Any ideas on how to assign a new variable with a value from an array? Searched the forums here and in smarty but can't find anything that works.

Andy
23-12-09, 18:00
$this is always an Array as $this is just a way of accessing where the pointer is when iterating over an array or object.

The easiest thing to do would be to use the following in your php:


print('<pre>');
print_r($oShopCore);
That will print out the entire shopcore object which contains everything in an object or arrays.

From there, find the bit you want to use, and add it to the $oShopCore variable. For instance (in productlist.php)

$oShopCore->oApiMerchant->oMerchant contains every merchant you are joined with

$oShopCore->oApiProductList->oResponse->oProduct contains all the products listed

Therefore, this would print out each merchant name and ID for each product in the order the product is displayed on the page:



foreach($oShopCore->oApiProductList->oResponse->oProduct as $product){
echo 'name: '.$product->oMerchant->sName.' - id: '.$product->oMerchant->iId.'<br />';
}
In smarty, everything is assigned into the oSmarty object at the bottom of the PHP files. So to display the merchant ID of a product in the Smarty templates (assuming templates/TEMPLATENAME/elements/product_display_list.tpl) we use:

{$oProduct->oMerchant->iId}If you add that somewhere within the foreach loop of templates/TEMPLATENAME/elements/product_display_list.tpl, you'll get the merchant ID of each product.

vhound
04-01-10, 12:19
Thanks for the help but managed to do it another way. Quite simple really. All I did was assign a new variable with the variable I needed

{assign var='test' value=$oProduct->oMerchant->iId}

then I just called in the new variable 'test' into the voucher code.

Working fine now.