Skip to content

Instantly share code, notes, and snippets.

@Rocky-Mehta88
Last active November 7, 2020 16:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rocky-Mehta88/b0458efd23af251be1fa3252a4b1b967 to your computer and use it in GitHub Desktop.
Save Rocky-Mehta88/b0458efd23af251be1fa3252a4b1b967 to your computer and use it in GitHub Desktop.
Add woocommerce product from font-end, Full width Layout (User must be logged in).
<?php
/**
* Template Name: Add Product Full width Page.
* Description: Add woocommerce product from font-end, Full width Layout (User must be logged in).
* Author: Rocky Mehta
* Link: https://rocky-mehta.com/
* Email: rocky.mehta88@gmail.com
*/
wp_head();
if(!is_user_logged_in()){
wp_redirect("my-account");
exit;
}
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
if( isset($_POST['submit']) ) {
$product_title = $_POST['product_title'];
$description = $_POST['description'];
$_regular_price = $_POST['_regular_price'];
$featured = $_POST['is_featured'];
$product_cat = $_POST['product_cat'];
$product_tag = $_POST['product_tag'];
// Add the content of the form to $post as an array
$post_data = array(
'post_title' => $product_title,
'post_content' => $description,
'post_author' => get_current_user_id(),
'post_status' => 'draft',
'post_type' => 'product'
);
$my_post_id = wp_insert_post($post_data);
update_post_meta($my_post_id, '_regular_price', $_regular_price);
update_post_meta($my_post_id, '_featured', $featured);
wp_set_post_terms( $my_post_id, array($product_cat), 'product_cat' );
wp_set_post_terms( $my_post_id, array($product_tag), 'product_tag' );
$attachment_id = media_handle_upload( 'my_image_upload', $my_post_id );
set_post_thumbnail( $my_post_id, $attachment_id );
$files = $_FILES["kv_multiple_attachments"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("kv_multiple_attachments" => $file);
foreach ($_FILES as $file => $array) {
$newupload = kv_handle_attachment($file,$my_post_id);
$img_arr[] = $newupload;
}
}
}
update_post_meta( $my_post_id, '_product_image_gallery', implode(',', $img_arr) );
$headers = "From: " . get_bloginfo( 'name' ) . " <rocky.mehta88@gmail.com>" . "\r\n\\";
$subject = sprintf( __( '[%s] New Product Added', 'wpuf' ), get_bloginfo( 'name' ) );
$msg = sprintf( __( 'New product added at %s', 'wpuf' ), get_bloginfo( 'name' ) );
$msg .= "Name: ".$product_title."\r\n\\";
$msg .= "Category: ".$product_cat."\r\n\\";
$msg .= "Tag: ".$product_tag."\r\n\\";
$msg .= "Price: ".$_regular_price."\r\n\\";
$msg .= "Featured: ".$featured."\r\n\\";
$receiver = get_bloginfo( 'admin_email' );
wp_mail( $receiver, $subject, $msg, $headers );
} ?>
<article <?php post_class();?>>
<header class="entry-header">
<h1 class="entry-title" itemprop="headline">Add Product</h1>
</header>
<div class="entry-content add_product_page" itemprop="text">
<form class="wpuf-form-add" action="" method="post" enctype="multipart/form-data">
<ul class="wpuf-form">
<li class="wpuf-el product_title" data-label="Product Title">
<div class="wpuf-label">
<label for="product_title">Product Title
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields">
<input required class="textfield wpuf_post_title" id="product_title" type="text" data-required="yes" data-type="text" name="product_title" placeholder="" value="" size="40">
</div>
</li>
<li class="wpuf-el _regular_price" data-label="Price">
<div class="wpuf-label">
<label for="_regular_price">Price
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields">
<input required class="textfield wpuf__regular_price" id="_regular_price" type="text" data-required="yes" data-type="text" name="_regular_price" placeholder="" value="" size="10">
<span class="wpuf-help">Price in Dollar.</span>
</div>
</li>
<li class="wpuf-el description" data-label="description">
<div class="wpuf-label">
<label for="my_image_upload">Image
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields">
<input required type="file" name="my_image_upload" id="my_image_upload" />
<span class="wpuf-wordlimit-message wpuf-help"></span>
</div>
</li>
<li class="wpuf-el description" data-label="description">
<div class="wpuf-label">
<label for="kv_multiple_attachments"> Product Gallery
<span class="required"></span>
</label>
</div>
<div class="wpuf-fields">
<input type="file" name="kv_multiple_attachments[]" id="kv_multiple_attachments" multiple="multiple" />
<span class="wpuf-wordlimit-message wpuf-help"></span>
</div>
</li>
<li class="wpuf-el product_tag" data-label="Product Tag">
<div class="wpuf-label">
<label for="product_tag">Product Tag
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields wpuf_product_tag_select">
<select required data-required="yes" data-type="select" name="product_tag" id="product_tag" class="product_tag wpuf_category_122">
<option value="">— Select —</option>
<?php
$tags = get_terms( 'product_tag' );
if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
if(in_array($tag->name,$tags_list)){
?><option value="<?php echo $tag->name;?>" selected><?php echo $tag->name;?></option><?php
} else {
?><option value="<?php echo $tag->name;?>"><?php echo $tag->name;?></option><?php
}
}
}
?>
</select>
</div>
</li>
<li class="wpuf-el category" data-label="Product Categories">
<div class="wpuf-label">
<label for="product_cat">Product Categories
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields wpuf_category_select">
<select required data-required="yes" data-type="select" name="product_cat" id="product_cat" class="product_cat">
<option value="">— Select —</option>
<?php
$terms = get_terms( 'product_cat' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
?><option value="<?php echo $term->term_id;?>"><?php echo $term->name;?></option><?php
}
}
?>
</select>
</div>
</li>
<li class="wpuf-el description" data-label="description">
<div class="wpuf-label">
<label for="description">Description
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields">
<textarea class="textareafield" id="description" name="description" data-required="yes" data-type="textarea" placeholder="" rows="5" cols="10"></textarea>
<span class="wpuf-wordlimit-message wpuf-help"></span>
</div>
</li>
<li class="wpuf-el delivery" data-label="is_featured">
<div class="wpuf-label">
<label for="is_featured_no">Is Featured Product?
<span class="required">*</span>
</label>
</div>
<div class="wpuf-fields">
<input class="radiofield" name="is_featured" id="is_featured_yes" type="radio" value="Yes" /><label>Yes</label>
<input class="radiofield" name="is_featured" id="is_featured_no" type="radio" value="No" checked="checked"/><label>No</label>
</div>
</li>
<li class="wpuf-submit">
<div class="wpuf-label">
&nbsp;
</div>
<input type="submit" name="submit" value="Save Product">
</li>
</ul>
</form>
</div>
</article>
<?php
}
// Add multiple image function
function kv_handle_attachment($file_handler, $my_post_id) {
$attach_id = media_handle_upload( $file_handler, $my_post_id );
return $attach_id;
}
wp_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment