Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Passing Custom PHP Variables Between Templates
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

I am currently working on my first Magento store and have hit my first stumbling block. I am trying to pass a custom PHP variable between templates but it seems if you declare a PHP variable in a .phtml file the variable’s scope is limited to that file.

I have the following in /app/design/frontend/default/custom/template/catalog/product/view/options/wrapper.phtml:

<fieldset class="product-options" id="product-options-wrapper">
    
    <!-- 
Initialise a variable and give it a value of 0 -->
    
<?php $tprice 0?>
    
    
<!-- get child HTMLthe value of $tprice will be changed in checkbox.phtml (see below)  -->
    
<?php echo $this->getChildHtml(''truetrue);?>
    
     
<!-- Now display the final value of $tprice -->
     
<?php echo $this->__("Normal Price: &pound;$tprice"); ?>
    
</fieldset>
<
script type="text/javascript">decorateGeneric($$('#product-options-wrapper dl'), ['last']);</script>

In /app/design/frontend/default/custom/template/bundle/catalog/product/view/type/bundle/option/checkbox.phtml:

<?php $_option $this->getOption() ?>
<?php $_selections 
$_option->getSelections() ?>
<dt><label><?php echo $this->htmlEscape($_option->getTitle()) ?><?php if ($_option->getRequired()) echo '<span class="required">&nbsp;</span>' ?></label></dt>
<
dd<?php if ($_option->decoratedIsLast){?> class="last"<?php }?>>
<?php if (count($_selections) == && $_option->getRequired()): ?>
    
        
<!-- Mod to show associated product images --> 
    
<?php foreach($_selections as $_selection): ?>
    <?php
        $_product 
Mage::getModel('catalog/product');
        
$_product->load($_selection->getProductId());
    
?>
    
<img src="<?php echo $_product->getThumbnailUrl(75,75); ?>" align="left" />
    
<?php endforeach; ?>

    
<center>
    
<?php echo $_product->getName(); ?><br /><font color="#ff0000"><b><?php echo $this->__('Normal Price: &pound;'); ?><?php echo number_format($_product->getPrice(),2,'.',','); ?></b></font>
    </
center>

        <!-- 
Now update $tprice -->
    
<?php $tprice $tprice $_product->getPrice(); ?>

    
<input type="hidden" name="bundle_option[<?php echo $_option->getId() ?>]" value="<?php echo $_selections[0]->getSelectionId() ?>" />
<?php else:?>
    
<ul class="options-list">
        
<?php foreach($_selections as $_selection): ?>
        
<li><input onclick="bundle.changeSelection(this)" class="checkbox bundle-option-<?php echo $_option->getId() ?> <?php echo $_option->getRequired() ? 'validate-one-required-by-name' : '' ?>" id="bundle-option-<?php echo $_option->getId() ?>-<?php echo $_selection->getSelectionId() ?>" type="checkbox" name="bundle_option[<?php echo $_option->getId() ?>][]"<?php echo ($_selection->getIsDefault() && $_selection->isSaleable())?' checked="checked" ':'' ?><?php echo !$_selection->isSaleable()?' disabled="disabled" ':'' ?> value="<?php echo $_selection->getSelectionId() ?>" />
            <
