模块上的产品自动完成输入(Prestashop)

模块上的产品自动完成输入(Prestashop),prestashop,prestashop-1.6,Prestashop,Prestashop 1.6,我正在开发一个prestashop模块,该模块必须列出现有产品的列表 对于模块的配置面板,使用renderForm()和getContent(),我尝试复制“accesories”功能,在该功能中,您开始在输入上写入产品的一些信息,并显示匹配的产品。选择该产品时,会将其添加到列表中。像这样: 这是目录/产品/关联选项卡的屏幕截图 我正在尝试使用PS1.6.0.14和PS1.6.1.0RC3。如何复制此功能以在模块配置面板上获取产品列表 我试着看这里,但我真的不明白一半的信息是从哪里来的。我认为

我正在开发一个prestashop模块,该模块必须列出现有产品的列表

对于模块的配置面板,使用renderForm()和getContent(),我尝试复制“accesories”功能,在该功能中,您开始在输入上写入产品的一些信息,并显示匹配的产品。选择该产品时,会将其添加到列表中。像这样:

这是目录/产品/关联选项卡的屏幕截图

我正在尝试使用PS1.6.0.14和PS1.6.1.0RC3。如何复制此功能以在模块配置面板上获取产品列表


我试着看这里,但我真的不明白一半的信息是从哪里来的。

我认为要实现这一功能,renderForm()函数是不够的,因为您必须绑定一些javascript和一些自定义html

编写一个全功能模块的过程有点长,但以附件功能为出发点不会太难,你将始终有一个关于“如何做”的参考。 我同意这一点:

1) 首先创建您的

getContent()

功能,以便能够显示自定义模板和与您的模块关联的产品,因此我们将提供一些信息:

    public function getContent(){

    //post process part to save the associations
    if(Tools::isSubmit('saveMyAssociations'){
     ... //we will see it later
    }

    $my_associations = MyModule::getAssociationsLight($this->context->language->id,Tools::getValue('id_product')); //function that will retrieve the array of all the product associated on my module table.

     $this->context->smarty->assign(array(
                    'my_associations' => $my_associations,
                    'product_id' => (int)Tools::getValue('id_product')
                ));

    return $this->display(__FILE__, 'views/templates/admin/admintemplate.tpl'); //custome template to create the autocomplete

    }

//our little function to get the already saved list, for each product we will retrieve id, name and reference with a join on the product/product_lang tables.
 public static function getAssociationsLight($id_lang, $id_product, Context $context = null)
    {
        if (!$context)
            $context = Context::getContext();

        $sql = 'SELECT p.`id_product`, p.`reference`, pl.`name`
                FROM `'._DB_PREFIX_.'my_associations`
                LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product`= `id_product_2`)
                '.Shop::addSqlAssociation('product', 'p').'
                LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
                    p.`id_product` = pl.`id_product`
                    AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
                )
                WHERE `id_product_1` = '.(int)$id_product;

        return Db::getInstance()->executeS($sql);
    }
2) 创建一个能够显示自动完成和列表的模板。 在这里,我们将通过保存的关联循环创建我们的自动完成列表,我们将使用一些隐藏字段来跟踪ID/名称和一个可见列表,因为我们将为每一行设置一个删除按钮

        <input type="hidden" name="inputMyAssociations" id="inputMyAssociations" value="{foreach from=$my_associations item=accessory}{$accessory.id_product}-{/foreach}" />
        <input type="hidden" name="nameMyAssociations" id="nameMyAssociations" value="{foreach from=$my_associations item=accessory}{$accessory.name|escape:'html':'UTF-8'}¤{/foreach}" />
        <div id="ajax_choose_product_association">
            <div class="input-group">
                <input type="text" id="product_autocomplete_input_association" name="product_autocomplete_input_association" />
                <span class="input-group-addon"><i class="icon-search"></i></span>
            </div>
        </div>

        <div id="divMyAssociations">
            {foreach from=$my_associations item=accessory}
                <div class="form-control-static">
                    <button type="button" class="btn btn-default delAssociation" name="{$accessory.id_product}">
                        <i class="icon-remove text-danger"></i>
                    </button>
                    {$accessory.name|escape:'html':'UTF-8'}{if !empty($accessory.reference)}{$accessory.reference}{/if}
                </div>
            {/foreach}
        </div>
    <input type="submit" name="submitMyAssociations" id="submitMyAssociations" value="Send"/>
    <input type="hidden" name="productId" id="productId" value="{$product_id|escape:'html'}"/>

我希望它能帮助您完成所有这些。

我认为要实现这些功能,renderForm()函数是不够的,因为您必须绑定一些javascript和一些自定义html

编写一个全功能模块的过程有点长,但以附件功能为出发点不会太难,你将始终有一个关于“如何做”的参考。 我同意这一点:

1) 首先创建您的

getContent()

功能,以便能够显示自定义模板和与您的模块关联的产品,因此我们将提供一些信息:

    public function getContent(){

    //post process part to save the associations
    if(Tools::isSubmit('saveMyAssociations'){
     ... //we will see it later
    }

    $my_associations = MyModule::getAssociationsLight($this->context->language->id,Tools::getValue('id_product')); //function that will retrieve the array of all the product associated on my module table.

     $this->context->smarty->assign(array(
                    'my_associations' => $my_associations,
                    'product_id' => (int)Tools::getValue('id_product')
                ));

    return $this->display(__FILE__, 'views/templates/admin/admintemplate.tpl'); //custome template to create the autocomplete

    }

//our little function to get the already saved list, for each product we will retrieve id, name and reference with a join on the product/product_lang tables.
 public static function getAssociationsLight($id_lang, $id_product, Context $context = null)
    {
        if (!$context)
            $context = Context::getContext();

        $sql = 'SELECT p.`id_product`, p.`reference`, pl.`name`
                FROM `'._DB_PREFIX_.'my_associations`
                LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product`= `id_product_2`)
                '.Shop::addSqlAssociation('product', 'p').'
                LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
                    p.`id_product` = pl.`id_product`
                    AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
                )
                WHERE `id_product_1` = '.(int)$id_product;

        return Db::getInstance()->executeS($sql);
    }
