442adcc395
* Remove template matching from front-end * Create 'woocommerce_rest_get_product' filter to allow extensions to change the product when it's fetched through the REST API * Stop defaulting to 'standard-product-template' product template and handle defaults only in getLayoutTemplateId function * Use created hook to determine and persist the product template ID when it's not defined. Also introduce 'match_fn' in ProductTemplate to allow extensions to provide robust ways of matching the template * Add changelog * Fix tests * Remove outdated tests * Fix issue in filter because $product can be false and move API change to v3 * Add comments to continue statements * Small refactor * Only send layout template id to useLayoutTemplate after product is loaded * Revert front-end changes * Remove match_fn and create filter to allow extensions to determine the product template id * Avoid loading layout template before product is loaded * Create woocommerce_product_editor_determine_product_template filter and use it * Update changelogs and remove unused code * Remove _product_template_id for products that were created with the new product editor. * Use only id to find productTemplate * Turn hook into experimental * Remove deprecated tests * Rename filter * Add more typings to useEntityRecord * Use hasResolved boolean to check whether product has been resolved * Add changelog * Add 'variation' to ProductType * Don't default to the standard-product-template for variations * Update changelog * Accept null in getLayoutTemplateId * Fix edit variable product test * Rename hook * Revert changed logic to avoid regressions * Increment useProductTemplate logic * Default to standard-product-template instead of undefined when no matches * Re-import Features class --------- Co-authored-by: Jon Lane <jon.lane@automattic.com> |
||
---|---|---|
.. | ||
changelog | ||
src | ||
.eslintrc.js | ||
.npmrc | ||
README.md | ||
babel.config.js | ||
changelog.md | ||
composer.json | ||
composer.lock | ||
jest.config.json | ||
package.json | ||
tsconfig-cjs.json | ||
tsconfig.json | ||
webpack.config.js |
README.md
@woocommerce/block-templates
A collection of utility functions for use with WooCommerce admin block templates.
API
registerWooBlockType
Registers a WooCommerce block type.
Usage
import { registerWooBlockType } from '@woocommerce/block-templates';
import metadata from './block.json';
import { Edit } from './edit';
registerWooBlockType( {
name: metadata.name,
metadata: metadata,
settings: {
edit: Edit,
},
} );
Parameters
- blockMetadata
Object
: Block metadata.
Returns
WPBlockType | undefined
: The block type if it was registered successfully, otherwiseundefined
.
useLayoutTemplate
This hook is used to retrieve a layout template from the server.
Usage
import { useLayoutTemplate } from '@woocommerce/block-templates';
export function Example() {
const { layoutTemplate, isResolving } =
useLayoutTemplate( 'my-layout-template' );
return (
<div>
{ isResolving && <p>Loading layout template...</p> }
{ layoutTemplate && (
<p>{ JSON.stringify( layoutTemplate, null, 4 ) }</p>
) }
{ ! layoutTemplate && ! isResolving && (
<p>'Layout template does not exist!'</p>
) }
</div>
);
}
Parameters
- layoutTemplateId
string
: The id of the layout template to retrieve.
Returns
Object
- layoutTemplate
Object | undefined
: The layout template if it was found, otherwisenull
. - isResolving
boolean
: Whether or not the layout template is resolving.
- layoutTemplate
useWooBlockProps
This hook is used to lightly mark an element as a WooCommerce block template block. The block's attributes must be passed to this hook and the return result passed to the outermost element of the block in order for the block to properly function in WooCommerce block template contexts.
If you define a ref for the element, it is important to pass the ref to this hook, which the hook in turn will pass to the component through the props it returns. Optionally, you can also pass any other props through this hook, and they will be merged and returned.
Usage
import { useWooBlockProps } from '@woocommerce/block-templates';
export function Edit( { attributes } ) {
const { blockProps } = useWooBlockProps( attributes, {
className: 'my-block',
} );
return <div { ...blockProps }>Block content</div>;
}
Parameters
- attributes
Object
: Block attributes. - props
Object
: Optional. Props to pass to the element.
Returns
Object
: Props to pass to the element to mark as a WooCommerce block.