Skip to content

Commit

Permalink
Add template URL parameter to allow choosing template directly (wooco…
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanss authored Jun 19, 2024
1 parent 3ea4df2 commit e0a3596
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/js/product-editor/changelog/add-product-template-url
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add template URL parameter to allow choosing template directly
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { __ } from '@wordpress/i18n';
import { useLayoutTemplate } from '@woocommerce/block-templates';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { Product } from '@woocommerce/data';
import { getPath, getQuery } from '@woocommerce/navigation';
import {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore No types for this exist yet.
Expand Down Expand Up @@ -52,6 +53,7 @@ import { BlockEditorProps } from './types';
import { LoadingState } from './loading-state';
import type { ProductFormPostProps, ProductTemplate } from '../../types';
import isProductFormTemplateSystemEnabled from '../../utils/is-product-form-template-system-enabled';
import useProductEntityProp from '../../hooks/use-product-entity-prop';

const PluginArea = lazy( () =>
import( '@wordpress/plugins' ).then( ( module ) => ( {
Expand Down Expand Up @@ -198,6 +200,11 @@ export function BlockEditor( {
[ product?.meta_data ]
);

const [ , setProductTemplateId ] = useProductEntityProp(
'meta_data._product_template_id',
{ postType }
);

const { productTemplate } = useProductTemplate(
productTemplateId,
hasResolved ? product : null
Expand Down Expand Up @@ -303,6 +310,18 @@ export function BlockEditor( {
setIsEditorLoading( isEditorLoading );
}, [ isEditorLoading ] );

useEffect( function maybeSetProductTemplateFromURL() {
const query: { template?: string } = getQuery();
const isAddProduct = getPath().endsWith( 'add-product' );
if ( isAddProduct && query.template ) {
const productTemplates =
window.productBlockEditorSettings?.productTemplates ?? [];
if ( productTemplates.find( ( t ) => t.id === query.template ) ) {
setProductTemplateId( query.template );
}
}
}, [] );

// Check if the Modal editor is open from the store.
const isModalEditorOpen = useSelect( ( selectCore ) => {
return selectCore( productEditorUiStore ).isModalEditorOpen();
Expand Down

0 comments on commit e0a3596

Please sign in to comment.