【问题标题】:Override woocommerce class files覆盖 woocommerce 类文件
【发布时间】:2018-02-16 04:05:52
【问题描述】:

我希望在 woocommerce/includes/ 中名为 class-wc-frontend-scripts.php 的文件中对 WooCommerce 中的函数进行一些修改

我要修改的函数是:

private static function get_script_data( $handle ) {
    global $wp;

    switch ( $handle ) {
        case 'wc-single-product' :
            return array(
                'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
                'review_rating_required'    => get_option( 'woocommerce_review_rating_required' ),
                'flexslider'                => apply_filters( 'woocommerce_single_product_carousel_options', array(
                    'rtl'            => is_rtl(),
                    'animation'      => 'slide',
                    'smoothHeight'   => true,
                    'directionNav'   => false,
                    'controlNav'     => 'thumbnails',
                    'slideshow'      => false,
                    'animationSpeed' => 500,
                    'animationLoop'  => false, // Breaks photoswipe pagination if true.
                ) ),
                'zoom_enabled'       => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ),
                'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ),
                'photoswipe_options' => apply_filters( 'woocommerce_single_product_photoswipe_options', array(
                    'shareEl'               => false,
                    'closeOnScroll'         => false,
                    'history'               => false,
                    'hideAnimationDuration' => 0,
                    'showAnimationDuration' => 0
                ) ),
                'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ),
            );
        break;
    }
    return false;
}

对于我的产品滑块,我需要在单个产品页面上显示上一个和下一个箭头。所以我需要将 'directionNav' 更改为 true。

如何在不更改核心文件的情况下执行此操作?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您创建了子主题了吗?如果没有,那是你的第一步。这是来自 WordPress 法典的链接:

    https://codex.wordpress.org/Child_Themes

    然后,您无需编辑该核心文件,而是挂钩该函数,并将该代码写入您在子主题中创建的 functions.php 文件中。如果要向现有函数添加代码,则需要使用操作挂钩。如果要修改代码,请使用过滤器。看起来您正在尝试更改代码,所以最好使用过滤器。

    这是使用过滤器的外观:

    add_filter( 'get_script_data', 'change_nav_direction' );
    
     function change_nav_direction( $variable ) {
       //any other code you may need
    
                    'directionNav'   => true,
    
     //you must have a return
     return $variable;
    }
    

    这是 WooCommerce 文章的链接,可能会有所帮助:

    https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/

    【讨论】:

    • 感谢您的建议。我已经创建了子主题。但是在这里我想确认我必须返回具有更改值的相同数组或仅返回更改后的值
    • 嘿抱歉,我刚刚意识到我的最后一条评论根本没有帮助。所以看起来 WooCommerce 正在创建自己的过滤器,它与 woocommerce_single_product_carousel_options 挂钩,所以我认为您实际上想要挂钩到该功能。然后你应该只需要改变一个值。
    • 感谢您的帮助。我为 woocommerce_single_product_carousel_options 添加了过滤器。为我工作。
    猜你喜欢
    • 2017-05-06
    • 2013-02-08
    • 2018-04-18
    • 1970-01-01
    • 2014-08-25
    • 2018-06-03
    • 2013-02-26
    • 2014-11-19
    • 2022-10-14
    相关资源
    最近更新 更多