Automattic\WooCommerce\Admin\Features\ProductBlockEditor

Init::create_default_product_template_by_custom_product_type()privateWC 1.0

Create default product template by custom product type if it does not have a template associated yet.

Метод класса: Init{}

Хуков нет.

Возвращает

Массив. The new templates.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->create_default_product_template_by_custom_product_type( $templates );
$templates(массив) (обязательный)
The registered product templates.

Код Init::create_default_product_template_by_custom_product_type() WC 9.7.1

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;
}