Automattic\WooCommerce\Admin\Features\ProductBlockEditor
Init::create_default_product_template_by_custom_product_type
Create default product template by custom product type if it does not have a template associated yet.
Method of the class: Init{}
No Hooks.
Returns
Array. The new templates.
Usage
// private - for code of main (parent) class only $result = $this->create_default_product_template_by_custom_product_type( $templates );
- $templates(array) (required)
- The registered product templates.
Init::create_default_product_template_by_custom_product_type() Init::create default product template by custom product type code WC 10.4.3
private function create_default_product_template_by_custom_product_type( array $templates ) {
// Getting the product types registered via the classic editor.
$registered_product_types = wc_get_product_types();
$custom_product_types = array_filter(
$registered_product_types,
function ( $product_type ) {
return ! in_array( $product_type, $this->supported_product_types, true );
},
ARRAY_FILTER_USE_KEY
);
$templates_with_product_type = array_filter(
$templates,
function ( $template ) {
$product_data = $template->get_product_data();
return ! is_null( $product_data ) && array_key_exists( 'type', $product_data );
}
);
$custom_product_types_on_templates = array_map(
function ( $template ) {
$product_data = $template->get_product_data();
return $product_data['type'];
},
$templates_with_product_type
);
foreach ( $custom_product_types as $product_type => $title ) {
if ( in_array( $product_type, $custom_product_types_on_templates, true ) ) {
continue;
}
$templates[] = new ProductTemplate(
array(
'id' => $product_type . '-product-template',
'title' => $title,
'product_data' => array(
'type' => $product_type,
),
)
);
}
return $templates;
}