扩展 WooCommerce 管理类 [英] Extending a WooCommerce admin class

查看:22
本文介绍了扩展 WooCommerce 管理类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展一个与 Wordpress 管理界面相关的 WooCommerce 类.我目前正在尝试通过扩展它来更改类中的功能之一.我正在尝试:

I'm trying to extend a WooCommerce class that concerns the Wordpress admin interface. I'm currently trying to change one of the functions inside the class by extending it. I'm attempting this:

if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
  add_action('plugins_loaded', "overwrite_woocommerce");
}

function overwrite_woocommerce() {
  class Extend_WC_Admin_Permalink_Settings extends WC_Admin_Permalink_Settings{
    public function settings_save() {
        // Stuff that happens
    }
  }
}

但是在尝试了 plugins_loaded 和 admin_init 钩子之后,两者似乎都不起作用.直接在插件文件夹中编辑 WooCommerce 类即可实现我正在寻找的结果.

But after trying both the plugins_loaded and admin_init hooks, neither seem to work. Editing the WooCommerce class directly in the plugin folder achieves the outcome I'm looking for.

我正在尝试从函数中删除这些行:

I'm trying to remove these lines in particular from the function:

if ( '/%product_cat%/' === trailingslashit( $product_base ) ) {
    $product_base = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_base;
}

这个函数解析为

update_option( 'woocommerce_permalinks', $permalinks );

最终我可以使用 update_option 钩子,问题是 WooCommerce 强制/product/"如果/%product_cat%/"作为基础被设置为自定义 URL 库,这让我无法知道原始值是否为/%product_cat%/";开始,因为它是在钩子发射之前设置的.这就是为什么我首先要扩展这个类.

Ultimately I could use the update_option hooks, the problem being that WooCommerce forces "/product/" as a base if "/%product_cat%/" is set as a custom URL base, which leaves me with no way of knowing if the original value was "/%product_cat%/" to begin with as it's set before the hook fires. Hence why I want to extend the class in the first place.

推荐答案

我认为您无法做到这一点.当覆盖核心/模块文件很常见时,我不知道您是来自 Prestashop 还是 Joomla,但在 WP 中,您最好的办法是分析您尝试覆盖的部分,看看它是否有任何钩子可以使它像您一样工作想要.

I think that you won't be able to do that. I don't know if you are coming from Prestashop or Joomla when overwriting core/modules file is common but in WP your best shot is to analyze the part you are trying to overwrite and see if it has any hooks to make it work as you want.

如果这个答案还不够,请发布您尝试更改的部分,我们会尝试告诉您是否还有其他方法可以做到这一点

If this answer is not enough then post the part you are trying to change and we will try to tell you if there's another way of doing that

这篇关于扩展 WooCommerce 管理类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