2

如何在 PRESTASHOP 1.5 上以编程方式添加新产品字段?

我在 SQL 上创建了这些字段,但我不知道如何保存?

这些字段是 3 bool,我想将其保存在单选按钮选项中。

4

3 回答 3

5

您应该查看对应于 Product 类和 ObjectModel 类的 Product.php 和 ObjectModel.php。

如果要在 Product 中添加字段,则必须在类中添加属性,并通过添加您添加的属性的定义来更新类中的 $definition 变量。

关于单选按钮,它是控制器的一部分,看看 AdminProductsController.php

希望能帮助到你,

溴,

于 2012-08-24T08:22:20.563 回答
0

Mayby 你会使用模块添加新字段吗?(这仅用于示例)

如果您在数据库中创建了字段(在我的示例中,我使用表“product_lang”)。

当你有skieleton模块时:

在模型/中创建“ProductField.php”

<?php

class ProductField extends ObjectModel
{
/** @var string Name */
public $id_product_field;

/** @var integer */
public $id_product;

/** @var string */
public $new_field;

/**
 * @see ObjectModel::$definition
 */
public static $definition = array(
    'table' => 'product_lang',
    'primary' => 'id_product',
    'multilang' => false,
    'fields' => array(
        'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => TRUE),
        'new_field' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
    ),
);

public function __construct($ID_PRODUCT) {
    $result = Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'product_lang WHERE id_product = '. (int) $ID_PRODUCT );
    $this->id_product = $ID_PRODUCT;
    $this->new_field = $result['new_field'];
}

public function update() {
    Db::getInstance()->update( 'product_lang', array( 'new_field' => $this->new_field ),  'id_product = ' . $this->id_product );
}

}
?>

请记住在主文件中包含模型。

在模块添加方法的主文件中:

public function hookDisplayAdminProductsExtra($params) {
$Fields = new ProductField( Tools::getValue('id_product') );
if( !empty( $Fields ) && isset( $Fields->id_product ) ) {
    $this->context->smarty->assign(array(
        'new_field' => $Fields->new_field,
    ) );
}
return $this->display(__FILE__, 'views/admin/admin.tpl');
}

创建视图/admin/admin.tpl

<!-- New Field MODULE -->
<input type="hidden" name="submitted_tabs[]" value="productfields" />
<h4>{l s='Additional Fields' mod='productfields'}</h4>
<div class="separation"></div>
<fieldset style="border:none;">
    <table cellspacing="0" cellpadding="5" border="0">
        <tbody>
        <tr>
            <td class="col-left">
                <label>{l s='New field' mod='productfields'}<br></label>
                <p class="product_description">{l s='description of new field' mod='productfields'}</p>
            </td>
            <td style="padding-bottom:5px;">
                <input type="text" name="new_field" value="{if isset($new_field)}{$new_field}{/if}" />
            </td>
        <tr>
        </tbody>
    </table>
</fieldset>
<div class="separation"></div>
<!-- END New Field MODULE -->

我希望我没有忘记任何事情。只适合您的需求。

于 2013-10-16T13:21:25.637 回答
0

看看这里:https ://gist.github.com/kpodemski/21a37617b6b488590dc1

这是 PrestaShop 1.5.4 的示例,但如果您下载 1.5.6 并将 informations.tpl 更改为较新的版本,它应该可以正常工作。

于 2013-10-18T15:19:45.130 回答