Zen Cart 2.1.0中EZ-Feeds插件配置更新观察者类实现问题求助
Zen Cart 2.1.0中EZ-Feeds插件配置更新观察者类实现问题求助
我最近在折腾Zen Cart 2.1.0上的EZ-Feeds插件,需求是把配置表中86个以EZ_FEEDS_...为前缀的配置项,同步到供应商专属的设置表ezfeeds_datafeed_settings里——这个表的作用是记住每个供应商对应的86项配置值。
现有方案的痛点
最开始我在admin/ezfeeds.php里写了一段硬编码的更新逻辑,能实现同步但有个问题:只有在表单验证无重定向、无修正更新的时候才会生效。代码是这样的:
if($cfgKey !== 'EZ_FEEDS_DEFAULT_TASK' && $cfgKey !== 'EZ_FEEDS_DEFAULT_FEED' && EZ_FEEDS_SAVE_DATAFEED_OPTION_SETTINGS === TEXT_YES || $cfgTitle === TITLE_SAVE_DATAFEED_OPTIONS) { $db->execute("update " . TABLE_EZ_FEEDS_DATAFEED_DEFAULTS . " set `" . $cfgTitle . "` = '" . zen_db_input($configuration_value) . "' where " . TITLE_CHOOSE_DATAFEED . " like '%" . EZ_FEEDS_DEFAULT_FEED . "%' "); $messageStack->add_session(TEXT_DATAFEED_DEFAULT . $cfgTitle . TEXT_EQUALS . zen_db_input($configuration_value), TEXT_SUCCESS); zen_record_admin_activity(TEXT_CONFIGURATION_CHANGED . $result->fields['configuration_key'] . ': ' . $configuration_value, 'warning'); }
后来我把这段代码封装成了函数ezfeeds_update_suppliers_table,在代码里多个地方调用它,虽然能正常工作,但总感觉这种到处插函数调用的方式太“凑活”了,完全是hack的路子,所以想整个观察者类,监听配置更新事件自动同步数据。
观察者类的尝试与问题
我按照Zen Cart的规范,写了一个观察者类,放在zc_plugins/EZ-Feeds/v1.0.8/admin/includes/classes/observers/auto.ezfeeds.php路径下,代码如下:
/** * Ezfeeds Observer to update a specific suppliers ezfeeds_datafeeds_default value when the equivalent configuration setting is changed. * Copyright 2006-2025 Jest Country Enterprises * Created by mikeel100 2025 Aug 23 Modified in v2.1.0 @ * IN DEVELOPMENT... */ class ezfeeds_update_supplier_settings_table_on_configuration_update extends base { /** * @param $callingClass object Reference to the class calling the event. * @param $eventID string The ID of the event. */ function __construct() { $this->attach($this, array('NOTIFY_ADMIN_CONFIG_UPDATE')); } /** * Update the ezfeeds_datafeeds_default table when a specific configuration setting is changed. * * @param $callingClass object Reference to the class calling the event. * @param $eventID string The ID of the event. */ function update(&$callingClass, $eventID) { global $db; // Check if the specific configuration key we want to monitor was updated. // Replace 'YOUR_CONFIG_KEY_TO_WATCH' with the actual key, e.g., 'EZFEEDS_FEED_URL'. if ($callingClass->cfgKey == 'EZ_FEEDS_AUDIBLE_TONE') { $newValue = $callingClass->cfgValue; // Log the update for debugging. zen_mail(STORE_NAME, STORE_OWNER_EMAIL_ADDRESS, 'EZFeeds config update', "Configuration key EZ_FEEDS_AUDIBLE_TONE was changed to: " . $newValue, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS); // Perform the database update on the ezfeeds_datafeeds_default table. // Replace 'ezfeeds_column_to_update' with the correct column name. $sql = "UPDATE " . DB_PREFIX . "ezfeeds_datafeeds_default SET 'Option - Audible Tone' = :newValue WHERE " . TITLE_CHOOSE_DATAFEED . " like '%" . EZ_FEEDS_DEFAULT_FEED . "%' "; // Add a WHERE clause if necessary to target a specific row. $sql_data = $db->bind_array('newValue', $newValue, 'string'); $db->Execute($sql, $sql_data); } } }
结果这个观察者类完全没生效,日志里报错关联到init_observers.php,但我没修改过这个文件。现在我有几个疑问想请教大家:
- 这个观察者类的结构和事件绑定有没有问题?
NOTIFY_ADMIN_CONFIG_UPDATE事件的监听方式对吗? - 数据库更新的SQL语句里,我用单引号包裹字段名
'Option - Audible Tone'是不是错了?是不是应该用反引号? - 目前我只监听了单个配置项
EZ_FEEDS_AUDIBLE_TONE,后续要扩展到86项,怎么高效判断配置项是否属于EZ_FEEDS_...前缀,同时排除EZ_FEEDS_DEFAULT_TASK和EZ_FEEDS_DEFAULT_FEED这两个例外? - 观察者类的放置路径
zc_plugins/EZ-Feeds/v1.0.8/admin/includes/classes/observers/auto.ezfeeds.php符合Zen Cart的插件规范吗?会不会是路径不对导致没被加载?
希望有做过Zen Cart观察者开发的朋友能给我指指路,谢谢!
内容来源于stack exchange
相关产品推荐
相关产品推荐

