Automattic\WooCommerce\Admin\API
OnboardingTasks::create_product_from_template()
Creates a product from a template name passed in through the template_name param.
Method of the class: OnboardingTasks{}
Hooks from the method
Return
WP_REST_Response|WP_Error
.
Usage
$result = OnboardingTasks::create_product_from_template( $request );
- $request(WP_REST_Request) (required)
- Request data.
OnboardingTasks::create_product_from_template() OnboardingTasks::create product from template code WC 8.6.1
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 ) || 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( 'auto-draft' ); $product->save(); return rest_ensure_response( array( 'id' => $product->get_id(), ) ); }