span class="label"><label for="bundle-option-<?php echo $_option->getId() ?>-<?php echo $_selection->getSelectionId() ?>"><?php echo $this->getSelectionQtyTitlePrice($_selection?></label></span>
            
<?php if($_option->getRequired()): ?>
                <?php 
echo $this->setValidationContainer('bundle-option-'.$_option->getId().'-'.$_selection->getSelectionId(), 'bundle-option-'.$_option->getId().'-container'?>
            <?php 
endif; ?>
        
</li>
    
<?php endforeach;?>
    
</ul>
    <
span id="bundle-option-<?php echo $_option->getId() ?>-container"></span>
<?php endif; ?>
<br /><br /><hr />
</
dd>

Hopefully that makes sense.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ben Marks
Guru
 
Avatar
Total Posts:  452
Joined:  2008-10-09
Charleston, SC
 

Perhaps a general explanation of what you are trying to do will help folks answer your query.  Magento handles value updates across templates with javascript (see http://demo.magentocommerce.com/acer-ferrari-3200-notebook-computer-pc.html )

 
Magento Community Magento Community
Magento Community
Magento Community
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

Here’s a simple breakdown of what I am trying to achieve:

1. In wrapper.phtml I am declaring a variable ($tprice) and assigning it a value of 0.

2. wrapper.phtml then calls the child HTML (which includes a call to checkbox.phtml).

3. In checkbox.phtml I am (attempting) to add the retail price of each associated product to the value of $tprice using simple PHP.

4. In wrapper.phtml I am trying to display the final value of $tprice (after the child HTML has been called).

I could do this with JavaScript but as I plan to do this a lot, using JavaScript will become laborious in the long term.

I was just wondering if there was an easier way?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ben Marks
Guru
 
Avatar
Total Posts:  452
Joined:  2008-10-09
Charleston, SC
 

1) If you have a preview URL, that would be handy. 
2) How are you trying to do something outside of standard bundle behavior? http://demo.magentocommerce.com/computer.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 
blueben - 03 December 2009 06:19 AM

1) If you have a preview URL, that would be handy.

Unfortunately I don’t, our development server is on our LAN so I cannot provide a URL.

blueben -

2) How are you trying to do something outside of standard bundle behavior? http://demo.magentocommerce.com/computer.html

When I first looked at Magento it was the first cart I noticed that supported bundled products. The bundled product feature, however, did not work in quite the way we needed it to.

We wanted a way to sell individual products as a bundle, for a fixed price that is cheaper than buying the products individually.

Now, I can do this by setting up a bundle product, setting ‘Price’ to ‘Fixed’ and adding each simple product by itself under ‘Bundle Items’ then setting ‘Input Type’ to ‘Checkbox’ and ‘Required’ to ‘Yes’. Although this works fine, the customer sees each bundle item as a price modifier on the product page (although the modifier is displayed as £0.00 and cannot be changed).

We really wanted to display:

1. The product image for each bundle product

2. The normal retail price of each bundle product

3. The saving made when buying the bundle product over buying the items individually.

I have already accomplished 1 & 2, but I am now struggling with 3.

 
Magento Community Magento Community
Magento Community
Magento Community
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

Could I use setData() to pass a variable from wrapper.phtml to it’s child templates?

 
Magento Community Magento Community
Magento Community
Magento Community
 
MageParts
Guru
 
Avatar
Total Posts:  415
Joined:  2007-11-18
 

Maybe, but you could try using the registry instead. Example:

Storing data in the registry:

Mage::register('my_data_name','some_data');

In this case “some_data” could be any type of data, such as boolean, int, string, array, etc.

Retrieving data from the registry

Mage::registry('my_data_name');

The registry keeps your data from the time it’s set, til the page has finished loading (i.e: the registry will be reset once you refresh the page).

 
Magento Community Magento Community
Magento Community
Magento Community
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

Thank you MageParts!

Exactly what I was looking for.

 
Magento Community Magento Community
Magento Community
Magento Community
 
xmivite
Guru
 
Total Posts:  355
Joined:  2009-10-16
 

my data is persistent - even upon removing the registering key - and page refresh , i have used “unregister” as well to try to unregister the data key.. what the hell?? is my version broken???

1.4.01

 
Magento Community Magento Community
Magento Community
Magento Community
 
vladyn
Jr. Member
 
Total Posts:  1
Joined:  2010-05-25
 

thanks for clarifying Register and Registry. I’m trying however assigning custom variable that way and transfer it to the next page.
There however I’m getting empty string when trying to call the variable declared on previous template.
page1.phtml:

<h1>
<?php 
    $lSearch 
Mage::getUrl('', array(
        
'_current' => true,
        
'_use_rewrite' => true
    
));

    echo 
Mage::register("lastSearch""$lSearch");
 
?>
</h1>
<
h2>
    
<?php 
    
echo Mage::registry("lastSearch");
     
?>
</h2>

get’s the right string
page2.phtml

<h1>
<?php 
    
echo Mage::registry("lastSearch");
 
?>
</h1>

this outputs nothing

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top