Skip to content

Commit b12b28a

Browse files
authored
Enqueue wc-cart-fragments on woocommerce pages (#2113)
* enqueue wc-cart-fragments on woocommerce pages * bump tested up version
1 parent bda638f commit b12b28a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tested up to: 6.2.2
55
Stable tag: 4.4.1
66
Version: 4.4.1
77
WC requires at least: 4.2
8-
WC tested up to: 7.7.0
8+
WC tested up to: 8.0.0
99
License: GPLv3 or later
1010
License URI: https://www.gnu.org/licenses/gpl-3.0.html
1111
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

inc/class-storefront.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct() {
3232
add_filter( 'wp_page_menu_args', array( $this, 'page_menu_args' ) );
3333
add_filter( 'navigation_markup_template', array( $this, 'navigation_markup_template' ) );
3434
add_action( 'enqueue_embed_scripts', array( $this, 'print_embed_styles' ) );
35+
add_filter( 'woocommerce_get_script_data', array( $this, 'limit_cart_sync_to_wc_pages' ), 10, 2 );
3536
}
3637

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

358+
wp_enqueue_script( 'wc-cart-fragments' );
359+
357360
wp_enqueue_script( 'storefront-navigation', get_template_directory_uri() . '/assets/js/navigation' . $suffix . '.js', array(), $storefront_version, true );
358361

359362
if ( has_nav_menu( 'handheld' ) ) {
@@ -374,6 +377,24 @@ public function scripts() {
374377
}
375378
}
376379

380+
/**
381+
* Limit Cart Sync functionality to specific WooCommerce pages
382+
* More details: https://developer.woocommerce.com/2023/06/16/best-practices-for-the-use-of-the-cart-fragments-api/
383+
*
384+
* @param string $script_data The script data.
385+
* @param string $handle The script handle.
386+
* @return string|null
387+
*/
388+
public function limit_cart_sync_to_wc_pages( $script_data, $handle ) {
389+
if ( 'wc-cart-fragments' === $handle ) {
390+
if ( is_woocommerce() || is_cart() || is_checkout() ) {
391+
return $script_data;
392+
}
393+
return null;
394+
}
395+
return $script_data;
396+
}
397+
377398
/**
378399
* Register Google fonts.
379400
*

0 commit comments

Comments
 (0)