我再次尝试组合一个页面模板,该模板将执行以下操作:
#1:以草稿形式提交产品 #2:向管理员发送电子邮件,请求批准(从草稿更改为发布) #3: 提交成功后,跳转至“我的账户”页面
所有字段都在那里,但 WP-admin 中的“产品”下没有显示任何产品。有人可以找出问题所在吗?
这是代码:
使用子主题的功能文件:
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();
函数 wc_create_product() 不存在…从 WooCommerce 3 开始,您应该使用 CRUD 方法,如下所示(假设
WC_Product_Auction
类存在):
add_action( 'init', 'customer_creates_product_frontend' );
function customer_creates_product_frontend() {
$errors = array();
if ( isset( $_POST['submit_product'] ) && wp_verify_nonce( $_POST['product_nonce'], 'submit_product_form' ) ) {
$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 ( count($errors) == 0 && class_exists('WC_Product_Auction') ) {
$product = new WC_Product_Auction();
$product->set_name($product_title);
$product->set_regular_price($starting_price);
$product->set_price($starting_price);
$product->set_short_description($short_description);
$product->set_description($long_description);
$product->add_meta_data('product_condition', $product_condition, true);
$product->add_meta_data('product_condition', $product_condition, true);
$product->add_meta_data('reservation_price', $reservation_price, true);
$product->add_meta_data('increment_price', $increment_price, true);
$product->add_meta_data('starting_sale_date', date( 'Y-m-d \a\t H:i:s', strtotime( $starting_sale_date ) ), true);
$product->add_meta_data('end_sale_date', date( 'Y-m-d \a\t H:i:s', strtotime( $end_sale_date ) ), true);
$product->add_meta_data('buy_now_price', $buy_now_price, true);
$product->set_status('draft');
$product_id = $product->save();
}
if ( ! $product_id ) {
echo '<p class="text-danger">Error creating product. Please try again.</p>';
} else {
global $current_user;
$first_name = $current_user->billing_first_name;
$admin_email = get_option( 'admin_email' );
$subject = 'Approval Request from '.$first_name.' who wants to publish '.$product_title.'';
$message = 'The customer wants to publish '.$product_title.', with the following short description: '.$short_description.'';
// Send email to the admin with information about the request
wp_mail( $admin_email, $subject, $message );
// Redirect User to My account
wp_safe_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
} elseif ( count($errors) > 0 ) {
foreach ( $errors as $error ) {
echo '<p class="text-danger">' . $error . '</p>';
}
}
}
模板保持不变...
代码位于子主题的functions.php 文件中(或插件中)。应该可以。