【问题标题】:Want to overwrite functions written in woocommerce-functions.php file想要覆盖写在 woocommerce-functions.php 文件中的函数
【发布时间】:2014-02-19 06:19:25
【问题描述】:

我想修改/覆盖写在 woocommerce-functions.php 文件中的函数,但我不想修改 woocommerce-functions.php 文件。那就是我想在插件或我的主题中实现这一点。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    可以覆盖 woocommerce 功能,我最近这样做了,并将我所有的 woocommerce 扩展功能添加到我的主题的 functions.php 文件中,以便 woocommerce 插件文件保持不变并且可以安全更新。

    此页面提供了一个示例,说明如何删除他们的操作并将其替换为您自己的 - http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp

    此页面提供了一个在不删除其功能的情况下扩展其功能以及使用子主题的示例 - http://uploadwp.com/customizing-the-woocommerce-checkout-page/

    希望这会有所帮助:)

    【讨论】:

    • 这似乎对我有用,但在我的情况下,我必须将挂钩名称更改为 remove_action('template_redirect', 'woocommerce_save_password'); add_action('template_redirect', 'theme_woocommerce_save_password');
    • uploadwp 链接目前已失效。仅供参考。
    【解决方案2】:

    如果您有子主题,您可以将相关文件复制到您的主题并重写副本。该副本将优先使用 WooCommerce 版本。

    【讨论】:

      【解决方案3】:

      WooCommerce 提供了一个模板系统。可以覆盖 woocommerce 功能。在不修改核心文件的情况下自定义 WooCommerce 的好方法是使用钩子 -

      如果您使用挂钩来添加或操作代码,则可以将自定义代码添加到主题 functions.php 文件中。

      • 使用动作挂钩 -

      要执行您自己的代码,您可以使用动作挂钩 do_action('action_name'); 进行挂钩。

      请参阅下面的代码放置位置的一个很好的示例:

      add_action('action_name', 'your_function_name');
      
      function your_function_name() 
      {
      // Your code
      }
      
      • 使用过滤器挂钩-

      过滤器钩子在代码中使用 apply_filter(‘filter_name’, $variable);

      要操作传递的变量,您可以执行以下操作:

      add_filter('filter_name', 'your_function_name');
      
      function your_function_name( $variable )
       {
      // Your code
      return $variable;
      }
      

      在这里您可以获得 WooCommerce 操作和过滤器挂钩 - https://docs.woothemes.com/wc-apidocs/hook-docs.html

      【讨论】:

        【解决方案4】:

        我需要为移动设备上的视频添加“播放”按钮(默认情况下,此按钮仅显示在桌面上)。

        我需要重写wp-content/themes/gon/framework/theme_functions.php中的函数:

        function ts_template_single_product_video_button(){
            if( wp_is_mobile() ){
                return;
            }
            global $product;
            $video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
            if( !empty($video_url) ){
                $ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
                echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
            }
        }
        

        我找到了this instruction,上面写着If you use a hook to add or manipulate code, you can add your custom code to your theme’s functions.php file.

        我已经有了wp-content/themes/gon-child/functions.php,(即原来的gon主题已经复制到gon-child),所以我做的是:

        // Enable tour video on mobile devices
        remove_action('ts_before_product_image', 'ts_template_single_product_video_button', 1);
        add_action('ts_before_product_image', 'ts_template_single_product_video_button_w_mobile', 1);
        function ts_template_single_product_video_button_w_mobile(){
            global $product;
            $video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
            if( !empty($video_url) ){
                $ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
                echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
            }
        }
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-16
          • 1970-01-01
          • 2015-12-09
          • 2014-08-25
          • 1970-01-01
          相关资源
          最近更新 更多