Little problem with woocommerce optimization

Hi, I have added a function to remove woocommerce scripts where they are not needed. The problem is that if I add the woocommerce slider to the homepage as it is not viewed as a woocommerce page (is_woocommerce) it is not displayed correctly. My question is if there is a way to target pages where woocommerce-related builder elements are inserted so that scripts can be loaded there too. I paste the code used


/**

 * Callback function that returns true if the current page is a WooCommerce page or false if otherwise.

 *

 * @return boolean true for WC pages and false for non WC pages

 */

function is_wc_page() {

return class_exists( 'WooCommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() );

}


add_action( 'template_redirect', 'conditionally_remove_wc_assets' );

/**

 * Remove WC stuff on non WC pages.

 */

function conditionally_remove_wc_assets() {

// if this is a WC page, abort.

if ( is_wc_page() ) {

return;

}


// remove WC generator tag

remove_filter( 'get_the_generator_html', 'wc_generator_tag', 10, 2 );

remove_filter( 'get_the_generator_xhtml', 'wc_generator_tag', 10, 2 );


// unload WC scripts

remove_action( 'wp_enqueue_scripts', [ WC_Frontend_Scripts::class, 'load_scripts' ] );

remove_action( 'wp_print_scripts', [ WC_Frontend_Scripts::class, 'localize_printed_scripts' ], 5 );

remove_action( 'wp_print_footer_scripts', [ WC_Frontend_Scripts::class, 'localize_printed_scripts' ], 5 );


// remove "Show the gallery if JS is disabled"

remove_action( 'wp_head', 'wc_gallery_noscript' );


// remove WC body class

remove_filter( 'body_class', 'wc_body_class' );

}


add_filter( 'woocommerce_enqueue_styles', 'conditionally_woocommerce_enqueue_styles' );

/**

 * Unload WC stylesheets on non WC pages

 *

 * @param array $enqueue_styles

 */

function conditionally_woocommerce_enqueue_styles( $enqueue_styles ) {

return is_wc_page() ? $enqueue_styles : array();

}


add_action( 'wp_enqueue_scripts', 'conditionally_wp_enqueue_scripts' );

/**

 * Remove inline style on non WC pages

 */

function conditionally_wp_enqueue_scripts() {

if ( ! is_wc_page() ) {

wp_dequeue_style( 'woocommerce-inline' );

wp_dequeue_style( 'wc-blocks-vendors-style' );

wp_dequeue_style( 'wc-blocks-style' );

}

}


// add_action( 'init', 'remove_wc_custom_action' );

function remove_wc_custom_action(){

remove_action( 'wp_head', 'wc_gallery_noscript' );

}

Comments

  • Solved using another callback function that is

    function is_wc_product() {

    return class_exists( 'WooCommerce' ) && ( get_posts( array( 'post_type' => 'product') ) );

    }

    and then I adapted the code accordingly

Sign In or Register to comment.
This website uses cookies

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you’ve provided to them or that they’ve collected from your use of their services.

Cookies are small text files that can be used by websites to make a user's experience more efficient.

The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This means that cookies which are categorized as necessary, are processed based on GDPR Art. 6 (1) (f). All other cookies, meaning those from the categories preferences and marketing, are processed based on GDPR Art. 6 (1) (a) GDPR.

This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages.

You can at any time change or withdraw your consent from the Cookie Declaration on our website.

Learn more about who we are, how you can contact us and how we process personal data in our Privacy Policy.

Please state your consent ID and date when you contact us regarding your consent.