Woocommerce 核心扩展 if 语句

Woocommerce core extending if statement

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

我想我必须以某种方式将我的 if 语句加载到操作挂钩 'wp_enqueue_scripts' 中并使其扩展现有函数或 class,但无法弄清楚这到底是怎么回事完成...

有什么想法吗?

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' );
    }

删除您添加到插件的 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(),如果我们在存档页面等,则将需要的脚本排队。

我终于想出自己怎么做了:

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()