从其他字段检索信息到产品列表

时间:2014-08-29 11:20:55

标签: prestashop prestashop-1.6

我使用此免费module示例向产品添加新的视频网址字段。一切正常,我可以在product.tpl中看到新数据,但我无法获取数据product-list.tpl对我来说非常重要,我想为每个产品添加播放按钮。我发现为了这个目的,我必须使用hookActionProductListOverride函数,但没有运气。有人可以帮帮我吗?

public function hookDisplayAdminProductsExtra($params) {
    $id_product = Tools::getValue('id_product');
    $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
    if(!empty($sampleObj) && isset($sampleObj->id)){
        $this->context->smarty->assign(array(
            'belvg_textarea' => $sampleObj->textarea,
        ));
    }

    return $this->display(__FILE__, 'views/admin/sample.tpl');
}

public function hookActionProductUpdate($params) {
    $id_product = Tools::getValue('id_product');
    $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
    $sampleObj->textarea = Tools::getValue('belvg_sample');
    $sampleObj->id_product = $id_product;

    if(!empty($sampleObj) && isset($sampleObj->id)){
        $sampleObj->update();
    } else {
        $sampleObj->add();
    }
}

public function hookDisplayFooterProduct($params) {
    $id_product = Tools::getValue('id_product');
    $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
    if(!empty($sampleObj) && isset($sampleObj->id)){
        $this->context->smarty->assign(array(
            'belvg_textarea' => $sampleObj->textarea,
        ));
    }

    echo $sampleObj->textarea;
}

1 个答案:

答案 0 :(得分:1)

Wasn't easy to solve this and can be less ugly, but in my case working like a charm.

After lot of hours spent with the Belvg's module I've used this module, almost the same with same problem, but with language support and more flexibility: http://nemops.com/prestashop-products-new-tabs-fields

Needed to add multiple custom fields, so it's more detailed as the original questions requires:

-module(util).

priv_dir() ->
    case code:priv_dir(app_name) of
        {error, bad_name} ->
            {ok, Cwd} = file:get_cwd(),
            Cwd ++ "/" ++ "priv";
        Priv ->
            Priv
    end.

Calling the // adding more fields $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang ADD `custom_field1`, `custom_field2` TEXT NOT NULL'; ... // adding argument for the getCustomField() and multiply lines $this->context->smarty->assign(array( 'custom_field1' => $this->getCustomField('custom_field1',(int)Tools::getValue('id_product')), 'custom_field2' => $this->getCustomField('custom_field2',(int)Tools::getValue('id_product')), ... // expanding the actionProductUpdatea hook if(!Db::getInstance()->update('product_lang', array('custom_field1'=> pSQL(Tools::getValue('custom_field1_'.$lang['id_lang'])),'custom_field2'=> pSQL(Tools::getValue('custom_field2_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product )) ... // this is the missing part! // upgrading the getCustomField() function to accept multiple custom fields and registering global vars public function getCustomField($getKey,$id_product) { $result = Db::getInstance()->ExecuteS('SELECT '.$getKey.', id_lang FROM '._DB_PREFIX_.'product_lang WHERE id_product = ' . (int)$id_product); if(!$result) return array(); foreach ($result as $field) { $val=$field[$getKey]; $fields[$field['id_lang']] = $val; if(!empty($val)){ !Configuration::updateValue($getKey, $val); } } return $fields; } and $custom_field1 in every .tpl working fine like this:

$custom_field2

Hope someone also need this. Thank You!