Prestashop 1.6.1.16 - 我的 js 文件已加载,但没有看到我的功能

Prestashop 1.6.1.16 - My js file is loaded, but my functions aren't seen

我是 Prestashop (1.6.1.16) 的新用户。 我使用默认的 prestashop 主题(默认-bootstrap)。

我做了什么:

我把内容放在/themes/default-bootstrap/product.tpl: 在热门评论(关于 LICENSE 和其他评论)之后:

    <script type="text/javascript" src="modules/ask_bid/js/ask.js">
    </script>
    <button onclick="take_asks({$product->id})">See asks</button>
    <input type="hidden" id="product-id" value="{$product->id}" />
    <input type="hidden" id="customer-id" value="{$id_customer}" />

    <!-- Modal -->
    <div id="modal" class="modal fade">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-
                    dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal Header</h4>
                </div>
                <div class="modal-body">
                    <p>Some text in the modal.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" 
                    data-dismiss="modal">Close</button>
                </div>
            </div>

         </div>
    </div>

其中 {$product->id} 来自

/controllers/front/ProductController.php<br>

{$id_customer} 来自

/override/controllers/front/ProductController.php

我创建了 /modules/ask_bid/js/ask.js 并放置了下一个内容:

function isJSON(data) {
    var ret = true;
    try {
        JSON.parse(data);
    }catch(e) {
        ret = false;
    }
    return ret;
}


function take_asks (id_product) {
    $.ajax({
        type: 'POST',
        url: baseDir + 'modules/ask_bid/ajax.php',
        data: 'method=take_asks&id_product='+id_product,
        dataType: 'text',
        success: function(json) {
            if(isJSON(json)) {
                var json = JSON.parse(json);
                //alert("json: " + json[0].comment);

            }
        },
        error: function() {
            alert("error");
        }
    });
}

模态也不像模态 我的模式显示(未隐藏),这是不正常的。 它就在按钮之后,而不是 "in the air"(我希望你明白我的意思)。

我有 js 错误:

加载了 /modules/ask_bid/js/ask.js(我也在 INSPECT->f12/Network 中看到了这一点),但是没有看到 take_asks()

我收到下一个控制台错误(当我按下 'Take asks' 按钮时):

Uncaught ReferenceError: take_asks is not defined at HTMLButtonElement.onclick (index.php? id_product=6&controller=product&id_lang=1:413)

我试过的

-我删除了class_index.php
-我删除了缓存(使用 CTRL-f5)
-我试图从 /override/controllers/front/ProductController.php

添加 js 文件

但不起作用,我也没有收到错误:

public function setMedia()
{
    $this->addJS('modules/ask_bid/js/ask.js');

    parent::setMedia();
}

...或者...

function init () {
    $this->context->controller->addJS('modules/ask_bid/js/ask.js');

    parent::init()
}

你觉得我能做什么?

您应该在您的模块中创建一个钩子header:

public function hookHeader($params)
{
    $this->context->controller->addJS(($this->_path).'js/ask.js');
}

您只能在产品页面上添加它:

public function hookHeader($params)
{
    if (!isset($this->context->controller->php_self) || $this->context->controller->php_self != 'product')
           return;
    $this->context->controller->addJS(($this->_path).'js/ask.js');
}

并在您的模块中使用

安装挂钩到 header
$this->registerHook('header'))

要在不更改主题 tpls 的情况下向产品页面添加内容(如评论中所述),您可以使用 "Add new blocks under the product description.".

的 displayFooterProduct
public function hookDisplayFooterProduct($params)
{
    return "code you want to insert";
}

在此挂钩中,您可以访问以下参数: $params = array('product' => 产品, 'category' => 类别)

此外,记得在安装时使用 $this->registerHook('displayFooterProduct')) 挂钩它,如果模块已经安装,请重置它或手动挂钩它。