Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Introduce product name formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Manos Psychogyiopoulos committed May 19, 2021
1 parent 96f1844 commit 0f9279f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const OrderSummaryItem = ( { cartItem } ) => {
[]
);

const productNameValidation = useCallback(
( value ) => mustBeString( value ) && mustContain( value, '<name/>' ),
[]
);

const arg = useMemo(
() => ( {
context: 'summary',
Expand Down Expand Up @@ -110,6 +115,15 @@ const OrderSummaryItem = ( { cartItem } ) => {
validation: productPriceValidation,
} );

// Allow extensions to filter and format product names.
const productNameFormat = __experimentalApplyCheckoutFilter( {
filterName: 'productNameFormat',
defaultValue: '<name/>',
extensions,
arg,
validation: productNameValidation,
} );

return (
<div className="wc-block-components-order-summary-item">
<div className="wc-block-components-order-summary-item__image">
Expand All @@ -130,6 +144,7 @@ const OrderSummaryItem = ( { cartItem } ) => {
disabled={ true }
name={ name }
permalink={ permalink }
format= { productNameFormat }
/>
<ProductPrice
currency={ priceCurrency }
Expand Down
16 changes: 15 additions & 1 deletion assets/js/base/components/product-name/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import PropTypes from 'prop-types';
import { decodeEntities } from '@wordpress/html-entities';
import classnames from 'classnames';
import { createInterpolateElement } from 'wordpress-element';

/**
* Internal dependencies
Expand All @@ -15,10 +16,19 @@ const ProductName = ( {
disabled = false,
name,
permalink = '',
format = '<name/>',
...props
} ) => {

const classes = classnames( 'wc-block-components-product-name', className );
return disabled ? (

if ( ! format.includes( '<name/>' ) ) {
format = '<name/>';
// eslint-disable-next-line no-console
console.error( 'Product name formats need to include the `<name/>` tag.' );
}

const productNameComponent = disabled ? (
<span className={ classes } { ...props }>
{ decodeEntities( name ) }
</span>
Expand All @@ -27,6 +37,10 @@ const ProductName = ( {
{ decodeEntities( name ) }
</a>
);

return createInterpolateElement( format, {
name: productNameComponent
} );
};

ProductName.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ const CartLineItemRow = ( {
[]
);

const productNameValidation = useCallback(
( value ) => mustBeString( value ) && mustContain( value, '<name/>' ),
[]
);

const cartItemClassNameValidation = useCallback(
( value ) => mustBeString( value ),
[]
Expand Down Expand Up @@ -204,6 +209,16 @@ const CartLineItemRow = ( {
validation: productPriceValidation,
} );

// Allow extensions to filter and format product names.

const productNameFormat = __experimentalApplyCheckoutFilter( {
filterName: 'productNameFormat',
defaultValue: '<name/>',
extensions,
arg,
validation: productNameValidation,
} );

return (
<tr
className={ classnames( 'wc-block-cart-items__row', cartItemClassNameFilter, {
Expand Down Expand Up @@ -231,6 +246,7 @@ const CartLineItemRow = ( {
disabled={ isPendingDelete || isProductHiddenFromCatalog }
name={ name }
permalink={ permalink }
format={ productNameFormat }
/>
{ showBackorderBadge ? (
<ProductBackorderBadge />
Expand Down

0 comments on commit 0f9279f

Please sign in to comment.