扩展 Prestashop 1.7 的正确方法

Correct way to extend Prestashop 1.7

在 PS 1.7 文档中,我们多次读到不推荐使用 PS 覆盖系统,不允许在 PS 市场上发布模块。

我们必须只使用钩子,并扩展现有的 class,我明白为什么。

但是如何在我们的自定义主题中使用扩展 class 而不是核心?

举个例子:

我想为类别添加自定义字段。

在一个模块中,我扩展了 CategoryCore class :

class Category extend CategoryCore{

     private $bottom_description
     ...
}

然后要在类别的管理页面中添加字段,我可以使用一些挂钩,例如 displayBackOfficeCategory 和 actionBeforeAddCategory。

但我不确定前面的内容:新变量必须可以在某些主题模板文件中访问。

在我的自定义主题中,在 category.tpl 模板中,$category->bottom_description 未定义。

通过覆盖 CategoryController 来解决这个问题很容易,但是如何仅使用 hook 来解​​决这个问题?

我发现的唯一方法是使用 actionFrontControllerSetMedia 挂钩,如下所示:

function HookActionFrontControllerSetMedia(){
      // get my custom Category object base on url
      this->context->smarty->assign(["category_bottom_description"=>$category->buttom_description]);
   }

这看起来很棘手,我的新字段在其他上下文中仍然无法访问。

那么在我的定制中获得此 属性 的正确方法是什么?

理想情况下,每次我们找到一个类别对象时,新的 属性 应该可用,就像这样:$category->bottom_description。

不要忘记在类别 class 定义一个新字段,因为在前面你没有 class 实例,而是在 ObjectPresenter [=20= 中转换的数组].所以按照这个方法。扩展您的 Category class 并添加所有必要的定义

class Category extends CategoryCore
{
    public $bottom_description;

    public function __construct($idCategory = null, $idLang = null, $idShop = null)
    {
        Category::$definition['fields']['bottom_description'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml');
        $this->bottom_description = 'bottom_description'; // just to add demo data
        parent::__construct($idCategory, $idLang, $idShop);
    }
}

然后字段 bottom_description 将在 category.tpl 中可用,但类似于数组 {$category.bottom_description}。希望对你有帮助。