Skip to content

Commit

Permalink
Fix promotions filtering by tab (#44884)
Browse files Browse the repository at this point in the history
* Fixing filtering by tabs in `Promotions` component.
Using reference to WC_Admin_Marketplace_Promotions::TRANSIENT_NAME when outputting JS data for promotions, instead of transient string.
Better initialisation of wc global object in the inline script.

* Changelog.

* Using global var for marketplace.

* More tentative reference to global object.

* icon TS error
  • Loading branch information
andfinally committed Feb 22, 2024
1 parent 0a3c285 commit 24a216f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import Notice from '../notice/notice';

declare global {
interface Window {
wc: {
marketplace?: {
promotions: Promotion[];
};
wcMarketplace?: {
promotions?: Promotion[];
};
}
}
Expand All @@ -21,7 +19,7 @@ type Promotion = {
pages: Page[];
position: string;
content: { [ locale: string ]: string };
icon?: string | undefined;
icon?: string;
is_dismissible?: boolean;
menu_item_id?: string;
style?: string;
Expand All @@ -41,7 +39,7 @@ const Promotions: () => null | JSX.Element = () => {
if ( currentPage !== 'wc-admin' ) {
return null;
}
const promotions = window.wc?.marketplace?.promotions ?? [];
const promotions = window?.wcMarketplace?.promotions ?? [];
const currentDateUTC = Date.now();
const currentPath = decodeURIComponent( urlParams.get( 'path' ) || '' );
const currentTab = urlParams.get( 'tab' );
Expand All @@ -65,11 +63,12 @@ const Promotions: () => null | JSX.Element = () => {
)
? currentPath
: `/${ currentPath }`;
return (
page.page === currentPage &&

return page.page === currentPage &&
normalizedPath === normalizedCurrentPath &&
( page.tab ? page.tab === currentTab : true )
);
page.tab
? page.tab === currentTab
: ! currentTab;
}
);

Expand Down Expand Up @@ -104,7 +103,7 @@ const Promotions: () => null | JSX.Element = () => {
variant={
promotion.style ? promotion.style : 'info'
}
icon={ promotion?.icon }
icon={ promotion?.icon || '' }
isDismissible={ promotion.is_dismissible || false }
/>
);
Expand Down
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/44884-update-promotions-show
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Fix condition in Promotions component so marketplace promotions are properly filtered by page.
4 changes: 2 additions & 2 deletions plugins/woocommerce/includes/admin/class-wc-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,15 @@ public function admin_scripts() {
// Marketplace promotions.
if ( in_array( $screen_id, array( 'woocommerce_page_wc-admin' ), true ) ) {

$promotions = get_transient( 'woocommerce_marketplace_promotions' );
$promotions = get_transient( WC_Admin_Marketplace_Promotions::TRANSIENT_NAME );

if ( false === $promotions ) {
return;
}

wp_add_inline_script(
'wc-admin-app',
'window.wc = window.wc || {}; wc.marketplace = ' . wp_json_encode( array( 'promotions' => $promotions ) ),
'window.wcMarketplace = ' . wp_json_encode( array( 'promotions' => $promotions ) ),
'before'
);
}
Expand Down

0 comments on commit 24a216f

Please sign in to comment.