Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 109 additions & 16 deletions inc/header-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,7 @@ function spacious_cart_icon() {
if ( ( get_theme_mod( 'spacious_cart_icon', 0 ) == 1 ) && class_exists( 'woocommerce' ) ) :
?>
<div class="cart-wrapper">
<div class="spacious-woocommerce-cart-views">

<!-- Show cart icon with total cart item -->
<?php $cart_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : WC()->cart->get_cart_url(); ?>

<a href="<?php echo esc_url( $cart_url ); ?>" class="wcmenucart-contents">
<i class="fa fa-shopping-cart"></i>
<span class="cart-value"><?php echo wp_kses_data( WC()->cart->get_cart_contents_count() ); ?></span>
</a>

<!-- Show total cart price -->
<div class="spacious-woocommerce-cart-wrap">
<div class="spacious-woocommerce-cart"><?php esc_html_e( 'Total', 'spacious' ); ?></div>
<div class="cart-total"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></div>
</div>
</div>
<?php spacious_cart_icon_views(); ?>

<!-- WooCommerce Cart Widget -->
<?php the_widget( 'WC_Widget_Cart', '' ); ?>
Expand All @@ -106,6 +91,114 @@ function spacious_cart_icon() {
endif;
}

// Registered with the default renderer, so a child theme overriding spacious_cart_icon() keeps its own markup.
add_filter( 'woocommerce_add_to_cart_fragments', 'spacious_cart_icon_fragment' );
add_action( 'wp_enqueue_scripts', 'spacious_cart_icon_scripts' );

endif;

if ( ! function_exists( 'spacious_cart_icon_views' ) ) :

/**
* Output the header cart icon, item count and total.
*
* Shared by the initial render and the WooCommerce cart fragment so both stay identical.
*/
function spacious_cart_icon_views() {
$cart_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : WC()->cart->get_cart_url();
?>
<div class="spacious-woocommerce-cart-views">

<!-- Show cart icon with total cart item -->

<a href="<?php echo esc_url( $cart_url ); ?>" class="wcmenucart-contents">
<i class="fa fa-shopping-cart"></i>
<span class="cart-value"><?php echo wp_kses_data( WC()->cart->get_cart_contents_count() ); ?></span>
</a>

<!-- Show total cart price -->
<div class="spacious-woocommerce-cart-wrap">
<div class="spacious-woocommerce-cart"><?php esc_html_e( 'Total', 'spacious' ); ?></div>
<div class="cart-total"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></div>
</div>
</div>
<?php
}

endif;

if ( ! function_exists( 'spacious_cart_icon_fragment' ) ) :

/**
* Keep the header cart icon in sync when the cart changes via AJAX.
*
* Leaves alone a fragment that a plugin or child theme already supplies for the same selector.
*
* @param array $fragments Cart fragments, keyed by the CSS selector they replace.
* @return array Cart fragments including the header cart icon.
*/
function spacious_cart_icon_fragment( $fragments ) {
$selector = 'div.spacious-woocommerce-cart-views';

if ( 1 !== (int) get_theme_mod( 'spacious_cart_icon', 0 ) || ! isset( WC()->cart ) || isset( $fragments[ $selector ] ) ) {
return $fragments;
}

ob_start();
spacious_cart_icon_views();
$fragments[ $selector ] = ob_get_clean();

return $fragments;
}

endif;

if ( ! function_exists( 'spacious_cart_icon_scripts' ) ) :

/**
* Load what keeps the header cart icon in sync on the Cart and Checkout pages.
*
* The WooCommerce cart widget, which normally loads cart fragments, hides itself on those pages,
* and the Cart and Checkout blocks change the cart without firing the events cart fragments listen for.
*/
function spacious_cart_icon_scripts() {
if ( 1 !== (int) get_theme_mod( 'spacious_cart_icon', 0 ) || ! class_exists( 'woocommerce' ) ) {
return;
}

wp_enqueue_script( 'wc-cart-fragments' );

// Printed only where the blocks load their cart store.
wp_add_inline_script(
'wc-blocks-data-store',
"( function () {
var cart = window.wp && wp.data && wp.data.select( 'wc/store/cart' );

if ( ! cart ) {
return;
}

function key() {
var data = cart.getCartData();

return data.itemsCount + '|' + data.totals.total_items;
}

var last = key();

wp.data.subscribe( function () {
var now = key();

if ( now !== last && window.jQuery ) {
jQuery( document.body ).trigger( 'wc_fragment_refresh' );
}

last = now;
} );
} )();"
);
}

endif;

/****************************************************************************************/
Expand Down
34 changes: 17 additions & 17 deletions languages/spacious.pot
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Spacious 1.9.12\n"
"Report-Msgid-Bugs-To: themegrill@gmail.com\n"
"POT-Creation-Date: 2026-09-18 11:47:31+00:00\n"
"POT-Creation-Date: 2026-09-28 06:56:32+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down Expand Up @@ -187,7 +187,7 @@ msgstr ""

#: functions.php:324 functions.php:329
#: inc/customizer/class-spacious-customizer-partials.php:30
#: inc/functions.php:662 inc/header-functions.php:166
#: inc/functions.php:662 inc/header-functions.php:259
#: inc/widgets/class-spacious-featured-single-page-widget.php:104
#: inc/widgets/class-spacious-service-widget.php:48
#: inc/widgets/class-spacious-service-widget.php:74
Expand Down Expand Up @@ -421,7 +421,7 @@ msgstr ""

#: inc/customizer/class-spacious-customizer-partials.php:49
#: inc/customizer/class-spacious-customizer-partials.php:56
#: inc/header-functions.php:314 inc/header-functions.php:320
#: inc/header-functions.php:407 inc/header-functions.php:413
msgid "You are here: "
msgstr ""

Expand Down Expand Up @@ -1447,59 +1447,59 @@ msgstr ""
msgid "Comments Off"
msgstr ""

#: inc/header-functions.php:96
#: inc/header-functions.php:121
msgid "Total"
msgstr ""

#: inc/header-functions.php:244
#: inc/header-functions.php:337
msgid "Author: %s"
msgstr ""

#: inc/header-functions.php:252
#: inc/header-functions.php:345
msgid "Day: %s"
msgstr ""

#: inc/header-functions.php:255
#: inc/header-functions.php:348
msgid "Month: %s"
msgstr ""

#: inc/header-functions.php:258
#: inc/header-functions.php:351
msgid "Year: %s"
msgstr ""

#: inc/header-functions.php:261
#: inc/header-functions.php:354
msgid "Asides"
msgstr ""

#: inc/header-functions.php:264
#: inc/header-functions.php:357
msgid "Images"
msgstr ""

#: inc/header-functions.php:267
#: inc/header-functions.php:360
msgid "Videos"
msgstr ""

#: inc/header-functions.php:270
#: inc/header-functions.php:363
msgid "Quotes"
msgstr ""

#: inc/header-functions.php:273
#: inc/header-functions.php:366
msgid "Links"
msgstr ""

#: inc/header-functions.php:279 sidebar-left.php:29 sidebar.php:32
#: inc/header-functions.php:372 sidebar-left.php:29 sidebar.php:32
msgid "Archives"
msgstr ""

#: inc/header-functions.php:283
#: inc/header-functions.php:376
msgid "Page NOT Found"
msgstr ""

#: inc/header-functions.php:285
#: inc/header-functions.php:378
msgid "Search Results"
msgstr ""

#: inc/header-functions.php:341
#: inc/header-functions.php:434
msgid "Menu"
msgstr ""

Expand Down