Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
54 changes: 54 additions & 0 deletions includes/plugins/woocommerce/my-account/class-my-account-ui-v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function init() {
\add_action( 'woocommerce_after_save_address_validation', [ __CLASS__, 'handle_delete_address_submission' ], 10, 4 );
\add_filter( 'woocommerce_address_to_edit', [ __CLASS__, 'reorder_address_fields' ], PHP_INT_MAX, 2 );
\add_action( 'woocommerce_account_content', [ __CLASS__, 'render_content_around_shortcode' ], 0 );
\add_filter( 'woocommerce_account_menu_items', [ __CLASS__, 'filter_subscriptions_menu_label' ], 20 );
}

/**
Expand Down Expand Up @@ -913,5 +914,58 @@ function() use ( $after ) {
);
}
}

/**
* Get non-trashed subscriptions for the current user.
*/
public static function get_non_trashed_users_subscriptions() {
// Check if WooCommerce Subscriptions is available.
if ( ! function_exists( 'wcs_get_users_subscriptions' ) ) {
return [];
}

$all_subscriptions = \wcs_get_users_subscriptions();

// Filter out trashed subscriptions.
return array_filter(
$all_subscriptions,
function( $subscription ) {
return ! $subscription->has_status( 'trash' );
}
);
}

/**
* Update 'Subcriptions' menu item based on trashed subscriptions.
*
* @param array $menu_items My Account menu items.
* @return array Modified menu items.
*/
public static function filter_subscriptions_menu_label( $menu_items ) {
// Only modify if subscriptions menu item exists.
if ( ! isset( $menu_items['subscriptions'] ) ) {
return $menu_items;
}

// Get only non-trashed subscriptions.
$active_subscriptions = self::get_non_trashed_users_subscriptions();
$active_count = count( $active_subscriptions );

// If there are no non-trashed subscriptions, remove the menu item.
if ( 0 === $active_count ) {
unset( $menu_items['subscriptions'] );
return $menu_items;
}

// If there is only one non-trashed subscription, use "My Subscription".
if ( 1 === $active_count && apply_filters( 'wcs_my_account_redirect_to_single_subscription', true ) ) {
$menu_items['subscriptions'] = __( 'My Subscription', 'newspack-plugin' );
} else {
// If there are more than one non-trashed subscription, use "Subscriptions".
$menu_items['subscriptions'] = __( 'Subscriptions', 'newspack-plugin' );
}

return $menu_items;
}
}
My_Account_UI_V1::init();
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

// Filter out subscriptions with 'trash' status.
if ( ! empty( $subscriptions ) ) {
$subscriptions = array_filter(
$subscriptions,
function( $subscription ) {
return 'trash' !== $subscription->get_status();
}
);
}
?>
<div class="woocommerce_account_subscriptions">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@
</a>

<ul>
<?php
// Check if viewing a single subscription page.
$is_viewing_single_subscription = false;
if ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'view-subscription' ) ) {
$is_viewing_single_subscription = true;
}
?>
<?php foreach ( wc_get_account_menu_items() as $endpoint => $label ) : ?>
<?php
// Highlight subscriptions menu item if viewing a single subscription.
$is_current_item = wc_is_current_account_menu_item( $endpoint ) || ( $is_viewing_single_subscription && 'subscriptions' === $endpoint );
?>
<li class="<?php echo esc_attr( wc_get_account_menu_item_classes( $endpoint ) ); ?>">
<a href="<?php echo esc_url( wc_get_account_endpoint_url( $endpoint ) ); ?>" <?php echo wc_is_current_account_menu_item( $endpoint ) ? 'aria-current="page"' : ''; ?> class="newspack-ui__button newspack-ui__button--small <?php echo wc_is_current_account_menu_item( $endpoint ) ? 'newspack-ui__button--accent' : 'newspack-ui__button--ghost'; ?>">
<a href="<?php echo esc_url( wc_get_account_endpoint_url( $endpoint ) ); ?>" <?php echo $is_current_item ? 'aria-current="page"' : ''; ?> class="newspack-ui__button newspack-ui__button--small <?php echo $is_current_item ? 'newspack-ui__button--accent' : 'newspack-ui__button--ghost'; ?>">
<?php echo esc_html( $label ); ?>
</a>
</li>
Expand Down
16 changes: 16 additions & 0 deletions src/newspack-ui/scss/elements/woocommerce/_overrides.scss
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@
border: none !important;
}
}

&.woocommerce-account.newspack-my-account {
.woocommerce-message {
.woocommerce-Button.button {
background: transparent;
border-radius: 0;
color: inherit;
float: none;
font-size: inherit;
font-weight: inherit;
min-height: auto;
padding: 0;
text-decoration: underline;
}
}
}
}

/** See #3292. */
Expand Down