jolo
jolo

Reputation: 125

Woocommerce core extending if statement

I added an if statement to the class_wc_frontend_scripts.php, which is a core file of the woocommerce plugin and therefore should not be changed itself. So I need to move the whole thing to the functions.php, in order to keep woocommerce updatable.

I think I somehow have to load my if statement in to the action hook 'wp_enqueue_scripts' and make it extend the existing function or class, but couldn't figure out how exactly this can be done ...

Any ideas?

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

Upvotes: 0

Views: 375

Answers (2)

KAGG Design
KAGG Design

Reputation: 1945

Remove your if added to plugin and add the following code to functions.php of your theme:

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

This code fires at wp_enqueue_scripts event. Code checks is_archive(), and if we are on archive page etc., enqueues scripts needed.

Upvotes: 0

jolo
jolo

Reputation: 125

I finally figured out how to do this by myself:

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

}

This loads the woocommerce gallery features on archive page same as on product page. thanks to Kagg Design who actually brought me the idea of using wp_enqueue_script()

Upvotes: 1

Related Questions