Allow customization of variable product and variations
This commit is contained in:
parent
46b5cf8803
commit
28c2597b0f
|
@ -25,7 +25,8 @@ const client = factories.api.withDefaultPermalinks;
|
|||
const config = require( 'config' );
|
||||
const simpleProductName = config.get( 'products.simple.name' );
|
||||
const simpleProductPrice = config.has('products.simple.price') ? config.get('products.simple.price') : '9.99';
|
||||
|
||||
const defaultVariableProduct = config.get( 'products.variable' );
|
||||
const defaultVariations = config.get( 'products.variation' );
|
||||
|
||||
/**
|
||||
* Verify and publish
|
||||
|
@ -223,16 +224,17 @@ const createSimpleProductWithCategory = async ( productName, productPrice, categ
|
|||
/**
|
||||
* Create variable product.
|
||||
*
|
||||
* @returns the ID of the variable product
|
||||
* @param varProduct Defaults to the variable product object in `default.json`
|
||||
* @param variations Defaults to the variation object in `default.json`
|
||||
* @returns the ID of the created variable product
|
||||
*/
|
||||
const createVariableProduct = async () => {
|
||||
const variableProduct = await factories.products.variable.create();
|
||||
const defaultVariations = config.get( 'products.variations' );
|
||||
const createVariableProduct = async ( varProduct = defaultVariableProduct, variations = defaultVariations ) => {
|
||||
const variableProduct = await factories.products.variable.create( varProduct );
|
||||
|
||||
for( const variation of defaultVariations ){
|
||||
for( const v of variations ){
|
||||
await factories.products.variation.create({
|
||||
productId: variableProduct.id,
|
||||
variation: variation
|
||||
variation: v
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
import { VariableProduct } from '@woocommerce/api';
|
||||
import { Factory } from 'fishery';
|
||||
import config from 'config';
|
||||
|
||||
/**
|
||||
* Creates a new factory for creating variable products.
|
||||
* This factory will create a default variable product when `create()` is called without parameters.
|
||||
* This does not include creating product variations.
|
||||
* Instead, use `variationFactory()` for that.
|
||||
*
|
||||
* @param {HTTPClient} httpClient The HTTP client we will give the repository.
|
||||
* @return {AsyncFactory} The factory for creating models.
|
||||
*/
|
||||
export function variableProductFactory(httpClient) {
|
||||
const repository = VariableProduct.restRepository(httpClient);
|
||||
const defaultVariableProduct = config.get('products.variable');
|
||||
|
||||
return Factory.define(({ params, onCreate }) => {
|
||||
onCreate((model) => {
|
||||
|
@ -19,10 +18,10 @@ export function variableProductFactory(httpClient) {
|
|||
});
|
||||
|
||||
return {
|
||||
name: params.name ?? defaultVariableProduct.name,
|
||||
name: params.name,
|
||||
type: 'variable',
|
||||
defaultAttributes: params.defaultAttributes ?? defaultVariableProduct.defaultAttributes,
|
||||
attributes: params.attributes ?? defaultVariableProduct.attributes
|
||||
defaultAttributes: params.defaultAttributes,
|
||||
attributes: params.attributes
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { ProductVariation } from '@woocommerce/api';
|
||||
import { Factory } from 'fishery';
|
||||
import config from 'config';
|
||||
|
||||
/**
|
||||
* Creates a new factory for creating a product variation.
|
||||
|
@ -10,8 +9,6 @@ import config from 'config';
|
|||
*/
|
||||
export function variationFactory(httpClient) {
|
||||
const repository = ProductVariation.restRepository(httpClient);
|
||||
const defaultVariations = config.get('products.variations');
|
||||
const defaultVariation = defaultVariations[0];
|
||||
|
||||
return Factory.define(({ params, onCreate }) => {
|
||||
const { productId, variation } = params;
|
||||
|
@ -21,8 +18,8 @@ export function variationFactory(httpClient) {
|
|||
});
|
||||
|
||||
return {
|
||||
regularPrice: variation.regularPrice ?? defaultVariation.regularPrice,
|
||||
attributes: variation.attributes ?? defaultVariation.attributes
|
||||
regularPrice: variation.regularPrice,
|
||||
attributes: variation.attributes
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue