我试图通过在OpenCart 2.0.1.1版本中编写以下OCmod,使OpenCart中所需的模型字段成为可选字段。
<modification>
<name>Remove required model (OC 2.0+)</name>
<version>v1.0</version>
<link>http://www.example.com</link>
<author>John Doe</author>
<code>fv16343000</code>
<!-- Remove required "Model" from Product controller -->
<file path="admin/model/catalog/product.php">
<operation>
<search><![CDATA[if ((utf8_strlen($this->request->post['model']) < 1) || (utf8_strlen($this->request->post['model']) > 64)) {]]></search>
<add position="replace"><![CDATA[if (utf8_strlen($this->request->post['model']) > 64) {]]></add>
</operation>
</file>
</modification>
正如你所看到的,我的目的是替换条件,使其忽略最小长度。
我还修改了product_form.tpl(删除了表单组旁边的"必需"类):
<div class="form-group">
<label class="col-sm-2 control-label" for="input-model"><?php echo $entry_model; ?></label>
<div class="col-sm-10">
<input type="text" name="model" value="<?php echo $model; ?>" placeholder="<?php echo $entry_model; ?>" id="input-model" class="form-control" />
<?php if ($error_model) { ?>
<div class="text-danger"><?php echo $error_model; ?></div>
<?php } ?>
</div>
</div>
然后我尝试添加没有模型的产品,星号消失了,但错误仍然弹出(说我需要用最少1个字符到最多64个字符来完成字段)。
我认为我的OCmod可能不够好,所以我尝试直接编辑控制器product.php,如OCmod中所示。错误仍然存在,所以我从.tpl:中完全删除了条件和以下代码
<?php if ($error_model) { ?>
<div class="text-danger"><?php echo $error_model; ?></div>
<?php } ?>
删除引导程序类有效(没有显示星号,所以它有效),但由于某种原因,即使控制器中的条件消失,该字段仍然是必需的。
我做错了什么?如何使模型字段可选?
应用vQmod后,要覆盖的文件将随修改一起重写到vqcache文件夹中。因此,更改原始文件将不再生效。
通过删除vqcache文件夹中的相应文件来清除vQmod缓存。你也可以检查同一文件夹中的文件,以确保你最近的mod已经生效。
现在,继续做你想做的事情,取消长度限制。要做到这一点,只需将if语句替换为if (false){
,以确保它永远不会触发。
我使用OCmod解决了我的问题。删除模型的正确方法是:
<!-- Remove required class from register.tpl-->
<file path="admin/view/template/catalog/product_form.tpl">
<operation>
<search offset="1" index="2"><![CDATA[<div class="form-group required">]]></search>
<add position="replace"><![CDATA[<div class="form-group">]]></add>
</operation>
</file>
<!-- Comment the error line from the controller-->
<file path="admin/controller/catalog/product.php">
<operation>
<search><![CDATA[$this->error['model'] = $this->language->get('error_model');]]></search>
<add position="replace"><![CDATA[//$this->error['model'] = $this->language->get('error_model');]]></add>
</operation>
</file>
此代码也可以作为VQmod使用(如果在此之前使用了正确的标记)。
对于那些不了解OCmod是如何工作的,仍然想硬编码删除强制模型的人,你需要做的是:
- 打开admin/view/template/controlog/product_form.tpl
- 搜索与模型字段相关的
<div class="form-group required">
并删除required
类 - 打开admin/controller/controlog/product.php文件
- 查找此行:
$this->error['model'] = $this->language->get('error_model');
- 评论它
你完了。
我开发了一个免费的扩展,在添加产品时可以选择模型字段。我希望这就是你想要的——http://www.opencart.com/index.php?route=extension/extension/info&extension_id=225426.
Joel