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:
parent
7765984bf3
commit
e2eaa55b7d
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: add
|
||||
|
||||
Add meta_data parameter in generateProductVariations
|
|
@ -17,7 +17,11 @@ import type {
|
|||
GenerateRequest,
|
||||
} from './types';
|
||||
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 ) {
|
||||
return {
|
||||
|
@ -48,6 +52,7 @@ export const generateProductVariations = function* (
|
|||
type?: string;
|
||||
attributes: ProductAttribute[];
|
||||
default_attributes?: ProductDefaultAttribute[];
|
||||
meta_data?: Product[ 'meta_data' ];
|
||||
},
|
||||
data: GenerateRequest,
|
||||
saveAttributes = true
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: add
|
||||
|
||||
Apply filter to get meta_data to generate variations
|
|
@ -14,6 +14,7 @@ import { uploadMedia } from '@wordpress/media-utils';
|
|||
import { PluginArea } from '@wordpress/plugins';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useLayoutTemplate } from '@woocommerce/block-templates';
|
||||
import { Product } from '@woocommerce/data';
|
||||
import {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore No types for this exist yet.
|
||||
|
@ -154,7 +155,7 @@ export function BlockEditor( {
|
|||
{ postType }
|
||||
);
|
||||
|
||||
const { record: product } = useEntityRecord(
|
||||
const { record: product } = useEntityRecord< Product >(
|
||||
'postType',
|
||||
postType,
|
||||
productId
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
* External dependencies
|
||||
*/
|
||||
import { dispatch, resolveSelect, useSelect } from '@wordpress/data';
|
||||
import { useEntityProp } from '@wordpress/core-data';
|
||||
import { useCallback, useMemo, useState } from '@wordpress/element';
|
||||
import { getNewPath, getPath, navigateTo } from '@woocommerce/navigation';
|
||||
import {
|
||||
|
@ -11,6 +10,8 @@ import {
|
|||
ProductDefaultAttribute,
|
||||
ProductVariation,
|
||||
} from '@woocommerce/data';
|
||||
import { applyFilters } from '@wordpress/hooks';
|
||||
import { useEntityProp, useEntityRecord } from '@wordpress/core-data';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
|
@ -58,6 +59,13 @@ export function useProductVariationsHelper() {
|
|||
'product',
|
||||
'id'
|
||||
);
|
||||
|
||||
const { record: product } = useEntityRecord< Product >(
|
||||
'postType',
|
||||
'product',
|
||||
productId
|
||||
);
|
||||
|
||||
const [ _isGenerating, setIsGenerating ] = useState( false );
|
||||
|
||||
const { isGeneratingVariations, generateError } = useSelect(
|
||||
|
@ -116,6 +124,18 @@ export function useProductVariationsHelper() {
|
|||
await dispatch(
|
||||
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME
|
||||
).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 )
|
||||
.generateProductVariations< {
|
||||
|
@ -133,6 +153,7 @@ export function useProductVariationsHelper() {
|
|||
{
|
||||
delete: true,
|
||||
default_values: defaultVariationValues,
|
||||
meta_data,
|
||||
}
|
||||
)
|
||||
.then( async ( response ) => {
|
||||
|
|
|
@ -19,4 +19,9 @@ declare module '@wordpress/core-data' {
|
|||
prop: string,
|
||||
id?: string
|
||||
): [ T, ( value: T ) => void, T ];
|
||||
function useEntityRecord< T = unknown >(
|
||||
kind: string,
|
||||
name: string,
|
||||
id: number | string
|
||||
): { record: T };
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: add
|
||||
|
||||
Add meta_data parameter in generate variations endpoint
|
|
@ -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.
|
||||
* @param WC_Product $product Variable product.
|
||||
* @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.
|
||||
*/
|
||||
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;
|
||||
|
||||
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->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_attributes( $possible_attribute );
|
||||
$variation_id = $variation->save();
|
||||
|
|
|
@ -1121,8 +1121,9 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
|
|||
$response = array();
|
||||
$product = wc_get_product( $product_id );
|
||||
$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();
|
||||
$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'] ) {
|
||||
$deleted_count = $this->delete_unmatched_product_variations( $product );
|
||||
|
|
Loading…
Reference in New Issue