Appearance
shopware/frontends - helpers ​
Welcome to @shopware-pwa/helpers-next
package.
For getting started documentation visit https://frontends.shopware.com/
Documentation specific for this package: helpers
Reusable classes ​
The helpersCssClasses
variable, defined in the cms/layoutClasses.ts
helper file, comprises an array of class names utilized within the CMS.
To enhance type support, a union type HelpersCssClasses
is defined, which encompasses all class names present in the helpersCssClasses
array.
ts
const visibilityMap: Record<CmsVisibility, HelpersCssClasses> = {
mobile: "max-md:hidden",
tablet: "md:max-lg:hidden",
desktop: "lg:hidden",
};
These classes can be integrated into a custom template, thereby ensuring consistency across different packages. For example as a safelist
classes in unocss configuration file
ts
import { helpersCssClasses } from "@shopware-pwa/helpers-next";
export default defineConfig({
safelist: helpersCssClasses,
});
Changelog ​
Full changelog for stable version is available here
Latest changes: 1.0.0 ​
Major Changes ​
#871
1566f7a
Thanks @patzick! - Read more about new major release: https://github.com/shopware/frontends/discussions/965#1056
c729e70
Thanks @patzick! - Removed deprecations from the package:getProductThumbnailUrl
is removed. UsegetSmallestThumbnailUrl
instead.- internal method
_debounce
is removed. - internal method
_parseUrlQuery
is removed.
Minor Changes ​
#857
864616f
Thanks @mdanilowicz! - Add isMaintenanceMode to check if backend is available#508
a92941e
Thanks @mdanilowicz! - AddaccessGranted
property for the getMedia helper#477
487d991
Thanks @mdanilowicz! - Remove "@shopware-pwa/types" dependency and replace it with generic types#582
97b5949
Thanks @mdanilowicz! - Export css classes as a separate file
Patch Changes ​
- #524
6b54268
Thanks @BrocksiNet! - addedbuildUrlPrefix
, which perviously was internal helper of the@shopware/cms-base
package
API ​
getTranslatedProperty
​
Get translated property from the given object.
ts
export function getTranslatedProperty<T>(
element: T | undefined | null | never,
property: keyof T,
): string
downloadFile
​
Download file
ts
export function downloadFile<T>(file: T, name: string)
getBiggestThumbnailUrl
​
Returns the biggest thumbnail url from the media object
ts
export function getBiggestThumbnailUrl<
T extends {
thumbnails?: Array<{
width: number;
url: string;
}>;
},
>(media?: T): string | undefined
getSmallestThumbnailUrl
​
Returns the smallest thumbnail url from the media object
ts
export function getSmallestThumbnailUrl<
T extends {
thumbnails?: Array<{
width: number;
url: string;
}>;
},
>(media?: T): string | undefined
getMedia
​
Prepare media object
ts
export function getMedia<
T extends {
downloads?: Array<{
id: string;
accessGranted: boolean;
media: {
fileName: string;
fileExtension: string;
};
}>;
},
>(lineItem: T)
getSrcSetForMedia
​
Returns the srcset attribute for the image, for available breakpoints
ts
export function getSrcSetForMedia<
T extends {
thumbnails?: Array<{
width: number;
url: string;
}>;
},
>(media?: T): string
relativeUrlSlash
​
Add/remove slash from the relative path
ts
export function relativeUrlSlash(relativeUrl: string, slash = true)
urlIsAbsolute
​
ts
export function urlIsAbsolute(url: string)
canUseQuoteActions
​
ts
export function canUseQuoteActions<
T extends {
stateMachineState?: {
technicalName: string;
};
},
>(quote: T)
getCmsLayoutConfiguration
​
Get layout configuration for CMS content
ts
export function getCmsLayoutConfiguration<
T extends CmsBlock | CmsSection | CmsSlot,
>(content: T): LayoutConfiguration
expand LayoutConfiguration
ts
export type LayoutConfiguration = {
layoutStyles: {
backgroundColor?: string | null;
backgroundImage?: string | null;
backgroundSize?: string | null;
sizingMode?: string | null;
marginBottom?: string | null | undefined;
marginLeft?: string | null | undefined;
marginRight?: string | null | undefined;
marginTop?: string | null | undefined;
};
cssClasses: {
[cssClass: string]: boolean;
} | null;
};
LayoutConfiguration
​
ts
export type LayoutConfiguration = {
layoutStyles: {
backgroundColor?: string | null;
backgroundImage?: string | null;
backgroundSize?: string | null;
sizingMode?: string | null;
marginBottom?: string | null | undefined;
marginLeft?: string | null | undefined;
marginRight?: string | null | undefined;
marginTop?: string | null | undefined;
};
cssClasses: {
[cssClass: string]: boolean;
} | null;
};
getCmsEntityObject
​
Returns the main page object depending of the type of the CMS page.
ts
export function getCmsEntityObject(
response: CmsPageResponse,
): Product | Category | LandingPage
isProduct
​
Predicate function to check if the entity is a product.
ts
export function isProduct<T extends { apiAlias: string }>(
entity: T | Product,
): entity is Product
isCategory
​
ts
export function isCategory<T extends { apiAlias: string }>(
entity: T | Category,
): entity is Category
isLandingPage
​
ts
export function isLandingPage<T extends { apiAlias: string }>(
entity: T | LandingPage,
): entity is LandingPage
getProductFreeShipping
​
Get product free shipping property
ts
export function getProductFreeShipping<
T extends {
shippingFree: boolean;
},
>(product?: T): boolean
getProductName
​
ts
export function getProductName<
T extends {
name: string;
},
>({ product }: { product?: T } = {}): string | null
getMainImageUrl
​
gets the cover image
ts
export function getMainImageUrl<
T extends
| {
cover?: {
media?: {
url: string;
};
};
}
| {
media?: Array<{
media?: {
url?: string;
};
}>;
}
| {
cover?: {
url: string;
};
},
>(object: T): string
getProductReviews
​
Format product reviews to ui-interfaces
ts
export function getProductReviews<
T extends {
id: string;
productReviews?: Array<{
id: string;
externalUser?: string;
customerId?: string;
createdAt: string;
content: string;
points?: number;
}>;
},
>({ product }: { product?: T } = {}): UiProductReview[]
getProductUrl
​
Get product url. The priority is SEO url and then technical url.
ts
export function getProductUrl<
T extends {
id: string;
seoUrls?: Array<{
seoPathInfo?: string;
}>;
},
>(product?: T): string
getProductRatingAverage
​
Get product rating average property
ts
export function getProductRatingAverage<T extends { ratingAverage: number }>(
product: T,
): number | null
getProductTierPrices
​
Get the prices depending on quantity added to cart. Tier prices can be set in Advanced pricing
tab in Product view
(admin panel)
ts
export function getProductTierPrices<
T extends {
calculatedPrices?: Array<{
unitPrice: number;
quantity: number;
}>;
},
>(product?: T): TierPrice[]
getProductCalculatedListingPrice
​
Get the calculated list price
ts
export function getProductCalculatedListingPrice<
T extends {
calculatedPrice?: CalculatedPrice;
calculatedPrices?: CalculatedPrice[];
},
>(product?: T): number | undefined
getProductFromPrice
​
ts
export function getProductFromPrice<
T extends {
calculatedPrice?: CalculatedPrice;
calculatedPrices?: CalculatedPrice[];
},
>(product: T): number | undefined
getProductRoute
​
Get product route information for Vue router.
Returns product URL and route informations for resolving SEO url. Use it with combination of <RouterLink>
or <NuxtLink>
in Vue.js or Nuxt.js projects.
ts
export function getProductRoute<
T extends {
id: string;
seoUrls?: Array<{
seoPathInfo?: string;
}>;
},
>(product?: T)
getCategoryImageUrl
​
gets the cover image
ts
export function getCategoryImageUrl<
T extends {
media?: { url: string };
type: string;
},
>(category: T): string
getCategoryBreadcrumbs
​
Gather breadcrumbs from category
ts
export function getCategoryBreadcrumbs<
T extends {
translated?: {
breadcrumb?: string[];
};
breadcrumb?: string[];
},
>(
category: T,
options?: {
/**
* Start at specific index if your navigation
* contains root names which should not be visible.
*/
startIndex?: number;
},
)
getCategoryUrl
​
Get URL for category. Some link
ts
export function getCategoryUrl<
T extends {
type: string;
externalLink?: string;
seoUrls?: { seoPathInfo: string }[];
internalLink?: string;
id: string;
linkType?: string;
},
>(category: T): string
getCategoryRoute
​
Get category/navigation route information for Vue Router.
Returns category or navigation URL and route informations for resolving SEO url. Use it with combination of <RouterLink>
or <NuxtLink>
in Vue.js or Nuxt.js projects.
Example:
html
<RouterLink :to="getCategoryRoute(navigationElement)">
ts
export function getCategoryRoute<
T extends {
type: string;
externalLink?: string;
seoUrls?: { seoPathInfo: string }[];
internalLink?: string;
id: string;
linkType?: string;
},
>(category: T)
getShippingMethodDeliveryTime
​
Get shipping delivery time
ts
export function getShippingMethodDeliveryTime<
T extends {
deliveryTime?: {
translated?: {
name?: string;
};
};
},
>(shippingMethod: T)
getFormattedPrice
​
Get formatted price
ts
export function getFormattedPrice(
value: string | number,
currency: string,
options: Options = {
direction: "ltr",
removeDecimals: false,
removeCurrency: false,
},
): string
getLanguageName
​
Get translated language name
ts
export function getLanguageName<
T extends {
translationCode?: { translated?: { name?: string } };
},
>(language: T): string
getListingFilters
​
biome-ignore lint/suspicious/noExplicitAny: Listing filters needs to be improved when schema types are ready
ts
export function getListingFilters<T extends Record<string, any>>(
aggregations: T | undefined | null,
): ListingFilter[]
getBackgroundImageUrl
​
ts
export function getBackgroundImageUrl<
T extends {
backgroundMedia?: {
metaData?: {
width?: number;
height?: number;
};
};
},
>(url: string, element: T): string
buildUrlPrefix
​
ts
export function buildUrlPrefix(
url: string | UrlRoute,
prefix: string,
): UrlRouteOutput
isMaintenanceMode
​
ts
export function isMaintenanceMode<
T extends {
code?: string;
},
>(errors: [T]): boolean