2) 创建一个能够显示自动完成和列表的模板。 在这里,我们将通过保存的关联循环创建我们的自动完成列表,我们将使用一些隐藏字段来跟踪ID/名称和一个可见列表,因为我们将为每一行设置一个删除按钮

        <input type="hidden" name="inputMyAssociations" id="inputMyAssociations" value="{foreach from=$my_associations item=accessory}{$accessory.id_product}-{/foreach}" />
        <input type="hidden" name="nameMyAssociations" id="nameMyAssociations" value="{foreach from=$my_associations item=accessory}{$accessory.name|escape:'html':'UTF-8'}¤{/foreach}" />
        <div id="ajax_choose_product_association">
            <div class="input-group">
                <input type="text" id="product_autocomplete_input_association" name="product_autocomplete_input_association" />
                <span class="input-group-addon"><i class="icon-search"></i></span>
            </div>
        </div>

        <div id="divMyAssociations">
            {foreach from=$my_associations item=accessory}
                <div class="form-control-static">
                    <button type="button" class="btn btn-default delAssociation" name="{$accessory.id_product}">
                        <i class="icon-remove text-danger"></i>
                    </button>
                    {$accessory.name|escape:'html':'UTF-8'}{if !empty($accessory.reference)}{$accessory.reference}{/if}
                </div>
            {/foreach}
        </div>
    <input type="submit" name="submitMyAssociations" id="submitMyAssociations" value="Send"/>
    <input type="hidden" name="productId" id="productId" value="{$product_id|escape:'html'}"/>

我希望它能帮助你完成这一切。

prestashop中有一个自动完成插件,你可以使用它来完成这一切。在js->jquery->plugins中,你必须将这个插件添加到你的模块中,使它工作。

在prestashop中有一个自动完成插件,你必须使用它。在js->jquery->plugins中,您必须将此插件添加到您的模块中,以使其正常工作。

对于延迟,我很抱歉,我一直在忙于另一个项目,而此项目遭受了很多延迟。非常感谢您的长时间回复。在接下来的几天里,我将尝试解决这个问题。你完美的解释所缺少的唯一一件事是,你在数据库中使用了“我的关联”表,但没有说你必须在“安装”模块时创建它(如果不存在)。很抱歉,延迟,我一直在忙于另一个项目,而这个项目耽搁了很多时间。非常感谢您的长时间回复。在接下来的几天里,我将尝试解决这个问题。你完美的解释所缺少的唯一一件事就是你在数据库中使用了“我的关联”表,而没有说你必须在“安装”模块时创建它(如果不存在)。但是如何创建呢?老师,教我,教我们感官,但是怎么教呢?老师,教我,老师