如何在 prestashop 中移动挂钩?

How to move a hook in prestashop?

我需要将我的挂钩移到产品描述和添加到购物车按钮之间,在我在照片中标记的区域

到目前为止,我只能使用管理员将其放在社交网络按钮之前和之后,但我需要将我的模块放在按钮之前。

这就是我创建挂钩的方式:

public function install()
    {
        if (!parent::install() || !$this->registerHook('displayProductAdditionalInfo'))
            return false;
        return true;
    }

public function uninstall()
    {
        if (!parent::uninstall() || !$this->unregisterHook('displayProductAdditionalInfo'))
            return false;
        return true;
    }

public function hookDisplayProductAdditionalInfo($params)
    {
        return $this->runnable();
    }

这是我的文件,其中包含我要显示的 iframe

{block name='product_description_short' prepend}
    <style type='text/css'> 
        .products { border: none; width: 100%; height: {$height}px;  } 
    </style>
    <iframe class='products' src='{$iframe}{$token}'></iframe>
{/block}

如何在不接触主题源代码的情况下将挂钩移动到我需要的位置?

这是一个内置的 Prestashop 挂钩,它必须留在那里。

你可以做的是注册一个你自己的自定义钩子,比如 displayMyHookHere,这里有一个如何做的指南hooks in PS 1.7+

将您的模块挂在上面,并将其显示在您页面上的任何位置,例如 {hook h='displayMyHookHere'}

这正是widget概念的目的所在。 Widgets 是 PS 1.7 中引入的一项高级功能,它扩展了 hooks 功能。

来自documentation

With widgets, module developers can display content everywhere the module is asked to do so. When a module implements widgets in its code, it allows:

1 a theme to call the module directly with {widget name="module_name"}

2 the core to fallback on it if a registered hook is called but its method hook() does not exist.

通过遵循相同的 link,您可以使模块小部件兼容,然后在 templates/catalog/product.tpl 中所需的位置调用小部件。

在 1.7.5.0 的 classic 主题中,您可以在 <div class="product-actions">.

之前的行 98 中调用小部件

希望对您有所帮助。

编辑:

假设您的模块名称是 'my_module',您的自定义挂钩名称是 'myCustomHook',模块 class 应该如下所示:

class MyModule extends Module implements WidgetInterface
{

    public function __construct()
    {
        $this->name = 'my_module';
        $this->version = '1.0.0';
        $this->author = 'me';
        $this->tab = 'front_office_features';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->description = ...
        $this->displayName = ...
        $this->confirmUninstall = ...
    }



    public function install()
    {
        if(!parent::install())
        {
            return false;
        }

        return true;
    }


    public function uninstall()
    {
        return(parent::uninstall());
    }

    public function renderWidget($hookName = null, array $configuration = [])
    {

        $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));

        return $this->display(__FILE__, 'views/templates/widget/my_template.tpl');

    }

    public function getWidgetVariables($hookName = null, array $configuration = [])
    {
            if($hookName === 'MyCustomHook')
            {
                $your_height_value, $your_iframe_value, $your_token_value... //  Get the values depending on your logic

                return [
                    'token' => $your_token_value;
                    'height' => $your_height_value;
                    'iframe' => $your__iframe_value;
                ];

            }
    }

在模板文件中,只需像这样调用小部件:

{widget module='my_module' hook='myCustomHook'}