Skip to content

Commit

Permalink
Enqueue wc-cart-fragments on woocommerce pages (#2113)
Browse files Browse the repository at this point in the history
* enqueue wc-cart-fragments on woocommerce pages

* bump tested up version
  • Loading branch information
gigitux authored Aug 7, 2023
1 parent bda638f commit b12b28a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tested up to: 6.2.2
Stable tag: 4.4.1
Version: 4.4.1
WC requires at least: 4.2
WC tested up to: 7.7.0
WC tested up to: 8.0.0
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Tags: e-commerce, two-columns, left-sidebar, right-sidebar, custom-background, custom-colors, custom-header, custom-menu, featured-images, full-width-template, threaded-comments, accessibility-ready, rtl-language-support, footer-widgets, sticky-post, theme-options, editor-style
Expand Down
21 changes: 21 additions & 0 deletions inc/class-storefront.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct() {
add_filter( 'wp_page_menu_args', array( $this, 'page_menu_args' ) );
add_filter( 'navigation_markup_template', array( $this, 'navigation_markup_template' ) );
add_action( 'enqueue_embed_scripts', array( $this, 'print_embed_styles' ) );
add_filter( 'woocommerce_get_script_data', array( $this, 'limit_cart_sync_to_wc_pages' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -354,6 +355,8 @@ public function scripts() {
*/
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

wp_enqueue_script( 'wc-cart-fragments' );

wp_enqueue_script( 'storefront-navigation', get_template_directory_uri() . '/assets/js/navigation' . $suffix . '.js', array(), $storefront_version, true );

if ( has_nav_menu( 'handheld' ) ) {
Expand All @@ -374,6 +377,24 @@ public function scripts() {
}
}

/**
* Limit Cart Sync functionality to specific WooCommerce pages
* More details: https://developer.woocommerce.com/2023/06/16/best-practices-for-the-use-of-the-cart-fragments-api/
*
* @param string $script_data The script data.
* @param string $handle The script handle.
* @return string|null
*/
public function limit_cart_sync_to_wc_pages( $script_data, $handle ) {
if ( 'wc-cart-fragments' === $handle ) {
if ( is_woocommerce() || is_cart() || is_checkout() ) {
return $script_data;
}
return null;
}
return $script_data;
}

/**
* Register Google fonts.
*
Expand Down

1 comment on commit b12b28a

@joldnl
Copy link

@joldnl joldnl commented on b12b28a Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now, the mini cart we're using will only be active on woocommerce pages. Which is weird, because the whole purpose of the minicart is to get insight of the cart on the whole website, not just woo pages...

Is there any way of enabling the mini-cart again on all pages? We have a very performant server, so performance is not an issue.

Please sign in to comment.