【问题标题】:Woocommerce core extending if statementWoocommerce 核心扩展 if 语句
【发布时间】:2017-11-24 09:52:15
【问题描述】:

我在 class_wc_frontend_scripts.php 中添加了一条 if 语句,它是 woocommerce 插件的核心文件,因此不应自行更改。所以我需要将整个东西移到functions.php,以保持woocommerce可更新。

我想我必须以某种方式将我的 if 语句加载到操作钩子“wp_enqueue_scripts”中,并使其扩展现有的函数或类,但无法弄清楚如何做到这一点......

有什么想法吗?

public static function load_scripts() {
    global $post;
    // Load gallery scripts on product pages only if supported.
    // THIS ONE IS HERE BY DEFAULT AND STAYS
    if ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) {
        if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
            self::enqueue_script( 'zoom' );
        }
        if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
            self::enqueue_script( 'flexslider' );
        }
        if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
            self::enqueue_script( 'photoswipe-ui-default' );
            self::enqueue_style( 'photoswipe-default-skin' );
            add_action( 'wp_footer', 'woocommerce_photoswipe' );
        }
        self::enqueue_script( 'wc-single-product' );
    }

    // Load gallery scripts on archive pages only if supported.
    // >> THIS ONE I ADDED. IT NEEDS TO BE MOVED TO FUNCTIONS.PHP <<
    if ( is_archive() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) {
        if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
            self::enqueue_script( 'zoom' );
        }
        if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
            self::enqueue_script( 'flexslider' );
        }
        if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
            self::enqueue_script( 'photoswipe-ui-default' );
            self::enqueue_style( 'photoswipe-default-skin' );
            add_action( 'wp_footer', 'woocommerce_photoswipe' );
        }
        self::enqueue_script( 'wc-single-product' );
    }

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    我终于自己想出了如何做到这一点:

    add_action( 'wp_enqueue_scripts', 'gallery_scripts', 20 );
    
    
    function gallery_scripts() {
        if ( is_archive()) {
            if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { 
                wp_enqueue_script( 'zoom' );
            }
            if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
                wp_enqueue_script( 'flexslider' );
            }
            if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
                wp_enqueue_script( 'photoswipe-ui-default' );
                wp_enqueue_style( 'photoswipe-default-skin' );
                add_action( 'wp_footer', 'woocommerce_photoswipe' );
            }
            wp_enqueue_script( 'wc-single-product' );
        }
    
    }
    

    这会在存档页面上加载与产品页面相同的 woocommerce 图库功能。感谢 Kagg Design 给我带来了使用wp_enqueue_script()的想法

    【讨论】:

    • 是的,没错。我迟到了 27 秒。 )) 我在 27 秒后修改了我的答案...
    【解决方案2】:

    删除您添加到插件的if,并将以下代码添加到您主题的functions.php:

    add_action( 'wp_enqueue_scripts', 'gallery_scripts' );
    function gallery_scripts() {
        global $post;
    
        if ( ! did_action( 'before_woocommerce_init' ) ) {
            return;
        }
    
        if ( is_archive() ) {
    
            if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
                wp_enqueue_script( 'zoom' );
            }
            if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
                wp_enqueue_script( 'flexslider' );
            }
            if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
                wp_enqueue_script( 'photoswipe-ui-default' );
                wp_enqueue_script( 'photoswipe-default-skin' );
                add_action( 'wp_footer', 'woocommerce_photoswipe' );
            }
            wp_register_script( 'wc-single-product', '', array( 'jquery' ), WC_VERSION, true );
            wp_enqueue_script( 'wc-single-product' );
        }
    }
    

    此代码在wp_enqueue_scripts 事件中触发。代码检查 is_archive(),如果我们在存档页面等,则需要将脚本排入队列。

    【讨论】:

    • 嘿,非常感谢您的评论,但代码还不能工作:解析错误:syntax error, unexpected '}' in /www/htdocs/w0176f0c/waelderschaetze/wp-content/themes/Waelderschaetze/functions.php on line 370 如果我然后删除{,接下来我会收到此错误:Parse error: syntax error, unexpected 'else' (T_ELSE) in /www/htdocs/w0176f0c/waelderschaetze/wp-content/themes/Waelderschaetze/functions.php on line 370跨度>
    • 我也不知道这将如何解决我的问题。如果我的代码正确,它将阻止 woocommerce 库功能加载到其他页面然后存档等。但我实际上是在尝试激活存档页面上的库功能。默认情况下,它们仅适用于产品页面。我的两个if 块之间的唯一区别是is_product()is_archive()。对此有何建议?
    猜你喜欢
    • 2016-03-02
    • 2015-07-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多