Overriding classes

Virtually always, you will want to keep part of the default functionality and only modify some aspect of the existing class. This is how the theme is usually extended.

For example, when overriding ProductCard, suppose we want to keep the default ScandiPWA implementation of the component, but with one change - we don't want the product images to be visible in the product cards.

In order to do this, we will make sure our class extends the base ProductCard class. Then, it will inherit the default functionality from its parent class, and we will be able to override any part of it.

All we need to do is import the original ProductCard class, extend it, and re-export the modified version. But there is one challenge: as mentioned before, if we try to import from Component/ProductCard/ProductCard.component, the import mechanism will resolve this to our overridden implementation, not the desired original class. Hence, it is impossible to import the original class from the parent theme using the Component alias if we intend to override this class.

The solution is to use another alias, SourceComponent to extend classes from the parent theme.

The ScandiPWA import mechanism provides the Source aliases to enable you to directly import from the parent theme, regardless of whether there is an overridden version available. Similarly, you can use the SourceRoute alias to import directly from a route defined in the parent theme, or the SourceUtil alias to import an utility file from the parent theme.

override: component/ProductCard/ProductCard.component.js
import { PureComponent } from 'react';
​
// we use the SourceComponent alias to explicitly import from the parent theme
// (if we would use Component/ProductCard/... instead, we would be trying to import
// the current file, which would result in an error)
import { ProductCard as SourceProductCard } from 'SourceComponent/ProductCard/ProductCard.component';
​
// we imported the original ProductCard class defined in the parent theme.
// we aliased the import to `SourceProductCard` to indicate that SourceProductCard
// is the parent theme version of the class
​
// you should always copy over the namespace declaration when overriding an existing class
// to avoid breaking plugins
/** @namespace Component/ProductCard/Component */
export class ProductCard extends SourceProductCard { // we can now extend SourceProductCard,
// and override any methods we want to change
​
// this method overrides the default implementation provided in the original ProductCard class
renderPicture() {
// returning null in a React component means rendering nothing
return null;
}
}
​
// we now export the extended and modified version of the class
export default ProductCard;

You should never have to copy any code from the parent theme to reuse it. It is a good practice to always import functions, classes, and values from the parent theme when you want to extend them, as shown above. Following this pattern will result in cleaner code - it will be immediately clear which functions your code changes by overriding them. In addition, there will be less code to maintain, resulting in faster development.

Indeed, we can check that now, the ProductCard component is the same as in the default theme, with one change: it no longer has a picture.

When overriding a JavaScript file, you should make the same exports as the file you are overriding. If the original theme file exports a certain constant, it is possible that other classes (or third party plug-ins) will expect that they can import this value. If you forget to re-export it when overriding the file, these imports will break. You are of course allowed to add new exports and modify existing ones, but please be careful to never leave out an export.

Overriding Constructors

ScandiPWA uses magic __construct functions instead of constructors. This is done to enable overriding of these functions, which you can override like any other function.