diff --git a/packages/calypso-products/src/has-marketplace-product.ts b/packages/calypso-products/src/has-marketplace-product.ts index c6fe83843dbdb9..ddc10280957891 100644 --- a/packages/calypso-products/src/has-marketplace-product.ts +++ b/packages/calypso-products/src/has-marketplace-product.ts @@ -1,20 +1,20 @@ -const cleanSlug = ( slug: string ) => - slug && slug.replace( /_/g, '-' ).split( /-(monthly|yearly|2y)/ )[ 0 ]; - /** - * Returns true if a list of products contains a marketplace product with the specified product slug. + * Returns true if a list of products includes a product with a matching product or store product slug. * * @param {object} productsList - List of products - * @param {string} productSlug - internal product slug, eg 'jetpack_premium' + * @param {string} searchSlug - Either a product slug e.g. woocommerce-product-csv-import-suite or store product slug, e.g wc_product_csv_import_suite_yearly * @returns {boolean} */ export const hasMarketplaceProduct = ( - productsList: Record< string, { product_type: string; product_slug: string } >, - productSlug: string + productsList: Record< string, { product_type: string; billing_product_slug: string } >, + searchSlug: string ): boolean => + // storeProductSlug is from the legacy store_products system, billing_product_slug is from + // the non-legacy billing system and for marketplace plugins will match the slug of the plugin + // by convention. Object.entries( productsList ).some( - ( [ subscriptionSlug, { product_type } ] ) => - cleanSlug( productSlug ) === cleanSlug( subscriptionSlug ) && + ( [ storeProductSlug, { product_type, billing_product_slug } ] ) => + ( searchSlug === storeProductSlug || searchSlug === billing_product_slug ) && // additional type check needed when called from JS context typeof product_type === 'string' && product_type.startsWith( 'marketplace' ) diff --git a/packages/calypso-products/test/has-marketplace-product.js b/packages/calypso-products/test/has-marketplace-product.js index 86af83a2aead39..6161b50b31633d 100644 --- a/packages/calypso-products/test/has-marketplace-product.js +++ b/packages/calypso-products/test/has-marketplace-product.js @@ -5,14 +5,42 @@ describe( 'hasMarketplaceProduct', () => { empty_product: {}, product_with_wrong_types: { product_type: 1, slug: 1 }, jetpack: { product_type: 'jetpack' }, - woocommerce_bookings_monthly: { product_type: 'marketplace_plugin' }, - woocommerce_bookings_yearly: { product_type: 'marketplace_plugin' }, - woocommerce_bookings_2y: { product_type: 'marketplace_plugin' }, - 'woocommerce-subscriptions-monthly': { product_type: 'marketplace_plugin' }, - 'woocommerce-subscriptions-yearly': { product_type: 'marketplace_plugin' }, - 'woocommerce-subscriptions-2y': { product_type: 'marketplace_plugin' }, - 'woocommerce-test-plugin': { product_type: 'plugin' }, - 'woocommerce-test-plugin-advanced': { product_type: 'marketplace_plugin' }, + woocommerce_bookings_monthly: { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-bookings', + }, + woocommerce_bookings_yearly: { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-bookings', + }, + woocommerce_bookings_2y: { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-bookings', + }, + 'woocommerce-subscriptions-monthly': { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-subscriptions', + }, + 'woocommerce-subscriptions-yearly': { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-subscriptions', + }, + 'woocommerce-subscriptions-2y': { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-subscriptions', + }, + 'woocommerce-test-plugin': { + product_type: 'plugin', + billing_product_slug: 'woocommerce-test-plugin', + }, + 'woocommerce-test-plugin-advanced': { + product_type: 'marketplace_plugin', + billing_product_slug: 'woocommerce-test-plugin-advanced', + }, + wc_store_products_usually_underscored: { + product_type: 'marketplace_plugin', + billing_product_slug: 'wc-billing-product-slug-can-differ', + }, }; test( "should return false when the product isn't in the products list", () => { @@ -35,11 +63,13 @@ describe( 'hasMarketplaceProduct', () => { expect( hasMarketplaceProduct( productsList, 'woocommerce-test-plugin' ) ).toBe( false ); } ); - describe( 'product is a marketplace product', () => { - test( 'should return true when the product slug contains underscores', () => { - expect( hasMarketplaceProduct( productsList, 'woocommerce_bookings' ) ).toBe( true ); - } ); + test( "should return true when the billing_product_slug matches even when key doesn't match", () => { + expect( hasMarketplaceProduct( productsList, 'wc-billing-product-slug-can-differ' ) ).toBe( + true + ); + } ); + describe( 'product is a marketplace product', () => { test( 'should return true when the product slug contains dashes', () => { expect( hasMarketplaceProduct( productsList, 'woocommerce-subscriptions' ) ).toBe( true ); } );