Automattic\WooCommerce\Admin\API
OnboardingTasks::create_product_from_template
Creates a product from a template name passed in through the template_name param.
Метод класса: OnboardingTasks{}
Хуки из метода
Возвращает
WP_REST_Response|WP_Error.
Использование
$result = OnboardingTasks::create_product_from_template( $request );
- $request(WP_REST_Request) (обязательный)
- Request data.
Код OnboardingTasks::create_product_from_template() OnboardingTasks::create product from template WC 10.5.2
public static function create_product_from_template( $request ) {
$template_name = basename( $request->get_param( 'template_name' ) );
$template_path = __DIR__ . '/Templates/' . $template_name . '_product.csv';
$template_path = apply_filters( 'woocommerce_product_template_csv_file_path', $template_path, $template_name );
$import = self::import_sample_products_from_csv( $template_path );
if ( is_wp_error( $import ) || ! is_array( $import['imported'] ) || 0 === count( $import['imported'] ) ) {
return new \WP_Error(
'woocommerce_rest_product_creation_error',
/* translators: %s is template name */
__( 'Sorry, creating the product with template failed.', 'woocommerce' ),
array( 'status' => 500 )
);
}
$product = wc_get_product( $import['imported'][0] );
$product->set_status( ProductStatus::AUTO_DRAFT );
$product->save();
return rest_ensure_response(
array(
'id' => $product->get_id(),
)
);
}