Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Last active August 29, 2015 14:14
Show Gist options
  • Save bappi-d-great/cdfa745adf93967a0514 to your computer and use it in GitHub Desktop.
Save bappi-d-great/cdfa745adf93967a0514 to your computer and use it in GitHub Desktop.
Get a quote form for WPMU marketpress plugin
<?php
/*
Plugin Name: MarketPress Get a Quote
Plugin URI: http://bappi-d-great.com
Description: Embed a form in single product page
Version: 1.1
Author: Ashok (WPMU DEV)
Author URI: http://bappi-d-great.com
Copyright 2009-2013 Incsub (http://incsub.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
Usage: It will show a button in the product page called "Get a Quote". When you click on that button you will see a form,
fill the form, submit the form and data will be saved.
You can see the data in the admin end, just edit the product and the all the submitted data you will see in a meta box.
*/
global $mp;
if( ! $mp ){
}else{
add_action( 'add_meta_boxes', 'mp_get_a_quote' );
function mp_get_a_quote() {
add_meta_box(
'get_a_quote',
'Get a Quote Data',
'mp_get_a_quote_callback',
'product'
);
}
function mp_get_a_quote_callback( $post ){
$quotes = get_post_meta( $post->ID, 'product_quote_data', true );
?>
<table cellpadding="10" cellspacing="10" width="100%">
<tr style="border-bottom: 1px solid #ccc;">
<td><strong>Name</strong></td>
<td><strong>Email</strong></td>
<td width="50%"><strong>Comment</strong></td>
</tr>
<?php foreach( $quotes as $quote ) { ?>
<tr>
<td><?php echo $quote['name'] ?></td>
<td><?php echo $quote['email'] ?></td>
<td><?php echo $quote['comment'] ?></td>
</tr>
<?php } ?>
</table>
<?php
}
add_filter( 'the_content', 'the_content_mp_cb' );
function the_content_mp_cb( $content ){
global $post;
$html = '';
if( $post->post_type == 'product' ){
$html .= '<div class="quote_form">';
$html .= '<input type="hidden" id="q_product_id" value="' . $post->ID . '">';
$html .= '<button type="button" id="q_button">Get a Quote</button>';
$html .= '<table cellpadding="10" cellspacing="10" class="q_form">';
$html .= '<tr>';
$html .= '<td>Name</td>';
$html .= '<td>:</td>';
$html .= '<td><input type="text" id="q_name"></td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td>Email</td>';
$html .= '<td>:</td>';
$html .= '<td><input type="text" id="q_email"></td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td>Comment</td>';
$html .= '<td>:</td>';
$html .= '<td><textarea id="q_comment"></textarea>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td>&nbsp;</td>';
$html .= '<td>&nbsp;</td>';
$html .= '<td><button type="button" id="q_submit">Get a quote</button>';
$html .= '</tr>';
$html .= '</table>';
$html .= '</div>';
return $content . $html;
}else{
return $content;
}
}
add_action( 'wp_head', 'mp_quote_script' );
function mp_quote_script() {
?>
<style>
.quote_form{overflow: hidden}
#q_button{display: block; margin-bottom: 20px;}
.q_form{display: none;}
</style>
<script type="text/javascript">
jQuery(function($) {
$('#q_button').click(function() {
$(this).next( '.q_form' ).slideToggle();
});
$('#q_submit').click(function() {
$('.q_form').hide();
var data = {
name : $('#q_name').val(),
email : $('#q_email').val(),
comment : $('#q_comment').val(),
id: $('#q_product_id').val(),
action : 'mp_quote'
};
$.post( '<?php echo admin_url( 'admin-ajax.php' ) ?>', data, function(response){
$('#q_button').after('Your data is submitted successfully.');
});
});
});
</script>
<?php
}
add_action( 'wp_ajax_mp_quote', 'mp_quote_action_callback' );
add_action( 'wp_ajax_nopriv_mp_quote', 'mp_quote_action_callback' );
function mp_quote_action_callback() {
$data['name'] = $_POST['name'];
$data['email'] = $_POST['email'];
$data['comment'] = $_POST['comment'];
$id = $_POST['id'];
$arr = get_post_meta( $id, 'product_quote_data', true );
if( ! is_array( $arr ) ) $arr = array();
array_push( $arr, $data );
update_post_meta( $id, 'product_quote_data', $arr );
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment