DeividasJJ
DeividasJJ

Reputation: 125

Retrieve info from additional field to product list

I'm using this free module sample to add new video url field to the product. Everything works fine, I can see new data in product.tpl, but I cant get data to product-list.tpl what is very important to me, I want to add play button to each product. I found that for this purpose I have to use hookActionProductListOverride function but no luck with that. Anyone can help me?

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;
}

Upvotes: 1

Views: 1559

Answers (1)

eapo
eapo

Reputation: 1081

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:

    // 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;
}

Calling the $custom_field1 and $custom_field2 in every .tpl working fine like this:

    {if $custom_field1}
        {if isset($custom_field1) && $custom_field1}
            <div>{$custom_field1}</div>
    {/if}
    {if $custom_field2}
        {if isset($custom_field2) && $custom_field2}
            <div>{$custom_field2}</div>
    {/if}

Hope someone also need this. Thank You!

Upvotes: 1

Related Questions