We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

$return_data outside of the constructor?

Development and Programming

bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Hi, I have a function that sits outside of the constructor for the class.

function purchase()
    {
    
        global $IN, $PREFS, $DB, $TMPL, $FNS;    
    
        $this->return_data = '<form method="POST" action="purchase">'
                                .'<input type="hidden" name="entry_id" value="'.$TMPL->fetch_param('entry_id').'">'
                                .'<input type="hidden" name="return_url" value="'.$_SERVER['PHP_SELF'].'">'
                                .'<input type="text" size="2" name="quantity" value="1">'
                                .str_replace('{entry}',$TMPL->fetch_param('entry_id'), $TMPL->tagdata)
                                .'<input type="submit" value="Add to cart">
                             </form>';

    }[/code]

I am calling it like this in my template.
[code]
{exp:commerce:purchase entry_id="5"}
        {entry}
    {/exp:commerce:purchase}

I get no output, but if I throw it back up in the constructor it renders to the template just fine.

Am I missing something simple here?

       
Brian M.'s avatar
Brian M.
529 posts
16 years ago
Brian M.'s avatar Brian M.

I am by no means an expert here - but I don’t think what is happening is what you think. {entry} isn’t getting replaced by anything in either case I don’t believe - to do that you need to use $TMPL->swap_var_single.

Check out this page (if you haven’t already):

http://expressionengine.com/docs/development/usage/template.html

And look specifically at Data Within Tag Pairs.

       
bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Ah, I think you are right. I will give that a try.

       
bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

I have changed to the following:

{exp:commerce:purchase entry_id="5"}
    {buy_button}
{/exp:commerce:purchase}
function purchase()
    {
    
        global $IN, $PREFS, $DB, $TMPL, $FNS;    
    
        $buy_button = '<form method="POST" action="purchase">'
                                .'<input type="hidden" name="entry_id" value="'.$TMPL->fetch_param('entry_id').'">'
                                .'<input type="hidden" name="return_url" value="'.$_SERVER['PHP_SELF'].'">'
                                .'<input type="text" size="2" name="quantity" value="1">'
                                .str_replace('{entry}',$TMPL->fetch_param('entry_id'), $TMPL->tagdata)
                                .'<input type="submit" value="Add to cart">
                             </form>';
                            
        $this->return_data = $TMPL->swap_var_single('{buy_button}', $buy_button, $TMPL->tagdata);
        
    }

Still no output to the template though.

       
bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Another weird thing.

I get this debug output

(0.119531) Calling Class/Method: Commerce/purchase (0.119670) -> Class Called: Commerce (0.119804) -> Method Called: commerce (0.119821) -> Data Returned (0.119920) - End Tag Processing -

Shouldn’t the “Method Called” be “purchase” based on my tag format.

       
Brian M.'s avatar
Brian M.
529 posts
16 years ago
Brian M.'s avatar Brian M.

bohara - one problem I see is you don’t need the { } around your var name in this last line :

$this->return_data = $TMPL->swap_var_single('{buy_button}', $buy_button, $TMPL->tagdata)
       
bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Thanks. I ended up using str_replace, because It worked. I will give that a try.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.