Jump to content

How to inherit the smarty variables in my hook template/code


Recommended Posts

Hi,

I am developing a module (in PS 1.6) that supports adding multiple different product combinations to the basket with a single click of the add_to_cart button, in order to do this correctly and select all the applicable options i have elected to register a custom hook that replaces the product info block in the product template.

    public function hookDisplayProductInfoBlock($params)
    {
        $template = 'productInfoBlock.tpl';
        return $this->display(__FILE__, $template);
    }

And in product.tpl :

{if $useMyHook}
    {hook h='displayProductInfoBlock'}
{else}
<div class="box-info-product">
    <!-- end content_prices -->
    <div class="product_attributes clearfix" style="width:45%; margin:0 2.5%">
    <!-- quantity wanted -->
        {if !$PS_CATALOG_MODE}
            <p id="quantity_wanted_p"{if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || $PS_CATALOG_MODE} style="display: none;"{/if}>
                <label for="quantity_wanted">{l s='Quantity'}</label>

unfortunatley since this segment of template code is quite large, the list of params i have to pass in to the hook code to make it all work correctly is enormous... (OOS flags, productPrice, productPriceWithoutReduction, priceDisplay and other flags....)

What i'm after is a way to inherit the smarty variables from product.tpl, into the hook template.

A rather hacky workaround i found is to return the path to productInfoBlock.tpl in the hook method:

    public function hookDisplayProductInfoBlock($params)
    {
        $template = 'productInfoBlock.tpl';
        $includePath = _PS_MODULE_DIR_.$this->name.'/views/templates/hook/'.$template;
        if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$this->name.'/views/templates/hook/'.$template)) {
            $includePath =_PS_THEME_DIR_.'modules/'.$this->name.'/views/templates/hook/'.$template;
        }
        return $includePath;
    }

and then in product.tpl :

{if $useMyHook}
    {include {hook h='displayProductInfoBlock'[spam-filter]
{else}
<div class="box-info-product">
    <!-- end content_prices -->
    <div class="product_attributes clearfix" style="width:45%; margin:0 2.5%">
    <!-- quantity wanted -->
        {if !$PS_CATALOG_MODE}
            <p id="quantity_wanted_p"{if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || $PS_CATALOG_MODE} style="display: none;"{/if}>
                <label for="quantity_wanted">{l s='Quantity'}</label>
                <input type="text" min="1" name="qty" id="quantity_wanted" class="text" value="{if isset($quantityBackup)}{$quantityBackup|intval}{else}{if $product->minimal_quantity > 1}{$product->minimal_quantity}{else}1{/if}{/if}" />

since this works as a regular include, the included template has access to all the parent's templates variables, but i don't like it, it's a bodge, and seems like it might cause problems elsewhere, or be exploitable by hackers.

What is the recommended approach for this situation?

I have tried in my hook method to inspect the contents of $this->smarty (contains the vars, but not addressable in the same way), $this->smarty->parent, $this->smarty->smarty, i also tried calling getTemplateVars on all of these, and various other combinations, but have had no joy. 

I look forward to your answers .

Kind Regards

Andrew Cornforth

Edited by acorn
use general rather than specific variable names for ease of understanding (see edit history)
Link to comment
Share on other sites

Within your hook, you can get the products from the current controller. Something like:

if (method_exists($this->context->controller, 'getProduct') && ($product = $this->context->controller->getProduct())) 

This will return of instance of class Product.
Is this what you're looking for ?

Link to comment
Share on other sites

@Pierre_d that's a useful suggestion, although i could achieve much the same thing using the syntax {hook h='displayProductInfoBlock product=$product} and still have access to the product.tpls instance of $product, which is what I want.... the issue is that i also need to pass in a dozen other variables in this fashion. making for a very long line of code just to insert the hook. I am asking if there is a better practise than this when you need to inherit 20 or more variables from the parent template, or so many that you may as well import them all.....

Link to comment
Share on other sites

another issue i have discovered with the current workaround (using include, with the path to the modules template file) is that i cannot assign vars into the modules template from the modules controller.... so I really need a better solution for this

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...