stankobrin
8/5/2014 - 5:55 AM

front end posting

front end posting

define('APFSURL', WP_PLUGIN_URL."/".dirname( plugin_basename( __FILE__ ) ) );
define('APFPATH', WP_PLUGIN_DIR."/".dirname( plugin_basename( __FILE__ ) ) );

function apf_enqueuescripts()
{
    wp_enqueue_script('apf', APFSURL.'/js/apf.js', array('jquery'));
    wp_localize_script( 'apf', 'apfajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
	
}
add_action('wp_enqueue_scripts', apf_enqueuescripts);
  
function apf_post_form()
{
    ?>
    
    <form id="apfform" action="" method="post"enctype="multipart/form-data">
 
        <div id="apf-text">
  		<input type="text" class="orderresult_qty" id="apflink" name="apflink" value="508"/>
        <input type="hidden" class="orderresult_qty" id="apftitle" name="apftitle" value="SOI<?php the_ID();?>"/>
        <input type="text" class="orderresult_qty" id="apfqty" name="apfqty"/>
        <input type="text" class="orderresult_qty" id="apfdesc" name="apfdesc" value="<?php echo the_title() ;?>"/>
        <input type="text" class="orderresult_qty" id="apfcode" name="apfcode" value="<?php echo the_field('style_code') ;?>"/>
        <input type="text" class="orderresult_qty" id="apfprice" name="apfprice" value="<?php echo the_field('selling_price') ;?>"/>
          <a class="button" id="addbutton" onclick="apfaddpost(apftitle.value,apfqty.value,apflink.value,apfcode.value,apfdesc.value,apfprice.value);" style="cursor: pointer">Add</a>
 
 		
      </div>
      
      <div id="apf-response" style="background-color:#f4f0ed"></div>
      
    </form>
 
    <?php
}

function apf_addpost() {
    $results = '';
 
    $title = $_POST['apftitle'];
    $orderqty =  $_POST['apfqty'];
	$orderlink = $_POST['apflink'];
	$ordercode = $_POST['apfcode'];
	$orderdesc = $_POST['apfdesc'];
	$orderprice = $_POST['apfprice'];
	
  
    $post_id = wp_insert_post( array(
          'post_type'		=> 'order-items',
          'post_author'		=> $user->ID, 
  	      'post_title'      => $title,
          'post_status' 	=> 'publish'
    ) );
 	add_post_meta($post_id, 'linked_order_id', $orderlink, true);
	add_post_meta($post_id, 'product_code', $ordercode, true);
	add_post_meta($post_id, 'product_description', $orderdesc, true);
	add_post_meta($post_id, 'quantity', $orderqty, true);
	add_post_meta($post_id, 'unit_price', $orderprice, true);
	add_post_meta($post_id, 'line_item_total', $orderqty * $orderprice , true);
	add_post_meta($post_id, 'vat_amount', ($orderqty * $orderprice)*0.14 , true);
	add_post_meta($post_id, 'line_item_total_vat', (($orderqty * $orderprice)*0.14) + ($orderqty * $orderprice) , true);
 
    if ( $post_id != 0 )
    {
        $results = '*Item Added';
		wp_reset_postdata();
    }
    else {
        $results = '*Error occurred while adding the item';
    }
    // Return the String
    die($results);
}



// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_apf_addpost', 'apf_addpost' );
add_action( 'wp_ajax_apf_addpost', 'apf_addpost' );