My goal is to create a page template whereof several things needs to happen. The general idea is this:
- If the user is not logged in, exit with a message.
- Is the user logged + is admin = allow page template content to be displayed.
- I need all the fields that otherwise are available in WooCommerce admin to be available within the template, in other words – the following:
Product name
Short
andlong description
Price
Sale price
(with start and end date) = does it need to be a jQuery of sorts as in "date and time picker"?SKU
Shipping
= weight and dimensionsCategory
Product Featured Image
I have done what I can based on what I know etc, but need help making sure this works with all the required fields. It should be based on a BootStrap 5 layout.
I can fill out the fields and press "Submit", but nothing happens. No product shows up in admin.
This is the code that I am working with:
<?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();
?>
2
Answers
After reviewing your code, I have identified multiple issues that need to be addressed.
$product
global variable won’t point to any product data. It means$product->is_type( 'auction' )
won’t work as you expected.if else
block. You will see the users logged in but not admin can view&submit the form.exit
function call won’t be executed. (echo '<div class="not-logged-product-submit">You're not authorized to create products.</div>';
won’t be executed)headers already sent
warnings aswp_safe_redirect
is called afterget_header()
function. To show the error message and redirect to homepage for non-logged users, please use javascript instead ofwp_safe_redirect
.wp_insert_post
function. I guess you wanted to add the meta data with the product insert. In the case you can usemeta_input
parameter.Please check https://developer.wordpress.org/reference/functions/wp_insert_post/ for more details.
Also, I think it’s better to use WooCommerce function than
wp_insert_post
function like below.First you need add attribute
name=submit
:Also
Should always not empty 🙂