前端页面模板创建产品无草稿生成问题排查
问题排查:前端提交产品草稿后WP-admin产品列表无显示
我正在搭建一个页面模板,需要实现以下功能:
- 将产品提交为Draft(草稿);
- 发送邮件给管理员请求审核(将草稿转为发布状态);
- 提交成功后跳转至「我的账户」页面。
所有字段均已填写完成,但WP-admin的产品列表中未出现任何产品,烦请帮忙排查问题所在。
子主题functions文件代码
add_action( 'init', 'customer_creates_product_frontend' ); function customer_creates_product_frontend() { if ( isset( $_POST['submit_product'] ) && wp_verify_nonce( $_POST['product_nonce'], 'submit_product_form' ) ) { $errors = array(); $product_title = sanitize_text_field( $_POST['product_title'] ); if ( empty( $product_title ) ) { $errors[] = 'The title of the product is mandatory.'; } $product_condition = sanitize_text_field( $_POST['product_condition'] ); if ( empty( $product_condition ) ) { $errors[] = 'The condition of the product is mandatory.'; } $starting_price = sanitize_text_field( $_POST['starting_price'] ); if ( empty( $starting_price ) || ! is_numeric( $starting_price ) ) { $errors[] = 'A valid starting price of the product is mandatory.'; } $reservation_price = sanitize_text_field( $_POST['reservation_price'] ); if ( empty( $reservation_price ) || ! is_numeric( $reservation_price ) ) { $errors[] = 'The reserveration price of the product is mandatory.'; } $increment_price = sanitize_text_field( $_POST['increment_price'] ); if ( empty( $increment_price ) || ! is_numeric( $increment_price ) ) { $errors[] = 'The increment of how much each bid should increase the product price with is mandatory.'; } $starting_sale_date = sanitize_text_field( $_POST['starting_sale_date'] ); if ( empty( $starting_sale_date ) ) { $errors[] = 'Starting date for the product is mandatory.'; } $end_sale_date = sanitize_text_field( $_POST['end_sale_date'] ); if ( empty( $end_sale_date ) ) { $errors[] = 'End date for the product is mandatory.'; } $buy_now_price = sanitize_text_field( $_POST['buy_now_price'] ); if ( empty( $buy_now_price ) || ! is_numeric( $buy_now_price ) || $buy_now_price <= $starting_price) { $errors[] = 'A valid BUY NOW price for the product is mandatory, and it must be higher than the starting price.'; } $short_description = sanitize_text_field( $_POST['short_description'] ); if ( empty( $short_description ) ) { $errors[] = 'The short description for the product is mandatory.'; } $long_description = sanitize_text_field( $_POST['long_description'] ); if ( empty( $long_description ) ) { $errors[] = 'The long description for the product is mandatory.'; } if ( empty( $errors ) ) { $new_product = wc_create_product( array ( 'name' => $product_title, 'regular_price' => $starting_price, 'type' => 'auction', // set to "Auction" by the default - simple, grouped, externel, variable are also available 'description' => $short_description, // short desctiption 'short_description' => $long_description, // long description 'meta_input' => array ( 'product_condition' => $product_condition, 'reservation_price' => $reservation_price, 'increment_price' => $increment_price, 'starting_sale_date' => date( 'Y-m-d \a\t H:i:s', strtotime( $starting_sale_date ) ), 'end_sale_date' => date( 'Y-m-d \a\t H:i:s', strtotime( $end_sale_date ) ), 'buy_now_price' => $buy_now_price, ) ) ); if ( is_wp_error( $new_product ) ) { echo '<p class="text-danger">Error creating product. Please try again.</p>'; } else { $the_customer = wp_get_current_user(); $name_of_customer = $the_customer->get_billing_first_name(); // send email to the admin with information about the request $admin_email = get_option( 'admin_email' ); $subject = 'Approval Request from '.$name_of_customer.' who wants to publish '.$product_title.''; $message = 'The customer wants to publish '.$product_title.', with the following description: '.$short_description.''; wp_mail( $admin_email, $subject, $message ); $new_product->set_status( 'draft' ); wp_safe_redirect( home_url( '/mitt-konto/' ) ); exit(); } } else { foreach ( $errors as $error ) { echo '<p class="text-danger">' . $error . '</p>'; } } } }
页面模板代码
<?php /** * Template Name: Create Product * @package Bootscore */ get_header(); ?> <div id="content" class="site-content"> <div id="primary" class="content-area"> <?php bs_after_primary(); ?> <main id="main" class="site-main"> <div class="entry-content"> <br><br><br> <?php if ( is_user_logged_in() && in_array( 'administrator', wp_get_current_user()->roles ) ) { ?> <form method="post" enctype="multipart/form-data"> <div class="mb-3"> <label for="product_title" class="form-label">Product Name</label> <input type="text" id="product_title" name="product_title" class="form-control" required> </div> <div class="mb-3"> <label for="product_condition" class="form-label">Product Condition</label> <select id="product_condition" name="product_condition" class="form-select" required> <option value="new">New</option> <option value="old">Old</option> </select> </div> <div class="mb-3"> <label for="starting_price" class="form-label">Starting Price</label> <input type="number" id="starting_price" name="starting_price" class="form-control" required> </div> <div class="mb-3"> <label for="reservation_price" class="form-label">Reservation Price</label> <input type="number" id="reservation_price" name="reservation_price" class="form-control" required> </div> <div class="mb-3"> <label for="increment_price" class="form-label">Increment Price</label> <input type="number" id="increment_price" name="increment_price" class="form-control" required> </div> <div class="mb-3"> <label for="starting_sale_date" class="form-label">Starting Sale Date</label> <input type="datetime-local" id="starting_sale_date" name="starting_sale_date" class="form-control" required> </div> <div class="mb-3"> <label for="end_sale_date" class="form-label">End Sale Date</label> <input type="datetime-local" id="end_sale_date" name="end_sale_date" class="form-control" required> </div> <div class="mb-3"> <label for="buy_now_price" class="form-label">Buy Now Price</label> <input type="number" id="buy_now_price" name="buy_now_price" class="form-control" required> </div> <div class="mb-3"> <label for="short_description" class="form-label">Short Description</label> <textarea id="short_description" name="short_description" class="form-control" required></textarea> </div> <div class="mb-3"> <label for="long_description" class="form-label">Long Description</label> <textarea id="long_description" name="long_description" class="form-control" required></textarea> </div> <button type="submit" name="submit_product" class="btn btn-primary">Create Product</button> <?php wp_nonce_field( 'submit_product_form', 'product_nonce' ); ?> </form> <?php } ?> </div> </main> </div></div> <?php get_footer();
核心排查与修复方案
状态变更未保存
调用$new_product->set_status( 'draft' )后,必须执行$new_product->save()才能将状态写入数据库,否则产品不会被正确存储。修改代码:$new_product->set_status( 'draft' ); $new_product->save(); // 新增这一行 wp_safe_redirect( home_url( '/mitt-konto/' ) ); exit();拍卖产品类型依赖验证
代码中指定产品类型为auction,需确认已安装并启用支持拍卖产品的WooCommerce插件(如WooCommerce Simple Auctions)。若无对应插件,wc_create_product会创建失败,且错误提示会被重定向覆盖。日期转换问题
前端datetime-local输入格式为YYYY-MM-DDTHH:MM,strtotime转换可能失败,可添加日志排查:error_log('转换后开始日期:' . date( 'Y-m-d \a\t H:i:s', strtotime( $starting_sale_date ) ));若转换失败,需调整日期格式化逻辑。
产品列表筛选检查
WP-admin产品列表默认只显示「发布」状态产品,需切换筛选器至「草稿」状态查看是否存在提交的产品。错误提示被重定向覆盖
当产品创建失败时,echo的错误信息会被重定向的header覆盖,无法显示。可将错误存入session,跳转后展示:if ( is_wp_error( $new_product ) ) { $_SESSION['product_error'] = $new_product->get_error_message(); wp_safe_redirect( home_url( '/create-product/' ) ); exit(); }然后在页面模板中添加session错误展示逻辑。
描述字段赋值颠倒
代码中description和short_description赋值颠倒,虽不影响创建,但需修正为逻辑正确的写法:'description' => $long_description, 'short_description' => $short_description,
内容的提问来源于stack exchange,提问作者Learning WooStoreFront
相关产品推荐
相关产品推荐

