Apply filter to add metadata for generated variations (#45953)

* Add meta_data parameter in generateProductVariations

* Apply filter to get meta_data to generate variations

* Add meta_data parameter in generate variations endpoint

* Update name and add documentation

* Fix useEntityRecord in blockEditor
This commit is contained in:
Nathan Silveira 2024-04-02 05:25:01 -03:00 committed by GitHub
parent 7765984bf3
commit e2eaa55b7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 55 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add meta_data parameter in generateProductVariations

View File

@ -17,7 +17,11 @@ import type {
GenerateRequest, GenerateRequest,
} from './types'; } from './types';
import CRUD_ACTIONS from './crud-actions'; import CRUD_ACTIONS from './crud-actions';
import { ProductAttribute, ProductDefaultAttribute } from '../products/types'; import {
Product,
ProductAttribute,
ProductDefaultAttribute,
} from '../products/types';
export function generateProductVariationsError( key: IdType, error: unknown ) { export function generateProductVariationsError( key: IdType, error: unknown ) {
return { return {
@ -48,6 +52,7 @@ export const generateProductVariations = function* (
type?: string; type?: string;
attributes: ProductAttribute[]; attributes: ProductAttribute[];
default_attributes?: ProductDefaultAttribute[]; default_attributes?: ProductDefaultAttribute[];
meta_data?: Product[ 'meta_data' ];
}, },
data: GenerateRequest, data: GenerateRequest,
saveAttributes = true saveAttributes = true

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Apply filter to get meta_data to generate variations

View File

@ -14,6 +14,7 @@ import { uploadMedia } from '@wordpress/media-utils';
import { PluginArea } from '@wordpress/plugins'; import { PluginArea } from '@wordpress/plugins';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { useLayoutTemplate } from '@woocommerce/block-templates'; import { useLayoutTemplate } from '@woocommerce/block-templates';
import { Product } from '@woocommerce/data';
import { import {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore No types for this exist yet. // @ts-ignore No types for this exist yet.
@ -154,7 +155,7 @@ export function BlockEditor( {
{ postType } { postType }
); );
const { record: product } = useEntityRecord( const { record: product } = useEntityRecord< Product >(
'postType', 'postType',
postType, postType,
productId productId

View File

@ -2,7 +2,6 @@
* External dependencies * External dependencies
*/ */
import { dispatch, resolveSelect, useSelect } from '@wordpress/data'; import { dispatch, resolveSelect, useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useCallback, useMemo, useState } from '@wordpress/element'; import { useCallback, useMemo, useState } from '@wordpress/element';
import { getNewPath, getPath, navigateTo } from '@woocommerce/navigation'; import { getNewPath, getPath, navigateTo } from '@woocommerce/navigation';
import { import {
@ -11,6 +10,8 @@ import {
ProductDefaultAttribute, ProductDefaultAttribute,
ProductVariation, ProductVariation,
} from '@woocommerce/data'; } from '@woocommerce/data';
import { applyFilters } from '@wordpress/hooks';
import { useEntityProp, useEntityRecord } from '@wordpress/core-data';
/** /**
* Internal dependencies * Internal dependencies
@ -58,6 +59,13 @@ export function useProductVariationsHelper() {
'product', 'product',
'id' 'id'
); );
const { record: product } = useEntityRecord< Product >(
'postType',
'product',
productId
);
const [ _isGenerating, setIsGenerating ] = useState( false ); const [ _isGenerating, setIsGenerating ] = useState( false );
const { isGeneratingVariations, generateError } = useSelect( const { isGeneratingVariations, generateError } = useSelect(
@ -116,6 +124,18 @@ export function useProductVariationsHelper() {
await dispatch( await dispatch(
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME
).invalidateResolutionForStore(); ).invalidateResolutionForStore();
/**
* Filters the meta_data array for generated variations.
*
* @filter woocommerce.product.variations.generate.meta_data
* @param {Object} product Main product object.
* @return {Object} meta_data array for variations.
*/
const meta_data = applyFilters(
'woocommerce.product.variations.generate.meta_data',
[],
product
);
return dispatch( EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME ) return dispatch( EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME )
.generateProductVariations< { .generateProductVariations< {
@ -133,6 +153,7 @@ export function useProductVariationsHelper() {
{ {
delete: true, delete: true,
default_values: defaultVariationValues, default_values: defaultVariationValues,
meta_data,
} }
) )
.then( async ( response ) => { .then( async ( response ) => {

View File

@ -19,4 +19,9 @@ declare module '@wordpress/core-data' {
prop: string, prop: string,
id?: string id?: string
): [ T, ( value: T ) => void, T ]; ): [ T, ( value: T ) => void, T ];
function useEntityRecord< T = unknown >(
kind: string,
name: string,
id: number | string
): { record: T };
} }

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add meta_data parameter in generate variations endpoint

View File

@ -1193,10 +1193,11 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* @todo Add to interface in 4.0. * @todo Add to interface in 4.0.
* @param WC_Product $product Variable product. * @param WC_Product $product Variable product.
* @param int $limit Limit the number of created variations. * @param int $limit Limit the number of created variations.
* @param array $default_values Key value pairs to set on created variations. * @param array $default_values Key value pairs to set on created variations.
* @param array $metadata Key value pairs to set as meta data on created variations.
* @return int Number of created variations. * @return int Number of created variations.
*/ */
public function create_all_product_variations( $product, $limit = -1, $default_values = array() ) { public function create_all_product_variations( $product, $limit = -1, $default_values = array(), $metadata = array() ) {
$count = 0; $count = 0;
if ( ! $product ) { if ( ! $product ) {
@ -1226,6 +1227,9 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
} }
$variation = wc_get_product_object( 'variation' ); $variation = wc_get_product_object( 'variation' );
$variation->set_props( $default_values ); $variation->set_props( $default_values );
foreach ( $metadata as $meta ) {
$variation->add_meta_data( $meta['key'], $meta['value'] );
}
$variation->set_parent_id( $product->get_id() ); $variation->set_parent_id( $product->get_id() );
$variation->set_attributes( $possible_attribute ); $variation->set_attributes( $possible_attribute );
$variation_id = $variation->save(); $variation_id = $variation->save();

View File

@ -1121,8 +1121,9 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
$response = array(); $response = array();
$product = wc_get_product( $product_id ); $product = wc_get_product( $product_id );
$default_values = isset( $request['default_values'] ) ? $request['default_values'] : array(); $default_values = isset( $request['default_values'] ) ? $request['default_values'] : array();
$meta_data = isset( $request['meta_data'] ) ? $request['meta_data'] : array();
$data_store = $product->get_data_store(); $data_store = $product->get_data_store();
$response['count'] = $data_store->create_all_product_variations( $product, Constants::get_constant( 'WC_MAX_LINKED_VARIATIONS' ), $default_values ); $response['count'] = $data_store->create_all_product_variations( $product, Constants::get_constant( 'WC_MAX_LINKED_VARIATIONS' ), $default_values, $meta_data );
if ( isset( $request['delete'] ) && $request['delete'] ) { if ( isset( $request['delete'] ) && $request['delete'] ) {
$deleted_count = $this->delete_unmatched_product_variations( $product ); $deleted_count = $this->delete_unmatched_product_variations( $product );