我的目标是创建一个页面模板,其中需要发生几件事。总体思路是这样的:
Product name
Short
和 long description
Price
Sale price
(带有开始和结束日期)=它是否需要像“日期和时间选择器”中那样是一个 jQuery?SKU
Shipping
= 重量和尺寸Category
Product Featured Image
我已经根据我所知道的等做了我能做的事情,但需要帮助确保这适用于所有必填字段。它应该基于 BootStrap 5 布局。
我可以填写字段并按“提交”,但没有任何反应。管理中没有显示任何产品。
这是我正在使用的代码:
<?php
/*
Template Name: Create Product
*/
?>
<?php get_header();
global $product;
global $woocommerce;
// check if user is logged in - if not, return to home page with message
if ( ! is_user_logged_in() ) {
$redirect_to_page = home_url();
$page_viewed = basename( $_SERVER['REQUEST_URI'] );
if ( $page_viewed === "page-create-product.php" && $_SERVER['REQUEST_METHOD'] == 'GET' ) {
wp_safe_redirect( $redirect_to_page );
exit;
echo '<div class="not-logged-product-submit">You\'re not authorized to create products.</div>';
} else {
if ( is_user_logged_in() && current_user_can('administrator') ) {
echo '<div class="not-logged-product-submit">You\'re logged in. You are admin. Go ahead.</div>';
}
}
}
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<form method="post">
<div class="create-product-h1"><h1>Product Information</h1></div>
<div>
<div><label>Product Name</label></div>
<div><input type="text" name="product_name" class="product_name" placeholder="Name of your product" /></div>
</div>
<div>
<label for="product-type">
<select id="product-type" name="product-type">
<optgroup label="Product Type">
<option value="simple" selected="selected">Simple</option>
<option value="auction">Auction</option>
</optgroup>
</select>
<span class="woocommerce-product-type-tip"></span>
</label>
</div>
<?php if ( $product->is_type( 'auction' ) ) { ?>
<div>
<div><label>Item Condition</label></div>
<div><input type="text" name="auction_item_condition" class="auction_item_condition" placeholder="Old, New?" /></div>
</div>
<div>
<div><label>Auction Type</label></div>
<div><input type="text" name="auction_item_type" class="auction_item_type" placeholder="Normal or Sealed" /></div>
</div>
<div>
<div><label>Auction Start Price</label></div>
<div><input type="number" name="auction_item_start_price" class="auction_item_start_price" placeholder="e.g $100" /></div>
</div>
<div>
<div><label>Auction Reservation Price</label></div>
<div><input type="number" name="auction_item_res_price" class="auction_item_res_price" placeholder="e.g $100" /></div>
</div>
<div>
<div><label>Auction Minimum Bid Increment</label></div>
<div><input type="number" name="auction_item_min_increment" class="auction_item_min_increment" placeholder="e.g $10" /></div>
</div>
<div>
<div><label>Auction Reservation Price</label></div>
<div><input type="number" name="auction_item_res_price" class="auction_item_res_price" placeholder="e.g $40" /></div>
</div>
<?php } ?>
<div>
<div><label>Product Price</label></div>
<div><input type="number" name="product_price" class="product_price" placeholder="e.g $50" /></div>
</div>
<div>
<div><label>Product Sale Price</label></div>
<div><input type="number" name="product_sale_price" class="product_sale_price" placeholder="e.g $40" /></div>
</div>
<div>
<div><label>Product Short Description</label></div>
<div><textarea name="product_short_description" class="product_short_description" placeholder="Introduce your product"></textarea></div>
</div>
<div>
<div><label>Product Long Description</label></div>
<div><textarea name="product_long_description" class="product_long_description" placeholder="Describe your product in detail, making sure everyone knows what it is, etc."></textarea></div>
</div>
<div>
<div><label>Product SKU</label></div>
<div><input type="text" name="product_sku" class="product_sku" placeholder="e.g iPhone 14 Pro"/></div>
</div>
<div>
<div><label>Product Featured Image</label></div>
<div><input type="image" name="product_featured_image" class="product_featured_image" placeholder="URL to Image" />
<!-- not sure how to use this? -->
<!-- <a href="/wp-admin/media-upload.php?post_id=??&type=image&TB_iframe=1&width=750&height=750"
id="set-post-thumbnail" class="thickbox">Upload / Set Product Image</a> -->
</div>
</div>
<div>
<div><label>Product Weight</label></div>
<div><input type="number" name="product_weight" class="product_weight" placeholder="in ounce" /></div>
</div>
<div>
<div><label>Product Dimensions</label></div>
<div><input type="text" name="product_lenght" class="product_lenght" placeholder="in cm" /></div>
<div><input type="text" name="product_width" class="product_width" placeholder="in cm" /></div>
<div><input type="text" name="product_height" class="product_height" placeholder="in cm" /></div>
</div>
<div class="create-product-extra-fields"><h2>Order Fields From Product</h2></div>
<div>
<div><label>Private Customer</label></div>
<div><input type="text" name="order_meta_private" class="order_meta_private" placeholder="Yes or No" /></div>
</div>
<div>
<div><label>Buisness Customer</label></div>
<div><input type="text" name="order_meta_company" class="order_meta_company" placeholder="Name Of Company" /></div>
</div>
<div>
<input type="submit" value="Request Product To Be Published" class="submit" onclick="product_submit();" />
</div>
</form>
</main>
</div>
<?php
if ( isset( $_POST["submit"] ) ) {
global $wpdb;
$product_data = array(
'product_title' => $_POST['product_name'],
'product_type_simple' => $_POST['simple'],
'product_type_auction' => $_POST['auction'],
'product_price' => $_POST['auction_item_condition'],
'product_price' => $_POST['auction_item_type'],
'product_price' => $_POST['auction_item_start_price'],
'product_price' => $_POST['auction_item_res_price'],
'product_price' => $_POST['auction_item_min_increment'],
'product_price' => $_POST['product_price'],
'product_sale_price' => $_POST['product_sale_price'],
'product_short_description' => $_POST['product_short_description'],
'product_long_description' => $_POST['product_long_description'],
'product_sku' => $_POST['product_sku'],
'product_weight' => $_POST['product_weight'],
'product_width' => $_POST['product_width'],
'product_height' => $_POST['product_height'],
'product_lenght' => $_POST['product_lenght'],
'product_featured_image' => $_POST['product_featured_image'],
'product_meta_one' => $_POST['product_meta_one'],
'product_meta_two' => $_POST['product_meta_two'],
'post_type' => 'product',
'post_status' => 'draft'
);
$product_id = wp_insert_post( $product_data );
update_post_meta( $product_id, '_product_name', $_POST['product_name'] );
update_post_meta( $product_id, '_product_type_simple', $_POST['product_type_simple'] );
update_post_meta( $product_id, '_product_type_auction', $_POST['product_type_auction'] );
// the below is only for auction products
update_post_meta( $product_id, '_auction_item_condition', $_POST['auction_item_condition'] );
update_post_meta( $product_id, '_auction_item_type', $_POST['auction_item_type'] );
update_post_meta( $product_id, '_auction_item_start_price', $_POST['auction_item_start_price'] );
update_post_meta( $product_id, '_auction_item_res_price', $_POST['auction_item_res_price'] );
update_post_meta( $product_id, '_auction_item_min_increment', $_POST['auction_item_min_increment'] );
// the below is only for auction products - end
update_post_meta( $product_id, '_product_name', $_POST['product_name'] );
update_post_meta( $product_id, '_regular_price', $_POST['product_price'] );
update_post_meta( $product_id, '_sale_price', $_POST['product_sale_price'] );
update_post_meta( $product_id, '_product_short_description', $_POST['product_short_description'] );
update_post_meta( $product_id, '_product_long_description', $_POST['product_long_description'] );
update_post_meta( $product_id, '_product_sku', $_POST['product_sku'] );
update_post_meta( $product_id, '_product_weight', $_POST['product_weight'] );
update_post_meta( $product_id, '_product_width', $_POST['product_width'] );
update_post_meta( $product_id, '_product_height', $_POST['product_height'] );
update_post_meta( $product_id, '_product_lenght', $_POST['product_lenght'] );
update_post_meta( $product_id, '_product_featured_image', $_POST['product_featured_image'] );
update_post_meta( $product_id, '_product_meta_one', $_POST['produkt_meta_one'] );
update_post_meta( $product_id, '_product_meta_two', $_POST['produkt_meta_two'] );
}
get_footer();
?>
检查您的代码后,我发现了多个需要解决的问题。
$product
全局变量不会指向任何产品数据。这意味着 $product->is_type( 'auction' )
不会按您的预期工作。if else
块。您将看到登录的用户,但管理员无法查看和提交表单。exit
函数调用下的代码不会被执行。 (echo '<div class="not-logged-product-submit">You\'re not authorized to create products.</div>';
不会被执行)headers already sent
警告,因为在 wp_safe_redirect
函数之后调用 get_header()
。要向未登录的用户显示错误消息并重定向到主页,请使用 javascript 而不是 wp_safe_redirect
。wp_insert_post
函数中使用了不必要的参数。我猜您想在产品插入中添加元数据。在这种情况下,您可以使用 meta_input
参数。请查看 https://developer.wordpress.org/reference/functions/wp_insert_post/ 了解更多详细信息。 另外,我认为使用 WooCommerce 函数比使用下面的
wp_insert_post
函数更好。
$product = new WC_Product_Simple();
$product->set_name( 'Lorem Ipsum' ); // product title
$product->set_slug( 'lorem-ipsum' );
$product->set_regular_price( 500.00 ); // in current shop currency
$product->set_short_description( '<p>Lorem Ipsum</p>' );
$product->set_description( 'long description here...' );
$product->set_image_id( 90 );
// $product->set_category_ids( array( 19 ) );
// $product->set_tag_ids( array(123) );
$product->save();